PHP图片处理之图片背景、画布操作,
PHP图片处理之图片背景、画布操作,
像验证码或根据动态数据生成统计图标,以及前面介绍的一些GD库操作等都属于动态绘制图像。而在web开发中,也会经常去处理服务器中已存在的图片。例如,根据一些需求对图片进行缩放、加水印、裁剪、翻转和旋转等改图的操作。在web应用中,经常使用的图片格式有GIF、JPEG和PNG中的一种或几种,当然GD库也可以处理其他格式的图片,但都很少用到。所以安装GD库时,至少安装GIF、JPEG或PNG三种格式中的一种。
在前面介绍的画布管理中,使用imagecreate()和imageCreateTrueColor()两个函数去创建画布资源。但如果需要对自己已有的图片进行处理,只要将这个图片作为画布资源即可,也就是我们所说的创建图片背景。可以通过下面介绍的几个函数,打开服务器或网络文件中已经存在的GIF、JPEG和PNG图像,返回一个图像标识符,代表了从给定的文件名取得的图像作为操作的背景资源。它们的原型如下所示,它们在失败时都会返回一个空字符串,并且输出一条错误信息。
复制代码 代码如下:
resource imagecreatefromjpeg(string $filename) //从JPEG文件或URL新建一图像
resource imagecreatefrompng(string $filename) //从PNG文件或URL新建一图像
resource imagecreatefromgif(string $filename) //从GIF文件或URL新建一图像
不管使用哪个函数创建的图像资源,用完以后都需要使用imagedestroy()函数进行销毁。再有就是图片格式对应的问题,任何一种方式打开的图片资源都可以保存为同一种格式。例如,对于使用imagecreatefromjpeg()函数创建的图片资源,可以使用imagepng()函数以PNG格式将图像输出到浏览器或文件。当然最好是打开的是哪种格式的图片,就保存成对应的图片格式。如果要做到这一点,我们还需要先认识一下getimagesize()函数,通过图片名称就可以获取图片的类型、宽度和高度等。该函数的原型如下所示:
复制代码 代码如下:
array getimagesize(string filename[,array &imageinfo]) //获取图片的大小和类型
如果不能访问filename指定的图像或者其不是有效图像,该函数将返回FALSE并产生一条E_WARNING级的错误。如果不出错,getimagesize()返回一个具有四个单元的数组,索引0包含图像宽度的像素值,索引1包含图像高度的索引值,索引2是图像类型的标记:1=GIF,2=JPG,3=PNG,4=SWF等,索引3是文本字符串,内容为“height=”yyy” width=”xxx””,可以直接用于标记。如下所示:
复制代码 代码如下:
list($width,$height,$type,$attr) = getimagesize("image/brophp.jpg");
echo "";
?>
下面的例子声明一个image()函数,可以打开GIF、JPG和PNG中任意格式的图片,并在图片的中间加上一个字符串后,保存成原来的格式(文字水印)。在以后的开发中,如果需要同样的操作(打开的是哪种格式的图片,也保存成对应格式的文件),可以参与本例的模式,代码如下所示:
复制代码 代码如下:
//向不同格式的图片中间画一个字符串(也是文字水印)
function image($filename,$string){
//获取图片的属性,第一个宽度,第二个高度,类型1=>gif,2=>jpeg,3=>png
list($width,$height,$type) = getimagesize($filename);
//可以处理的图片类型
$types = array(1=>"gif",2=>"jpeg",3=>"png",);
//通过图片类型去组合,可以创建对应图片格式的,创建图片资源的GD库函数
$createfrom = "imagecreatefrom".$types[$type];
//通过“变量函数”去打对应的函数去创建图片的资源
$image = $createfrom($filename);
//设置居中字体的X轴坐标位置
$x = ($width-imagefontwidth(5)*strlen($string))/2;
//设置居中字体的Y轴坐标位置
$y = ($height-imagefontheight(5))/2;
//设置字体的颜色为红色
$textcolor = imagecolorallocate($image, 255, 0, 0);
//向图片画一个指定的字符串
imagestring($image, 5, $x, $y, $string, $textcolor);
//通过图片类型去组合保存对应格式的图片函数
$output = "image".$types[$type];
//通过变量函数去保存对应格式的图片
$output($image,$filename);
imagedestroy($image);
}
image("brophp.gif","GIF");
image("brophp.jpg", "JPEG");
image("brophp.png", "PNG");
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.
