PHP GD库处理图片的相关常用函数(二)

转载来自:http://blog.csdn.net/jiaobuchong/article/details/22477471

本节主要总结一下对于特定格式(png. jpg, gif)图片的一些处理。应用场景:处理用户的上传头像,处理上传图片……

imagecreatefromgif();

imagecreatefrompng();

imagecreatefromjpeg();

上面三个函数表示打开一个图像资源,参数是传入相应的图片地址。

[php]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2. $img = imagecreatefromgif('./clock.gif');  
  3. echo 'width:', imagesx($img), '<br />';  //获取图像的宽度,需要一个资源参数才能获取  
  4. echo 'height:', imagesy($img), '<br />';  
  5. $arr = getimagesize('./clock.gif');     //直接是一个文件名就可以获取图片的信息  
  6. print_r($arr);                          //下标2代表图像类型  
  7. echo '<br />width:'$arr[0], '<br />';  //获取图像的宽度  
  8. echo 'height:'$arr[1], '<br />';  
  9. imagegif($img'./dealed/clock.gif');  
  10. imagedestroy($img);  
  11. ?>  
上面的代码在浏览器里的输出结果是:

width:550
height:400
Array ( [0] => 550 [1] => 400 [2] => 1 [3] => width="550" height="400" [bits] => 7 [channels] => 3 [mime] => image/gif ) 
width:550
height:400


一、图片的缩放及透明处理

[php]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2. $per = 0.5;  //缩放一半  
  3. list($width$height) = getimagesize('./fire.jpg');  
  4. $new_w = 0.5 * $width//缩放后的宽度  
  5. $new_h = 0.5 * $height//缩放后的高度  
  6. $new_s = imagecreatetruecolor($new_w$new_h);  //创建真彩图像  
  7. $img = imagecreatefromjpeg('./fire.jpg');       //获得原图资源  
  8. /** 
  9.  * imagecopyresized()就是从原图的(0, 0)到($width, $height) 复制到新创建的(0, 0)到($new_w, $new_h)上,实现缩放 
  10.  **/  
  11. imagecopyresized($new_s$img, 0, 0, 0, 0, $new_w$new_h$width$height);  
  12. imagejpeg($new_s'./dealed/fire.jpg');  
  13. imagedestroy($new_s);  
  14. imagedestroy($img);  
  15. ?>  

上面的代码就会在dealed目录下有一个缩放的图像。

下面是一个按一定比例缩放的代码:

[php]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2. function resample_pic($source$width$height$newFileName)  
  3. {  
  4.     list($source_w$source_h) = getimagesize($source);  
  5.   
  6.     /** 
  7.      * $ratio, 原图的宽除以高,得到一个比例 
  8.      **/  
  9.     $ratio = $source_w / $source_h;   
  10.     if ($width/$height > $ratio//最大输出图像比例和原来的图像的比例进行对比  
  11.     {  
  12.         $width = $height * $ratio;  //说明高较大  
  13.     }  
  14.     else  
  15.     {  
  16.         $height = $width / $ratio;   //说明宽较大  
  17.     }  
  18.    
  19.     $new_s = imagecreatetruecolor($width$height);  
  20.     $img = imagecreatefromjpeg($source);  
  21.     imagecopyresampled($new_s$img, 0, 0, 0, 0, $width$height$source_w$source_h);  
  22.     imagejpeg($new_s$newFileName, 100);  
  23.     imagedestroy($new_s);  
  24.     imagedestroy($img);  
  25. }  
  26. //这个例子会以最大宽度高度为 200 像素显示一个图像  
  27. resample_pic('./fire.jpg', 200, 200, './dealed/fire1.jpg');  
  28. ?>  
imagecopyresampled(),采用插值算法平滑的插入图像,速度较imagecopyresized()慢,如果对缩略图要求不高,可以使用imagecopyresized()。

但这有个问题,就是在缩放gif格式的图片时,缩放后的gif透明度不正常,而png和jpeg的图片透明色正常。那如何处理gif图片呢?看代码:

[php]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2. function resample_pic($source$width$height$newFileName)  
  3. {  
  4.     list($source_w$source_h) = getimagesize($source);  
  5.   
  6.     /** 
  7.      * $ratio, 原图的宽除以高,得到一个比例 
  8.      **/  
  9.     $ratio = $source_w / $source_h;   
  10.     if ($width/$height > $ratio//最大输出图像比例和原来的图像的比例进行对比  
  11.     {  
  12.         $width = $height * $ratio;  //说明高较大  
  13.     }  
  14.     else  
  15.     {  
  16.         $height = $width / $ratio;   //说明宽较大  
  17.     }  
  18.  /** 
  19.   * imagecolortransparent(resource $image [, int color]), 将某个颜色定义为透明色,如果省略color则返回当前透明色的标识符, 
  20.   *  和 int imagecolorat ( resource $image , int $x , int $y ) 返回值一样 
  21.   * imagecolorstotal(resource $image), 取得一幅图像的调色板中颜色的数目 
  22.   * imagecolorsforindex(resource $image, int $index)  取得某索引的颜色,本函数返回一个具有 red,green,blue 和 alpha 
  23.   * 的键名的关联数组,包含了指定颜色索引的相应的值,  
  24.   **/  
  25.     $new_s = imagecreatetruecolor($width$height);  
  26.     $img = imagecreatefromgif($source);  
  27.   
  28.     //关于缩放gif图片后透明色有不足的解决之道:  
  29.     $trans_index = imagecolortransparent($img);  //返回当前图片的透明色标示符  
  30.     if ($trans_index >= 0 && $trans_index < imagecolorstotal($img))  
  31.     {  
  32.         //原图有透明色  
  33.         $color_index = imagecolorsforindex($img$trans_index);  //取得透明色的rgb值  
  34.         //print_r($color_index);  
  35.         //指定透明色颜色  
  36.         $trans_color = imagecolorallocate($img$color_index['red'], $color_index['green'], $color_index['blue']);  
  37.         imagefill($new_s, 0, 0, $trans_color); //填充背景色,但还不是透明色  
  38.         imagecolortransparent($new_s$trans_color);  //为图像指定透明色  
  39.     }  
  40.     imagecopyresampled($new_s$img, 0, 0, 0, 0, $width$height$source_w$source_h);  
  41.     imagegif($new_s$newFileName, 100);  
  42.     imagedestroy($new_s);  
  43.     imagedestroy($img);  
  44.   
  45. }  
  46. //这个例子会以最大宽度高度为 200 像素显示一个图像  
  47. resample_pic('./heben.gif', 200, 200, './dealed/heben.gif');  
  48. ?>  

二、图片的裁剪

[php]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2. //图像的裁剪  
  3. function tailor($source$cut_x$cut_y$cut_width$cut_height$newFileName)  
  4. {  
  5.     $img = imagecreatefromjpeg($source);  
  6.     $new_s = imagecreatetruecolor($cut_width$cut_height); //新的图片资源  
  7.     imagecopyresampled($new_s$img, 0, 0, $cut_x$cut_y$cut_width$cut_height$cut_width$cut_height);  
  8.     imagejpeg($new_s$newFileName);  
  9.   
  10.     imagedestroy($new_s);  
  11.     imagedestroy($img);  
  12. }  
  13. /** 
  14.  * 从图片的(10, 10)开始裁宽200px,高200px的图片 
  15.  **/  
  16. tailor('./fire.jpg', 10, 10, 200, 200, './dealed/fire_cut.jpg');  
  17. ?>  

三、加水印(文字,图片)

加文字的水印:

[php]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2. //加文本水印  
  3. function addTextWatermark($source$text$x$y$newFileName)  
  4. {  
  5.     $img = imagecreatefromjpeg($source);  
  6.     $color = imagecolorallocate($img, 0, 255, 0);  
  7.   
  8.     //imagettftext()默认只能画utf-8的字体,如果是输入是gbk的,可以  
  9.     //iconv('gbk', 'utf-8', $text);  
  10.     imagettftext($img, 20, 0, $x$y$color'msyh.ttf'$text);  
  11.     imagejpeg($img$newFileName);  
  12.     imagedestroy($img);  
  13. }  
  14. //在原图片的(300, 400)处加“jiaobuchong”的一个水印  
  15. addTextWatermark('./fire.jpg''jiaobuchong', 300, 400, './dealed/fire_addwatermark.jpg');  
  16. ?>  

效果:


加图片的水印:

[php]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2. //加图片的水印  
  3. function addPicWatermark($source$waterpic$x$y$newFileName)  
  4. {  
  5.     $img = imagecreatefromjpeg($source);  
  6.     $water = imagecreatefromgif($waterpic);  
  7.       
  8.     $water_w = imagesx($water); //获取水印图片的宽度  
  9.     $water_h = imagesy($water); //获取水印图片的高度  
  10.   
  11.     imagecopy($img$water$x$y, 0, 0, $water_w$water_h);  //复制图片  
  12.     imagejpeg($img$newFileName);  
  13.     imagedestroy($img);  
  14.     imagedestroy($water);  
  15. }  
  16. //将clock.gif 从原图fire.jpg的(300, 200)放入水印图片  
  17. addPicWatermark('./fire.jpg''clock.gif', 300, 200, './dealed/fire_addwaterpic.jpg');  
  18. ?>  

效果图:


四、图片的旋转 

[php]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2. //图像旋转函数  
  3. //resource imagerotate ( resource $image , float $angle , int $bgd_color   
  4. //[, int $ignore_transparent = 0 ] )  
  5. $img = imagecreatefromjpeg('./girl.jpg');  
  6. $new_s = imagerotate($img, 40, 0);  //0, 图片旋转40 , 旋转后的空白是黑色,其颜色可以自己指定  
  7. imagejpeg($new_s'./dealed/girl_rotate.jpg');  
  8. imagedestroy($img);  
  9. imagedestroy($new_s);  
  10. ?>  

效果如下面的图片。



五、图片的翻转

沿Y轴:

[php]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2. //沿Y轴翻转  
  3. function turn_Y($src$newfile)  
  4. {  
  5.     $img = imagecreatefromjpeg($src);  
  6.     $width = imagesx($img);  //原图宽度  
  7.     $height = imagesy($img);  //原图高度  
  8.   
  9.     $new_s = imagecreatetruecolor($width$height);  
  10.   
  11.     for ($x = 0; $x < $width$x++)  
  12.     {  
  13.         /*此处就是一个一个像素的复制*/  
  14.         imagecopy($new_s$img$width-$x-1, 0, $x, 0, 1, $height);  
  15.     }  
  16.     imagejpeg($new_s$newfile);  
  17.     imagedestroy($img);  
  18.     imagedestroy($new_s);  
  19. }  
  20. turn_Y('./girl.jpg''./dealed/girl_Y.jpg');  
  21. ?>  

效果如下图片:


沿X轴:

[php]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2. //沿X轴翻转  
  3. function turn_X($src$newfile)  
  4. {  
  5.     $img = imagecreatefromjpeg($src);  
  6.     $width = imagesx($img);  
  7.     $height = imagesy($img);  
  8.   
  9.     $new_s = imagecreatetruecolor($width$height);  
  10.   
  11.     for ($y = 0; $y < $height$y++)  
  12.     {  
  13.         imagecopy($new_s$img, 0, $height-$y-1, 0, $y$width, 1);  
  14.     }  
  15.     imagejpeg($new_s$newfile);  
  16.     imagedestroy($img);  
  17.     imagedestroy($new_s);  
  18. }  
  19. turn_X('./girl.jpg''./dealed/girl_X.jpg');  
  20. ?>  

效果如图:


六、图像特效

bool imagefilter (resource$src_im , int$filtertype [, int$arg1 [,int$arg2 [,int$arg3 ]]] )   

访问更多特效,请看http://www.php.net/manual/zh/function.imagefilter.php

[php]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2. $img = imagecreatefromjpeg('./cat.jpg');  
  3. //imagefilter($img, IMG_FILTER_NEGATE);  将图像中所有颜色反转  
  4. //imagefilter($img, IMG_FILTER_GRAYSCALE); 将图像转换为灰度的  
  5. //imagefilter($img, IMG_FILTER_EDGEDETECT); 用边缘检测来突出图像的边缘  
  6. //imagefilter($img,IMG_FILTER_MEAN_REMOVAL);  用平均移除法来达到轮廓效果  
  7. imagefilter($img, IMG_FILTER_SMOOTH, 200);    //使图像更柔滑。用 arg1 设定柔滑级别  
  8. imagepng($img'./dealed/cat1.png');  
  9. imagedestroy($img);  
  10. ?>  
看图:


最后再来看几个比较有意思的函数:

[php]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2. /** 
  3.     *imagecolorallocate() 
  4.     *imagecolortransparent() 
  5.     *imagecolorat() 
  6.     *imagecolorexact() 
  7.  **/  
  8. $img = imagecreatetruecolor(500, 500);  
  9. $hotpink = imagecolorallocate($img, 255, 105, 180); //为一幅图像分配颜色  
  10. echo $hotpink'<br />';  
  11. imagefill($img, 0, 0, $hotpink);  
  12. $index_tran = imagecolortransparent($img$hotpink);//将某个颜色定义为透明色  
  13. echo $index_tran'<br />';      
  14. $index_at = imagecolorat($img, 200, 200);   //取得具体位置像素的颜色索引值  
  15. echo $index_tran'<br />';  
  16. $index_exact = imagecolorexact($img, 255, 105, 180);   //取得指定颜色的索引值  
  17. echo $index_exact;  
  18. imagepng($img'./test.png');  
  19. imagedestroy($img);  
  20. ?>  
输出结果如:

16738740
16738740
16738740
16738740

可以看出它们的相似点了吧?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值