转载来自:http://blog.csdn.net/jiaobuchong/article/details/22477471
本节主要总结一下对于特定格式(png. jpg, gif)图片的一些处理。应用场景:处理用户的上传头像,处理上传图片……
imagecreatefromgif();
imagecreatefrompng();
imagecreatefromjpeg();
上面三个函数表示打开一个图像资源,参数是传入相应的图片地址。
- <?php
- $img = imagecreatefromgif('./clock.gif');
- echo 'width:', imagesx($img), '<br />'; //获取图像的宽度,需要一个资源参数才能获取
- echo 'height:', imagesy($img), '<br />';
- $arr = getimagesize('./clock.gif'); //直接是一个文件名就可以获取图片的信息
- print_r($arr); //下标2代表图像类型
- echo '<br />width:', $arr[0], '<br />'; //获取图像的宽度
- echo 'height:', $arr[1], '<br />';
- imagegif($img, './dealed/clock.gif');
- imagedestroy($img);
- ?>
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
- $per = 0.5; //缩放一半
- list($width, $height) = getimagesize('./fire.jpg');
- $new_w = 0.5 * $width; //缩放后的宽度
- $new_h = 0.5 * $height; //缩放后的高度
- $new_s = imagecreatetruecolor($new_w, $new_h); //创建真彩图像
- $img = imagecreatefromjpeg('./fire.jpg'); //获得原图资源
- /**
- * imagecopyresized()就是从原图的(0, 0)到($width, $height) 复制到新创建的(0, 0)到($new_w, $new_h)上,实现缩放
- **/
- imagecopyresized($new_s, $img, 0, 0, 0, 0, $new_w, $new_h, $width, $height);
- imagejpeg($new_s, './dealed/fire.jpg');
- imagedestroy($new_s);
- imagedestroy($img);
- ?>
上面的代码就会在dealed目录下有一个缩放的图像。
下面是一个按一定比例缩放的代码:
- <?php
- function resample_pic($source, $width, $height, $newFileName)
- {
- list($source_w, $source_h) = getimagesize($source);
- /**
- * $ratio, 原图的宽除以高,得到一个比例
- **/
- $ratio = $source_w / $source_h;
- if ($width/$height > $ratio) //最大输出图像比例和原来的图像的比例进行对比
- {
- $width = $height * $ratio; //说明高较大
- }
- else
- {
- $height = $width / $ratio; //说明宽较大
- }
- $new_s = imagecreatetruecolor($width, $height);
- $img = imagecreatefromjpeg($source);
- imagecopyresampled($new_s, $img, 0, 0, 0, 0, $width, $height, $source_w, $source_h);
- imagejpeg($new_s, $newFileName, 100);
- imagedestroy($new_s);
- imagedestroy($img);
- }
- //这个例子会以最大宽度高度为 200 像素显示一个图像
- resample_pic('./fire.jpg', 200, 200, './dealed/fire1.jpg');
- ?>
但这有个问题,就是在缩放gif格式的图片时,缩放后的gif透明度不正常,而png和jpeg的图片透明色正常。那如何处理gif图片呢?看代码:
- <?php
- function resample_pic($source, $width, $height, $newFileName)
- {
- list($source_w, $source_h) = getimagesize($source);
- /**
- * $ratio, 原图的宽除以高,得到一个比例
- **/
- $ratio = $source_w / $source_h;
- if ($width/$height > $ratio) //最大输出图像比例和原来的图像的比例进行对比
- {
- $width = $height * $ratio; //说明高较大
- }
- else
- {
- $height = $width / $ratio; //说明宽较大
- }
- /**
- * imagecolortransparent(resource $image [, int color]), 将某个颜色定义为透明色,如果省略color则返回当前透明色的标识符,
- * 和 int imagecolorat ( resource $image , int $x , int $y ) 返回值一样
- * imagecolorstotal(resource $image), 取得一幅图像的调色板中颜色的数目
- * imagecolorsforindex(resource $image, int $index) 取得某索引的颜色,本函数返回一个具有 red,green,blue 和 alpha
- * 的键名的关联数组,包含了指定颜色索引的相应的值,
- **/
- $new_s = imagecreatetruecolor($width, $height);
- $img = imagecreatefromgif($source);
- //关于缩放gif图片后透明色有不足的解决之道:
- $trans_index = imagecolortransparent($img); //返回当前图片的透明色标示符
- if ($trans_index >= 0 && $trans_index < imagecolorstotal($img))
- {
- //原图有透明色
- $color_index = imagecolorsforindex($img, $trans_index); //取得透明色的rgb值
- //print_r($color_index);
- //指定透明色颜色
- $trans_color = imagecolorallocate($img, $color_index['red'], $color_index['green'], $color_index['blue']);
- imagefill($new_s, 0, 0, $trans_color); //填充背景色,但还不是透明色
- imagecolortransparent($new_s, $trans_color); //为图像指定透明色
- }
- imagecopyresampled($new_s, $img, 0, 0, 0, 0, $width, $height, $source_w, $source_h);
- imagegif($new_s, $newFileName, 100);
- imagedestroy($new_s);
- imagedestroy($img);
- }
- //这个例子会以最大宽度高度为 200 像素显示一个图像
- resample_pic('./heben.gif', 200, 200, './dealed/heben.gif');
- ?>
二、图片的裁剪
- <?php
- //图像的裁剪
- function tailor($source, $cut_x, $cut_y, $cut_width, $cut_height, $newFileName)
- {
- $img = imagecreatefromjpeg($source);
- $new_s = imagecreatetruecolor($cut_width, $cut_height); //新的图片资源
- imagecopyresampled($new_s, $img, 0, 0, $cut_x, $cut_y, $cut_width, $cut_height, $cut_width, $cut_height);
- imagejpeg($new_s, $newFileName);
- imagedestroy($new_s);
- imagedestroy($img);
- }
- /**
- * 从图片的(10, 10)开始裁宽200px,高200px的图片
- **/
- tailor('./fire.jpg', 10, 10, 200, 200, './dealed/fire_cut.jpg');
- ?>
三、加水印(文字,图片)
加文字的水印:
- <?php
- //加文本水印
- function addTextWatermark($source, $text, $x, $y, $newFileName)
- {
- $img = imagecreatefromjpeg($source);
- $color = imagecolorallocate($img, 0, 255, 0);
- //imagettftext()默认只能画utf-8的字体,如果是输入是gbk的,可以
- //iconv('gbk', 'utf-8', $text);
- imagettftext($img, 20, 0, $x, $y, $color, 'msyh.ttf', $text);
- imagejpeg($img, $newFileName);
- imagedestroy($img);
- }
- //在原图片的(300, 400)处加“jiaobuchong”的一个水印
- addTextWatermark('./fire.jpg', 'jiaobuchong', 300, 400, './dealed/fire_addwatermark.jpg');
- ?>
效果:
- <?php
- //加图片的水印
- function addPicWatermark($source, $waterpic, $x, $y, $newFileName)
- {
- $img = imagecreatefromjpeg($source);
- $water = imagecreatefromgif($waterpic);
- $water_w = imagesx($water); //获取水印图片的宽度
- $water_h = imagesy($water); //获取水印图片的高度
- imagecopy($img, $water, $x, $y, 0, 0, $water_w, $water_h); //复制图片
- imagejpeg($img, $newFileName);
- imagedestroy($img);
- imagedestroy($water);
- }
- //将clock.gif 从原图fire.jpg的(300, 200)放入水印图片
- addPicWatermark('./fire.jpg', 'clock.gif', 300, 200, './dealed/fire_addwaterpic.jpg');
- ?>
效果图:
- <?php
- //图像旋转函数
- //resource imagerotate ( resource $image , float $angle , int $bgd_color
- //[, int $ignore_transparent = 0 ] )
- $img = imagecreatefromjpeg('./girl.jpg');
- $new_s = imagerotate($img, 40, 0); //0, 图片旋转40 , 旋转后的空白是黑色,其颜色可以自己指定
- imagejpeg($new_s, './dealed/girl_rotate.jpg');
- imagedestroy($img);
- imagedestroy($new_s);
- ?>
效果如下面的图片。
沿Y轴:
- <?php
- //沿Y轴翻转
- function turn_Y($src, $newfile)
- {
- $img = imagecreatefromjpeg($src);
- $width = imagesx($img); //原图宽度
- $height = imagesy($img); //原图高度
- $new_s = imagecreatetruecolor($width, $height);
- for ($x = 0; $x < $width; $x++)
- {
- /*此处就是一个一个像素的复制*/
- imagecopy($new_s, $img, $width-$x-1, 0, $x, 0, 1, $height);
- }
- imagejpeg($new_s, $newfile);
- imagedestroy($img);
- imagedestroy($new_s);
- }
- turn_Y('./girl.jpg', './dealed/girl_Y.jpg');
- ?>
效果如下图片:
沿X轴:
- <?php
- //沿X轴翻转
- function turn_X($src, $newfile)
- {
- $img = imagecreatefromjpeg($src);
- $width = imagesx($img);
- $height = imagesy($img);
- $new_s = imagecreatetruecolor($width, $height);
- for ($y = 0; $y < $height; $y++)
- {
- imagecopy($new_s, $img, 0, $height-$y-1, 0, $y, $width, 1);
- }
- imagejpeg($new_s, $newfile);
- imagedestroy($img);
- imagedestroy($new_s);
- }
- turn_X('./girl.jpg', './dealed/girl_X.jpg');
- ?>
效果如图:
六、图像特效
bool imagefilter (resource$src_im
, int$filtertype
[, int$arg1
[,int$arg2
[,int$arg3
]]] )
访问更多特效,请看http://www.php.net/manual/zh/function.imagefilter.php
- <?php
- $img = imagecreatefromjpeg('./cat.jpg');
- //imagefilter($img, IMG_FILTER_NEGATE); 将图像中所有颜色反转
- //imagefilter($img, IMG_FILTER_GRAYSCALE); 将图像转换为灰度的
- //imagefilter($img, IMG_FILTER_EDGEDETECT); 用边缘检测来突出图像的边缘
- //imagefilter($img,IMG_FILTER_MEAN_REMOVAL); 用平均移除法来达到轮廓效果
- imagefilter($img, IMG_FILTER_SMOOTH, 200); //使图像更柔滑。用 arg1 设定柔滑级别
- imagepng($img, './dealed/cat1.png');
- imagedestroy($img);
- ?>
最后再来看几个比较有意思的函数:
- <?php
- /**
- *imagecolorallocate()
- *imagecolortransparent()
- *imagecolorat()
- *imagecolorexact()
- **/
- $img = imagecreatetruecolor(500, 500);
- $hotpink = imagecolorallocate($img, 255, 105, 180); //为一幅图像分配颜色
- echo $hotpink, '<br />';
- imagefill($img, 0, 0, $hotpink);
- $index_tran = imagecolortransparent($img, $hotpink);//将某个颜色定义为透明色
- echo $index_tran, '<br />';
- $index_at = imagecolorat($img, 200, 200); //取得具体位置像素的颜色索引值
- echo $index_tran, '<br />';
- $index_exact = imagecolorexact($img, 255, 105, 180); //取得指定颜色的索引值
- echo $index_exact;
- imagepng($img, './test.png');
- imagedestroy($img);
- ?>
16738740
16738740
16738740
16738740
可以看出它们的相似点了吧?