1 "gif", 2=>"jpeg", 3=>"png"); 14 //组合创建图像函数名 15 $createfrom = "imagecreatefrom".$types{ $type}; 16 $image = $createfrom($filename); 17 //对该图片进行操作 18 $image = $func($image, $width, $height, $type, $vars); 19 //组合输出图像函数名 20 $output = "image".$types{ $type}; 21 $filename_s = preg_split("/\\./", $filename); 22 $filename = $filename_s{0}."_${func}.".$filename_s{1}; 23 $output($image, $filename); 24 25 imagedestroy($image); 26 } 27 //该方法用于在图片上加上字符串,传入一个参数表示添加的字符串 28 function imageaddstring($image, $width, $height, $type, $vars){ 29 $string = $vars{0}; 30 $sx = ($width - imagefontwidth(5)*strlen($string))/2; 31 $sy = ($height - imagefontheight(5))/2; 32 $textcolor = imagecolorallocate($image, 255, 0, 0); 33 imagestring($image, 5, $sx, $sy, $string, $textcolor); 34 return $image; 35 } 36 imagego("img/1.jpg", "imageaddstring", array("Add a string on JPEG")); 37 imagego("img/2.gif", "imageaddstring", array("Add a string on GIF")); 38 imagego("img/3.png", "imageaddstring", array("Add a string on PNG")); 39 40 //2. 图片缩放与裁剪处理 41 /* imagecopyresized 42 * imagecopyresampled($dst_img.$src_img,$dst_x,$dst_y,$src_x,$src_y,$dst_w,$dst_h,$src_w,$src_h) 43 * 以上两个函数都可以进行图片缩放,后者效果好一些.该函数可以在图像内部复制,但当区域重复时后果不可知 44 * 如果源和目标的高宽不一样,则会自动进行缩放. 45 * 同时,该函数也可以实现图像的裁剪. 46 */ 47 //该方法用于缩小图片1.5倍 48 function imagethumb($image, $width, $height, $type, $vars){ 49 $p = $vars{0}; 50 $n_image = imagecreatetruecolor($width/$p, $height/$p); 51 imagecopyresampled($n_image, $image, 0, 0, 0, 0, $width/$p, $height/$p, $width, $height); 52 imagedestroy($image); 53 return $n_image; 54 } 55 imagego("img/1.jpg", "imagethumb", array(1.5)); 56 imagego("img/2.gif", "imagethumb", array(1.5)); 57 imagego("img/3.png", "imagethumb", array(1.5)); 58 //该方法用于实现图片的裁剪 59 function imagecut($image, $width, $height, $type, $vars) { 60 $n_x = $vars{0}; 61 $n_y = $vars{1}; 62 $n_width = $vars{2}; 63 $n_height = $vars{3}; 64 65 $n_image = imagecreatetruecolor($n_width, $n_height); 66 imagecopyresampled($n_image, $image, 0, 0, $n_x, $n_y, $n_width, $n_height, $n_width, $n_height); 67 imagedestroy($image); 68 return $n_image; 69 } 70 imagego("img/1.jpg", "imagecut", array(200, 100, 100, 100)); 71 imagego("img/2.gif", "imagecut", array(200, 100, 100, 100)); 72 imagego("img/3.png", "imagecut", array(200, 100, 100, 100)); 73 74 //3. 添加图片水印 75 /* imagecopy($dst_img,$src_img,$dst_x,$dst_y,$src_x,$src_y,$src_w,$src_h) 76 * 将一个图片复制到另一个图片上面 77 */ 78 function watermark($image, $width, $height, $type, $vars){ 79 $w_image = imagecreatefrompng("img/logo.png"); 80 list($src_w, $src_h) = getimagesize("img/logo.png"); 81 imagecopy($image, $w_image, $vars{0}, $vars{1}, 0, 0, $src_w, $src_h); 82 imagedestroy($w_image); 83 return $image; 84 } 85 imagego("img/1.jpg", "watermark", array(50,50)); 86 imagego("img/2.gif", "watermark", array(100,100)); 87 imagego("img/3.png", "watermark", array(150,150)); 88 89 //4. 图片的选装和翻转 90 /* imagerotate($src,$degree,$bgcolor,[ignore transparent]) 可选参数是否忽视透明色,返回旋转后的图片 91 * 翻转用imagecopy按像素行或者像素列复制就可以了 92 * 下面的函数先旋转再翻转 93 */ 94 function rotateandturn($image, $width, $height, $type, $vars) { 95 $angle = $vars{0}; 96 $image = imagerotate($image, $angle, 0); 97 //选装后图片大小可能发生变化 98 $width = imagesx($image); 99 $height = imagesy($image);100 $n_image = imagecreatetruecolor($width, $height);101 //1代表x轴翻转,2代表y轴翻转,0代表不翻转102 if ($vars{1} == 1) {103 for ($x = 0; $x < $width; $x++) {104 imagecopy($n_image, $image, $x, 0, $width-$x-1, 0, 1, $height);105 }106 } else if ($vars{1} == 2){107 for ($x = 0; $x < $height; $x++) {108 imagecopy($n_image, $image, 0, $x, 0, $height-$x-1, $width, 1);109 } 110 } else {111 imagecopy($n_image, $image, 0, 0, 0, 0, $width, $height);112 }113 114 imagedestroy($image);115 return $n_image;116 }117 imagego("img/1.jpg", "rotateandturn", array(10, 0));118 imagego("img/2.gif", "rotateandturn", array(0,1));119 imagego("img/3.png", "rotateandturn", array(10,2));120 ?>121 122 123 原图124 125 图中添加字符串126 127 图片缩放128 129 图片裁剪130 131 图片水印132 133 图片旋转和翻转134 135 136 137