注意:如果有同学在本地环境使用代码输出后,没有图片,一是查看下php.ini里gd库是否开启,前面注释;去掉。如果还是不显示,只出现一个小小的正方形。在代码前面加ob_clean();
GD指的是Graphic Device,PHP的GD库是用来处理图形的扩展库,通过GD库提供的一系列API,可以对图像进行处理或者直接生成新的图片。
PHP除了能进行文本处理以外,通过GD库,可以对JPG、PNG、GIF、SWF等图片进行处理。
GD库常用在图片加水印,验证码生成等方面。
PHP默认已经集成了GD库,只需要在安装的时候开启就行。
$img = imagecreatetruecolor(100, 100); // 创建长、宽100图像(默认为黑色) $red = imagecolorallocate($img, 0xFF, 0x00, 0x00); // 建立的图像,颜色,颜色,颜色 imagefill($img, 0, 0, $red); // 建立图像,x,y,填充的颜色 $blue = imagecolorallocate($img, 0x00, 0x00, 0xFF); // 建立的图像,颜色,颜色,颜色 imageline($img, 0, 0, 100, 100, $blue); header("content-type: image/png"); imagepng($img); // 以 PNG 格式将图像输出到浏览器,可以改变格式 imagepng($img, 'img1.png'); // 以 PNG 格式将图像输出到文件,可以改变格式 imagedestroy($img); // 销毁图片要对图形进行操作,首先要新建一个画布,通过imagecreatetruecolor函数可以创建一个真彩色的空白图片:
$img = imagecreatetruecolor(100, 100); header("content-type: image/png"); imagepng($img); imagedestroy($img);GD库中对于画笔所用的颜色,需要通过imagecolorallocate函数进行分配,通过参数设定RGB的颜色值来确定画笔的颜色:
$img = imagecreatetruecolor(100, 100); $red = imagecolorallocate($img, 0xFF, 0x00, 0x00); imagefill($img, 0, 0, $red); header("content-type: image/png"); imagepng($img); imagedestroy($img);通过调用绘制线段函数imageline进行线条的绘制,通过指定起点跟终点来最终得到线条。
$img = imagecreatetruecolor(100, 100); $red = imagecolorallocate($img, 0xFF, 0x00, 0x00); imageline($img, 0, 0, 100, 100, $red); header("content-type: image/png"); imagepng($img); imagedestroy($img);线条绘制好以后,通过header与imagepng进行图像的输出。
使用 imagepng 将图像保存成png格式,
使用 imagejpeg 将图片保存成jpeg格式,
使用 imagegif 将图片保存成gif格式,
需要说明的是,imagejpeg会对图片进行压缩,因此还可以设置一个质量参数
$img = imagecreatetruecolor(100, 100); // 创建长、宽100图像(默认为黑色) $red = imagecolorallocate($img, 0xFF, 0x00, 0x00); // 建立的图像,颜色,颜色,颜色 imagestring($img, 5, 0, 40, "Hello world", $red); header("content-type: image/png"); imagepng($img); // 以 PNG 格式将图像输出到浏览器 imagepng($img, 'img1.png'); // 以 PNG 格式将图像输出到文件 imagejpeg($img, 'img1.jpeg', 80); // 以 JPEG 格式将图像输出到文件 imagedestroy($img);最后可以调用imagedestroy释放该图片占用的内存。
imagedestroy($img);跟绘制线条类似,首先需要新建一个图片与初始化颜色。
使用imagestring函数来进行文字的绘制
语法:imagestring(int img, int font, int x, int y, string s, int col);
$img = imagecreatetruecolor(100, 100); // 创建长、宽100图像(默认为黑色) $red = imagecolorallocate($img, 0xFF, 0x00, 0x00); // 建立的图像,颜色,颜色,颜色 imagestring($img, 5, 0, 40, "Hello world", $red); header("content-type: image/png"); imagepng($img); imagedestroy($img);imagestring 使用的是点阵字体 ,想要输中文,常用的方法是使用imagettftext 函数
$img = imagecreatetruecolor(100, 100); // 创建长、宽100图像(默认为黑色) $red = imagecolorallocate($img, 0xFF, 0x00, 0x00); // 建立的图像,颜色,颜色,颜色 $font = "C://windows/fonts/simhei.ttf"; $text='中国'; $text = mb_convert_encoding($text,'UTF-8','GB2312'); imagettftext($img, 10, 4, 20, 20, $red, $font, $text); header("content-type: image/png"); imagepng($img); imagedestroy($img); // TODO:输出乱码为了使验证码更加的安全,防止其他程序自动识别,因此常常需要对验证码进行一些干扰处理,通常会采用: - 绘制一些噪点 imagesetpixel() - 干扰线段 imageline() - 对输出的字符进行倾斜、扭曲等操作。
可以使用 imagesetpixel() 绘制点来实现噪点干扰,但是只绘制一个点的作用不大,因此这里常常会使用循环进行随机绘制。
// 创建验证码画布 $img = imagecreatetruecolor(100, 40); $black = imagecolorallocate($img, 0x00, 0x00, 0x00); $green = imagecolorallocate($img, 0x00, 0xFF, 0x00); $blue = imagecolorallocate($img, 0x00, 0x00, 0xFF); $white = imagecolorallocate($img, 0xFF, 0xFF, 0xFF); imagefill($img,0,0,$white); // 生成随机的验证码 [0, 9] $code = ''; for($i = 0; $i < 4; $i++) { $code .= rand(0, 9); } imagestring($img, 5, 35, 12, $code, $black); // 加入噪点干扰 for($i = 0; $i < 100; $i ++) { imagesetpixel($img, rand(0, 100) , rand(0, 100) , $black); imagesetpixel($img, rand(0, 100) , rand(0, 100) , $green); imagesetpixel($img, rand(0, 100) , rand(0, 100) , $blue); } // 输出验证码 header("content-type: image/png"); imagepng($img); imagedestroy($img);给图片添加水印的方法一般有两种:一种是在图片上面加上一个字符串,另一种是在图片上加上一张水印图片。
因为这里处理的是已经存在的图片,所以可以直接从已存在的图片建立画布,通过imagecreatefromjpeg可以直接从图片文件创建图像。
// 这里仅仅是为了案例需要准备一些素材图片 $url = 'http://www.iyi8.com/uploadfile/2014/0521/20140521105216901.jpg'; $content = file_get_contents($url); $filename = 'tmp.jpg'; file_put_contents($filename, $content); $url = 'http://wiki.ubuntu.org.cn/images/3/3b/Qref_Edubuntu_Logo.png'; file_put_contents('logo.png', file_get_contents($url)); // 开始添加水印操作 // 通过GD函数imagecopy将logo的图像复制到源图像中。 $im = imagecreatefromjpeg($filename); $logo = imagecreatefrompng('logo.png'); $size = getimagesize('logo.png'); imagecopy($im, $logo, 15, 15, 0, 0, $size[0], $size[1]); header("content-type: image/jpeg"); imagejpeg($im);