php图片处理代码分享,包括缩放、剪裁、缩放、翻转、旋转、透明、锐化等。
一、创建图片资源
imagecreatetruecolor(width,height);
imagecreatefromgif(图片名称);
imagecreatefrompng(图片名称);
imagecreatefromjpeg(图片名称);画出各种图像 imagegif(图片资源,保存路径);
imagepng()
imagejpeg();
二、获取图片属性
imagesx(res//宽度
imagesy(res//高度
getimagesize(文件路径)
返回一个具有四个单元的数组。索引 0 包含图像宽度的像素值,索引 1 包含图像高度的像素值。索引 2 是图像类型的标记:1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM。这些标记与 PHP 4.3.0 新加的 IMAGETYPE 常量对应。索引 3 是文本字符串,内容为“height="yyy" width="xxx"”,可直接用于 IMG 标记。
销毁图像资源
imagedestroy(图片资源);
三、透明处理
PNG、jpeg透明色都正常,只有gif不正常
imagecolortransparent(resource image [,int color])//将某个颜色设置成透明色
imagecolorstotal()
imagecolorforindex();
四、图片的裁剪
imagecopyresized()
imagecopyresampled();
五、加水印(文字、图片)
字符串编码转换string iconv ( string $in_charset , string $out_charset , string $str )
六、图片旋转
imagerotate();//制定角度的图片翻转
七、图片的翻转
沿X轴 沿Y轴翻转
八、锐化
imagecolorsforindex()
imagecolorat()
在图片上画图形 $img=imagecreatefromgif("./images/map.gif");
1 <?PHP 2 /** 3 * 图片锐化处理 4 * edit by www.jbxue.com 5 */ 6 $red= imagecolorallocate($img, 255, 0, 0); 7 8 imageline($img, 0, 0, 100, 100, $red); 9 imageellipse($img, 200, 100, 100, 100, $red); 10 imagegif($img, "./images/map2.gif"); 11 imagedestroy($img); 12 图片普通缩放 13 复制代码 代码如下: 14 15 $filename="./images/hee.jpg"; 16 $per=0.3; 17 list($width, $height)=getimagesize($filename); 18 $n_w=$width*$per; 19 $n_h=$width*$per; 20 $new=imagecreatetruecolor($n_w, $n_h); 21 $img=imagecreatefromjpeg($filename); 22 //拷贝部分图像并调整 23 imagecopyresized($new, $img,0, 0,0, 0,$n_w, $n_h, $width, $height); 24 //图像输出新图片、另存为 25 imagejpeg($new, "./images/hee2.jpg"); 26 imagedestroy($new); 27 imagedestroy($img); 28 29 图片等比例缩放、没处理透明色 30 复制代码 代码如下: 31 32 function thumn($background, $width, $height, $newfile) { 33 list($s_w, $s_h)=getimagesize($background);//获取原图片高度、宽度 34 if ($width && ($s_w < $s_h)) { 35 $width = ($height / $s_h) * $s_w; 36 } else { 37 $height = ($width / $s_w) * $s_h; 38 } 39 $new=imagecreatetruecolor($width, $height); 40 $img=imagecreatefromjpeg($background); 41 imagecopyresampled($new, $img, 0, 0, 0, 0, $width, $height, $s_w, $s_h); 42 imagejpeg($new, $newfile); 43 imagedestroy($new); 44 imagedestroy($img); 45 } 46 thumn("images/hee.jpg", 200, 200, "./images/hee3.jpg"); 47 48 gif透明色处理 49 复制代码 代码如下: 50 51 function thumn($background, $width, $height, $newfile) { 52 list($s_w, $s_h)=getimagesize($background); 53 if ($width && ($s_w < $s_h)) { 54 $width = ($height / $s_h) * $s_w; 55 } else { 56 $height = ($width / $s_w) * $s_h; 57 } 58 $new=imagecreatetruecolor($width, $height); 59 $img=imagecreatefromgif($background); 60 $otsc=imagecolortransparent($img); 61 if($otsc >=0 && $otst < imagecolorstotal($img)){//判断索引色 62 $tran=imagecolorsforindex($img, $otsc);//索引颜色值 63 $newt=imagecolorallocate($new, $tran["red"], $tran["green"], $tran["blue"]); 64 imagefill($new, 0, 0, $newt); 65 imagecolortransparent($new, $newt); 66 } 67 imagecopyresized($new, $img, 0, 0, 0, 0, $width, $height, $s_w, $s_h); 68 imagegif($new, $newfile); 69 imagedestroy($new); 70 imagedestroy($img); 71 } 72 thumn("images/map.gif", 200, 200, "./images/map3.gif");
图片裁剪
<?php /** * 图片裁剪处理 * edit by www.jbxue.com */ function cut($background, $cut_x, $cut_y, $cut_width, $cut_height, $location){ $back=imagecreatefromjpeg($background); $new=imagecreatetruecolor($cut_width, $cut_height); imagecopyresampled($new, $back, 0, 0, $cut_x, $cut_y, $cut_width, $cut_height,$cut_width,$cut_height); imagejpeg($new, $location); imagedestroy($new); imagedestroy($back); } cut("./images/hee.jpg", 440, 140, 117, 112, "./images/hee5.jpg"); ?>
图片加水印 文字水印
1 <?PHP 2 /** 3 * 4 * 图片添加文字水印 5 */ 6 7 function mark_text($background, $text, $x, $y){ 8 $back=imagecreatefromjpeg($background); 9 $color=imagecolorallocate($back, 0, 255, 0); 10 imagettftext($back, 20, 0, $x, $y, $color, "simkai.ttf", $text); 11 imagejpeg($back, "./images/hee7.jpg"); 12 imagedestroy($back); 13 } 14 mark_text("./images/hee.jpg", "细说PHP", 150, 250); 15 //图片水印 16 function mark_pic($background, $waterpic, $x, $y){ 17 $back=imagecreatefromjpeg($background); 18 $water=imagecreatefromgif($waterpic); 19 $w_w=imagesx($water); 20 $w_h=imagesy($water); 21 imagecopy($back, $water, $x, $y, 0, 0, $w_w, $w_h); 22 imagejpeg($back,"./images/hee8.jpg"); 23 imagedestroy($back); 24 imagedestroy($water); 25 } 26 mark_pic("./images/hee.jpg", "./images/gaolf.gif", 50, 200);
图片旋转
1 <?PHP 2 /** 3 * 图片旋转 4 * edit by www.jbxue.com 5 */ 6 $back=imagecreatefromjpeg("./images/hee.jpg"); 7 8 $new=imagerotate($back, 45, 0); 9 imagejpeg($new, "./images/hee9.jpg"); 10 ?>
图片水平翻转垂直翻转
1 <?php 2 /** 3 * 图片水平翻转 垂直翻转 4 * edit by www.jbxue.com 5 */ 6 function turn_y($background, $newfile){ 7 8 $back=imagecreatefromjpeg($background); 9 $width=imagesx($back); 10 $height=imagesy($back); 11 $new=imagecreatetruecolor($width, $height); 12 for($x=0; $x < $width; $x++){ 13 imagecopy($new, $back, $width-$x-1, 0, $x, 0, 1, $height); 14 } 15 imagejpeg($new, $newfile); 16 imagedestroy($back); 17 imagedestroy($new); 18 } 19 function turn_x($background, $newfile){ 20 $back=imagecreatefromjpeg($background); 21 $width=imagesx($back); 22 $height=imagesy($back); 23 $new=imagecreatetruecolor($width, $height); 24 for($y=0; $y < $height; $y++){ 25 imagecopy($new, $back,0, $height-$y-1, 0, $y, $width, 1); 26 } 27 imagejpeg($new, $newfile); 28 imagedestroy($back); 29 imagedestroy($new); 30 } 31 turn_y("./images/hee.jpg", "./images/hee11.jpg"); 32 turn_x("./images/hee.jpg", "./images/hee12.jpg"); 33 ?>