PHP學習筆記10——GD圖片處理

  1 <?php
  2     //1. 圖片背景管理
  3         /* imagecreatefromjpeg($filename)    從jpeg文件或者url新建一圖像
  4          * imagecreatefrompng($filename)    從png文件或者url新建一圖象
  5          * imagecreatefromgif($filename)    從gif文件或者url新建一圖像
  6          * 用完以後要用imagedestroy()函數進行銷燬    
  7          * getimagesize($filename)            返回數組,0寬1高,2類型(1GIF,2JPG,3PNG,4SWF..),3是可用於img標記的文本字符串(height="x" width="y")
  8          */
  9         //該方法用於自動識別類型並打開圖片,使用傳入的func函數操做圖片,vars是參數列表
 10         //若是返回的是否是原image,要在func中摧毀image
 11     function imagego($filename, $func, $vars){
 12         list($width, $height, $type, $attr) = getimagesize($filename);
 13         $types = array(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 <html>
122     <body>
123         原圖<br/>
124         <img alt="" src="img/1.jpg" ><img alt="" src="img/2.gif"><img alt="" src="img/3.png">
125         <br/>圖中添加字符串<br/>
126         <img alt="" src="img/1_imageaddstring.jpg" ><img alt="" src="img/2_imageaddstring.gif"><img alt="" src="img/3_imageaddstring.png">
127         <br/>圖片縮放<br/>
128         <img alt="" src="img/1_imagethumb.jpg" ><img alt="" src="img/2_imagethumb.gif"><img alt="" src="img/3_imagethumb.png">
129         <br/>圖片裁剪<br/>
130         <img alt="" src="img/1_imagecut.jpg" ><img alt="" src="img/2_imagecut.gif"><img alt="" src="img/3_imagecut.png">
131         <br/>圖片水印<br/>
132         <img alt="" src="img/1_watermark.jpg" ><img alt="" src="img/2_watermark.gif"><img alt="" src="img/3_watermark.png">
133         <br/>圖片旋轉和翻轉<br/>
134         <img alt="" src="img/1_rotateandturn.jpg" ><img alt="" src="img/2_rotateandturn.gif"><img alt="" src="img/3_rotateandturn.png">
135         
136     </body>
137 </html>


執行結果php

相關文章
相關標籤/搜索