这段时间好浮躁,不知道该从哪下手了。想学这个又觉得现在最缺那个,学了这个现在要用到那个。不知道走左边还是右边了,虽然都能到目的地。
这是图片处理的笔记。只是个人笔记,没有细细研究,并且有个小错误。
1 php 2 header(content-type:text/html; charset=utf-8); 3 error_reporting(e_error | e_warning | e_parse); 4 function echohtml($title, $info = '', $content = array(), $tip = '') 5 { 6 $html = ''; 7 $html .= ''.$title.''; 8 if($info) 9 { 10 $html .= {$info}
; 11 } 12 if(is_array($content) && !empty($content)) 13 { 14 foreach ($content as $key => $row) 15 { 16 $i = $key+1; 17 $html .= \t{$i}:\t{$row}
; 18 } 19 } 20 if($tip) 21 { 22 $html .= \ttip:{$tip}
; 23 } 24 $html .=
; 25 echo $html; 26 } 27 /** 28 * 生成缩略图 29 * @param $src_img 操作图片文件名 30 * @param $new_img 新的图片文件名 31 * @param $n_w 宽度 32 * @param $n_h 高度 33 * @return resouce 34 * @author cntnn11 35 * @date 2013-03-10 36 */ 37 function thumb($src_img, $new_img, $n_w = 0, $n_h = 0) 38 { 39 if(is_file($src_img)) 40 { 41 list($s_w, $w_h, $s_t) = getimagesize($src_img); 42 43 //对心图片的宽高进行等比缩放限制,使用固定的公式 44 //缩放以原始图片最大的边做为新图片最大的尺寸,另一边则根据以下公式进行等比缩放 45 //如果原图的宽度小于高度,那么重新计算新图的宽度,否则重新计算新图的高度 46 if($n_w && ($s_w $s_h)) 47 { 48 $n_w = ($n_h / $s_h) * $s_h; 49 } 50 else 51 { 52 $n_h = ($n_w / $s_w) * $s_w; 53 } 54 55 //开始生成 56 $res_img_new = imagecreatetruecolor($n_w, $n_h); 57 switch($s_t) 58 { 59 case 1: 60 $res_img = imagecreatefromgif($src_img); 61 $ext = 'gif'; 62 break; 63 case 2: 64 $res_img = imagecreatefromjpeg($src_img); 65 $ext = 'jpg'; 66 break; 67 case 3: 68 $res_img = imagecreatefrompng($src_img); 69 $ext = 'png'; 70 break; 71 default: 72 echo '不支持的图片类型'; 73 return false; 74 break; 75 } 76 77 imagecopyresampled($res_img_new, $res_img, 0, 0, 0, 0, $n_w, $n_h, $s_w, $s_h); 78 //$res_img_new = imagecreate($n_w, $n_h); 79 //imagecopyresized($res_img_new, $res_img, 0, 0, 0, 0, $n_w, $n_h, $s_w, $s_h); 80 global $img_new_dir; 81 $new_img_file = $img_new_dir.$new_img.'.'.$ext; 82 imagejpeg($res_img_new, $new_img_file); 83 //genimage($res_img_new, $new_img_file, $s_t); 84 imagedestroy($res_img); 85 imagedestroy($res_img_new); 86 echo '缩放后的图片:'; 87 echo '%24new_img_file.' alt=生成的缩略图>
'; 88 } 89 } 90 /** 91 * 接收传入参数生成一张图片 92 * @param $img_res 处理好的图片资源 93 * @param $img_file 新的图片地址 94 * @param $img_type 图片类型 95 * @return resouce 96 * @author cntnn11 97 * @date 2013-03-10 98 */ 99 function genimage($img_res, $img_file, $img_type = 2)100 {101 switch($img_type)102 {103 case 1:104 return imagegif($img_res, $img_file);105 break;106 case 2:107 return imagejpeg($img_res, $img_file);108 break;109 case 3:110 return imagepng($img_res, $img_file);111 break;112 default:113 return '不支持的图片类型';114 break;115 }116 }117 118 /*==============================================================================================================================*/119 global $img_name,$img_new_dir;120 $img_name = 'testimg.jpg';121 $img_new_dir = 'genimages/';122 123 echo 原始测试图片:‘{$img_name}’
;124 echo '%24img_name.' alt=测试图片 width=200px>';125 126 127 //获取图片资源,在此使用jpg格式做测试图片128 $img_sour = imagecreatefromjpeg('testimg.jpg');129 130 //图片宽高的获取131 $title = 图片宽高度获取;132 $info = 涉及使用函数imagesx(img source) imagesy(img source);133 $content= array(134 'imagesx(image):传入一个通过imagecreate_()函数创建的图片资源类型做为参数,返回宽度',135 'imagesy(image):同imagesx,返回高度!',136 '测试图片的原始宽度:'.imagesx($img_sour).'px 原始高度:'.imagesy($img_sour).'px',137 );138 $tip = 这两个函数的参数必须是图片资源类型!!;139 echohtml($title, $info, $content, $tip);140 141 //另一种获取图片属性的方法142 $title = 使用getimagesize(图片名);获取图片的属性;143 $info = 该函数返回一个数组,包含了宽度、高度、图片类型信息!只需接收一个文件名即可获取!;144 $content= array(145 '使用方式:getimagesize('.$img_name.')',146 '返回一个数组:下标0表示宽度,下标1表示高度,下标2表示图片类型',147 ' 图片类型说明: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',148 '下标从3开始的则为文本类型,可以说是对0、1、2三个下标值的说明',149 );150 echohtml($title, $info, $content, $tip);151 152 //图片缩放函数153 $title = 图片缩放,使用效果更好的imagecopyresampled()函数;154 $info = 主要学习等比缩放,因为不对宽高进行约束,那么新生成的图片会变形;155 $content= array(156 'imagecopyresampled(dst_image, src_image, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)',157 '参数说明:',158 ' dst_image【新图片】 图片资源类型',159 ' src_image【要缩略的原始图片】 图片资源类型',160 ' dst_x,dst_y,dst_w,dst_h【缩略图片的x轴起点,y轴起点,宽度和高度】 数值类型',161 ' src_x,src_y,src_w,src_h【原始图片的x轴起点,y轴起点,宽度和高度】 数值类型',162 );163 $tip = 还有一个imagecopyresized()的函数,参数同imagecopyresampled()函数一致,但效果没有他好,不知道为什么?;164 echohtml($title, $info, $content, $tip);165 166 //测试缩略方法167 $title = '写一个thumb()方法,用来等比缩略一张图片';168 $info = '使用imagecopyresampled()函数来使用';169 $content= array(170 '首先确定参数:原始图片,缩略图片目标位置,缩略图片的宽度和高度',171 '获取图片的属性,宽高、类型,以创建相应的图片资源',172 '使用固定公式算出新图片等比缩放的宽高',173 '根据图片类型生成新的缩略图片',174 '释放图片资源',175 );176 $tip = 生成了黑色的图片,需要解决!!!!!!!!!!!;177 echohtml($titl, $info, $content, $tip);178 thumb($img_name, 'suolve1', 500, 500);179 180 //图片透明181 /*imagecolortransparent()182 imagecolorstotal()183 imagecolorsforindex()184 imagecolorallocate()185 imagefill()*/186 187 imagedestroy($img_sour);
