PHP系列(十)GD庫

GDphp

1Phpgd庫的使用算法

Gd庫是一個畫圖或處理有圖片的函數庫瀏覽器

 

2、使用gd庫畫圖服務器

GD庫圖像繪製的步驟微信

PHP中建立一個圖像應該完成以下所示的4個步驟:session

1.建立一個背景圖像(也叫畫布),之後的操做都基於此背景圖像。dom

2.在背景上繪製圖像輪廓或輸入文本。ide

3.輸出最終圖形函數

4.釋放資源post

代碼:

<?php

//1. 建立畫布

$im = p_w_picpathCreateTrueColor(200, 200); //創建空白畫布背景

$white = p_w_picpathColorAllocate($im, 255, 255, 255); //設置畫布顏色

$blue = p_w_picpathColorAllocate ($im, 0, 0, 64);

//2. 開始繪畫

p_w_picpathFill($im, 0, 0, $blue); //繪製背景

p_w_picpathLine($im, 0, 0, 200, 200, $white); //畫線

p_w_picpathString($im, 4, 50, 150, 'Sales', $white); //添加字串

//3. 輸出圖像

header('Content-type: p_w_picpath/png');

p_w_picpathPng ($im); //PNG 格式將圖像輸出

//4. 釋放資源

p_w_picpathDestroy($im);

 

 

畫布管理

p_w_picpathcreate -- 新建一個基於調色板的圖像

resource p_w_picpathcreate ( int x_size, int y_size )

本函數用來創建空新畫布,參數爲圖片大小,單位爲像素 (pixel)。支持256色。

p_w_picpathcreatetruecolor -- 新建一個真彩×××像

resource p_w_picpathcreatetruecolor ( int x_size, int y_size )

新建一個真彩×××像畫布 ,須要 GD 2.0.1 或更高版本,不能用於 GIF 文件格式。

p_w_picpathdestroy -- 銷燬一圖像

bool p_w_picpathdestroy ( resource p_w_picpath )

p_w_picpathdestroy() 釋放與 p_w_picpath 關聯的內存

 

設置顏色

p_w_picpathcolorallocate -- 爲一幅圖像分配顏色

語法:int p_w_picpathcolorallocate ( resourcep_w_picpath, int red,int green, int blue )

p_w_picpathcolorallocate() 返回一個標識符,表明了由給定的 RGB

分組成的顏色。redgreen blue 分別是所須要的顏色的紅,

綠,藍成分。這些參數是 0 255 的整數或者十六進制的 0x00

0xFFp_w_picpathcolorallocate() 必須被調用以建立每一種用在

p_w_picpath 所表明的圖像中的顏色。

$im =p_w_picpathcreatetruecolor(100, 100); //建立畫布的大小爲100x100

$red =p_w_picpathcolorallocate($im,255,0,0); //由十進制整數設置一個顏色

$white =p_w_picpathcolorallocate($im, 0xFF, 0xFF, 0xFF);// 十六進制方式

 

生成圖片

p_w_picpathgif -- GIF 格式將圖像輸出到瀏覽器或文件

語法:bool p_w_picpathgif (resource p_w_picpath[,string filename] )

p_w_picpathjpeg -- JPEG 格式將圖像輸出到瀏覽器或文件

語法:bool p_w_picpathjpeg (resource p_w_picpath [,stringfilename [, int quality]])

p_w_picpathpng -- PNG 格式將圖像輸出到瀏覽器或文件

語法:bool p_w_picpathpng (resource p_w_picpath[,string filename] )

p_w_picpathwbmp -- WBMP 格式將圖像輸出到瀏覽器或文件

語法:bool p_w_picpathwbmp (resource p_w_picpath [,string filename [, intforeground]] )

 

3、畫各類圖形

p_w_picpathfill -- 區域填充

語法:bool p_w_picpathfill(resource p_w_picpath,intx,int y, int color)

p_w_picpathfill() p_w_picpath 圖像的座標 xy(圖像左上角爲 0, 0)處用color 顏色執行區域填充(即與 x, y 點顏色相同且相鄰的點都會被填充)。

p_w_picpathsetpixel -- 畫一個單一像素

語法:bool p_w_picpathsetpixel ( resourcep_w_picpath, int x, int y, int color )

p_w_picpathsetpixel() p_w_picpath 圖像中用 color 顏色在 xy 座標(圖像左上角爲 00)上畫一個點。

p_w_picpathline -- 畫一條線段

語法:bool p_w_picpathline ( resource p_w_picpath,int x1, int y1, int x2, inty2, int color )

p_w_picpathline() color 顏色在圖像 p_w_picpath 中從座標 x1y1 x2y2(圖像左上角爲 0, 0)畫一條線段。

p_w_picpathrectangle -- 畫一個矩形

語法:bool p_w_picpathrectangle ( resourcep_w_picpath, int x1, int y1,int x2, int y2, int col )

p_w_picpathrectangle() col 顏色在 p_w_picpath 圖像中畫一個矩形,其左上角座標爲 x1, y1,右下角座標爲 x2, y2。圖像的左上角座標爲 0, 0

p_w_picpathfilledrectangle -- 畫一矩形並填充

語法:bool p_w_picpathfilledrectangle (resource p_w_picpath, int x1, inty1, int x2, int y2, int color )

p_w_picpathfilledrectangle() p_w_picpath 圖像中畫一個用 color 顏色填充了的矩形,其左上角座標爲 x1y1,右下角座標爲 x2y20, 0 是圖像的最左上角。

p_w_picpathellipse -- 畫一個橢圓

語法:bool p_w_picpathellipse ( resourcep_w_picpath, int cx, int cy, intw, int h, int color )

p_w_picpathellipse() p_w_picpath 所表明的圖像中畫一箇中心爲 cxcy(圖像左上角爲 0, 0)的橢圓。w h 分別指定了橢圓的寬度和高度,橢圓的顏色由 color 指定。

p_w_picpathfilledellipse -- 畫一橢圓並填充

語法:bool p_w_picpathfilledellipse ( resourcep_w_picpath, int cx, intcy, int w, int h, int color )

p_w_picpathfilledellipse() p_w_picpath 所表明的圖像中以 cxcy(圖像左上角爲 0, 0)爲中心畫一個橢圓。w h 分別指定了橢圓的寬和高。橢圓用 color 顏色填充。若是成功則返回

TRUE,失敗則返回 FALSE

p_w_picpatharc -- 畫橢圓弧

bool p_w_picpatharc ( resource p_w_picpath, int cx, int cy, int w, int h, int s,inte, int color )

p_w_picpatharc() cxcy(圖像左上角爲 0, 0)爲中心在 p_w_picpath 所表明的圖像中畫一個橢圓弧。w h 分別指定了橢圓的寬度和高度,起始和結束點以 s e 參數以角度指定。0°位於三點鐘位置,以順時針方向繪畫。

p_w_picpathfilledarc -- 畫一橢圓弧且填充

bool p_w_picpathfilledarc ( resource p_w_picpath, int cx, int cy, int w, int h,ints, int e, int color, int style )

p_w_picpathfilledarc() p_w_picpath 所表明的圖像中以 cxcy(圖像左上角爲 0, 0)畫一橢圓弧。若是成功則返回 TRUE,失敗則返回 FALSEw h 分別指定了橢圓的寬和高,s e 參數以角度指定了起始和結束點。style 能夠是下列值按位或(OR)後的值:

IMG_ARC_PIE IMG_ARC_CHORD

IMG_ARC_NOFILL IMG_ARC_EDGED

p_w_picpathstring -- 水平地畫一行字符串

語法:bool p_w_picpathstring ( resource p_w_picpath,int font, int x, int y,string s, int col )

p_w_picpathstring() col 顏色將字符串 s 畫到 p_w_picpath 所表明的圖像的 xy 座標處(這是字符串左上角座標,整幅圖像的左上角爲 00)。若是 font 1234 5,則使用內置字體。

p_w_picpathstringup -- 垂直地畫一行字符串

語法:bool p_w_picpathstringup ( resourcep_w_picpath, int font, int x, inty, string s, int col )

p_w_picpathstring() col 顏色將字符串 s 垂直地畫到 p_w_picpath 所表明的圖像的 x , y 座標處(圖像的左上角爲 0, 0)。若是 font 1234 5,則使用內置字體。

p_w_picpathchar -- 水平地畫一個字符

語法:bool p_w_picpathchar ( resource p_w_picpath,int font, int x, int y, string c,int color )

p_w_picpathchar() 將字符串 c 的第一個字符畫在 p_w_picpath 指定的圖像中,其左上角位於 xy(圖像左上角爲 0, 0),顏色爲 color。若是 font 1234 5,則使用內置的字體(更大的數字對應於更大的字體)。

p_w_picpathcharup -- 垂直地畫一個字符

語法:bool p_w_picpathcharup ( resource p_w_picpath,int font, int x, int y, stringc, int color )

p_w_picpathcharup() 將字符 c 垂直地畫在 p_w_picpath 指定的圖像上,位於 xy(圖像左上角爲 0, 0),顏色爲 color。若是 font 1234 5,則使用內置的字體。

p_w_picpathttftext -- TrueType 字體向圖像寫入文本

語法 array p_w_picpathttftext ( resource p_w_picpath, floatsize, float angle, intx, int y, int color, string fontfile, string text )

代碼:

         //1建立資源(畫布的大小)

         $img = p_w_picpathcreatetruecolor(200, 200);        

         //設置畫布的顏色

         $white =  p_w_picpathcolorallocate($img, 0xFF, 0xFF, 0xFF);

         $red = p_w_picpathcolorallocate($img, 255, 0, 0);

         $blue = p_w_picpathcolorallocate($img, 0, 0, 0XFF);

         $pink = p_w_picpathcolorallocate($img, 0XFF, 0, 0XFF);

         $green =  p_w_picpathcolorallocate($img, 0, 0xFF, 0);

        

         p_w_picpathfill($img, 0, 0, $white);//填充畫布顏色

         2、畫各類圖形    

         //畫線

         p_w_picpathline($img, 0,0, 200,200, $blue);

         p_w_picpathline($img, 200, 0, 0, 200, $red);

 

         //畫矩形

         p_w_picpathrectangle($img, 50, 50, 150, 150,$pink);

         p_w_picpathfilledrectangle($img, 75,75,125,125, $blue);

        

         //畫圓

         p_w_picpathellipse($img, 50, 50, 100, 100,$red);

         p_w_picpathfilledellipse($img, 150, 150, 100,100, $red);

 

         //畫弧形

         p_w_picpatharc($img, 150, 50, 100, 100,  -90, 0, $blue);

 

         //畫一個字符串

         p_w_picpathstring($img, 5, 50, 150,"hello world", $blue);

         p_w_picpathstringup($img, 5, 50, 150,"hello world", $blue);

 

         //3. 畫出各類圖形,和寫(畫出)

         p_w_picpathttftext($img, 30, 0, 10, 100,$green, "./simkai.ttf", "妹子漂亮嗎?");

         p_w_picpathttftext($img, 30, 0, 13, 103,$red, "./simkai.ttf", "妹子漂亮嗎?");

 

         //4保存,或輸出給瀏覽, 寫第二個參數就是保存

         header("Content-Type:p_w_picpaths/gif");

 

         p_w_picpathgif($img);

 

         //5. 釋放資源

         p_w_picpathdestroy($img);

 

4、時鐘代碼:

//獲取系統時間

         date_default_timezone_set("PRC");

 

         $h = date("H");

         $i = date("i");

         $s = date("s");

 

         //1 建立資源(畫布的大小)

         $img = p_w_picpathcreatetruecolor(200, 250);        

         //設置畫布的顏色

         $white =  p_w_picpathcolorallocate($img, 0xFF, 0xFF, 0xFF);

         $red = p_w_picpathcolorallocate($img, 255, 0, 0);

         $blue = p_w_picpathcolorallocate($img, 0, 0, 0XFF);

         $pink = p_w_picpathcolorallocate($img, 0XFF, 0, 0XFF);

         $green =  p_w_picpathcolorallocate($img, 0, 0xFF, 0);

         p_w_picpathfill($img, 0, 0, $white);

         //2. 製做各類圖形

         p_w_picpathellipse($img, 100, 100, 190, 190,$blue);

         p_w_picpathfilledellipse($img, 100, 100, 4,4, $blue);

         p_w_picpathstring($img, 3, 95, 8,  "12", $blue);

         p_w_picpathstring($img, 3,180, 95,"03", $blue);

         p_w_picpathstring($img, 3, 95, 180,  "06", $blue);

         p_w_picpathstring($img, 3, 11, 95,  "09", $blue);

 

         //秒針

         $len = 80;

 

         $a = $len*sin(pi()/30*$s);

         $b = $len*cos(pi()/30*$s);

         $x = 100 + $a;

         $y = 100 - $b;

 

         p_w_picpathline($img, 100, 100, $x, $y,$red);

 

         //數字的時間

         p_w_picpathstring($img, 5, 20, 230,"NOW: {$h}:{$i}:{$s}", $red);

 

         //4保存,或輸出給瀏覽, 寫第二個參數就是保存

         header("Content-Type:p_w_picpath/gif");

         p_w_picpathgif($img);

 

         //5. 釋放資源

         p_w_picpathdestroy($img);

 

5、驗證碼類

Reg.php

<?php

         session_start();

if(isset($_POST['dosubmit'])){

         if(strtoupper($_SESSION['code']) ==strtoupper($_POST['code']) ) {

                   echo "輸入成功!<br>";

         }else{

                   echo "輸入不對!<br>";

         }

}

?>

 

<body>

         <form action="reg.php"method="post">

                   username: <input type="text"name="username"> <br>

                   password: <inputtype="password" name="password"> <br>

                   code: <inputtype="text" onkeyup="if(this.value!=this.value.toUpperCase())this.value=this.value.toUpperCase()" size="6"name="code">

                   <imgsrc="code.php" onclick="this.src='code.php?'+Math.random()"/>  <br>

                   <inputtype="submit" name="dosubmit" value=" "> <br>

                  

         </form>

</body>

Code.php

<?php

         //開啓session

         session_start();

         include "vcode.class.php";

         //構造方法

         $vcode = new Vcode(120, 30, 6);

         //將驗證碼放到服務器本身的空間保存一份

         $_SESSION['code'] =$vcode->getcode();

         //將驗證碼圖片輸出

         $vcode->outimg();

Vcode.class.php

<?php

         class Vcode {

                   private $width;    //

                   private $height;   //

                   private $num;     //數量

                   private $code;    //驗證碼

                   private $img;     //圖像的資源

 

                   //構造方法,三個參數

                   function__construct($width=80, $height=20, $num=4) {

                            $this->width =$width;

                            $this->height =$height;

                            $this->num =$num;

                            $this->code =$this->createcode(); //調用本身的方法

                   }

 

                   //獲取字符的驗證碼,用於保存在服務器中

                   function getcode() {

                            return$this->code;

                   }

 

                   //輸出圖像

                   function outimg() {

                            //建立背景 (顏色, 大小, 邊框)

                            $this->createback();

 

                            //畫字 (大小, 字體顏色)

                            $this->outstring();

 

                            //干擾元素(點, 線條)

                           

                            $this->setdisturbcolor();

                            //輸出圖像

                            $this->printimg();

                   }

 

                   //建立背景

privatefunction createback() {

//建立資源

$this->img =p_w_picpathcreatetruecolor($this->width, $this->height);

//設置隨機的背景顏色

$bgcolor =  p_w_picpathcolorallocate($this->img, rand(225,255), rand(225, 255), rand(225, 255));

//設置背景填充

p_w_picpathfill($this->img, 0, 0, $bgcolor);

//畫邊框

$bordercolor =  p_w_picpathcolorallocate($this->img, 0, 0, 0);

p_w_picpathrectangle($this->img, 0,0, $this->width-1, $this->height-1, $bordercolor);

                   }

                   //畫字

privatefunction outstring() {

         for($i=0; $i<$this->num; $i++) {

         $color=p_w_picpathcolorallocate($this->img, rand(0, 128), rand(0, 128), rand(0, 128));

                                    

         $fontsize=rand(3,5);  //字體大小

 

         $x =3+($this->width/$this->num)*$i; //水平位置

         $y = rand(0,p_w_picpathfontheight($fontsize)-3);

 

         //畫出每一個字符

                                     p_w_picpathchar($this->img,$fontsize, $x, $y, $this->code{$i}, $color);

                            }

                   }

 

                   //設置干擾元素

privatefunction setdisturbcolor() {

         //加上點數

         for($i=0; $i<100; $i++) {

         $color=p_w_picpathcolorallocate($this->img, rand(0, 255), rand(0, 255), rand(0, 255));

         p_w_picpathsetpixel($this->img, rand(1,$this->width-2), rand(1, $this->height-2), $color);

                            }

         //加線條

for($i=0;$i<10; $i++) {

         $color=p_w_picpathcolorallocate($this->img, rand(0, 255), rand(0, 128), rand(0, 255));

     p_w_picpatharc($this->img,rand(-10,$this->width+10), rand(-10, $this->height+10), rand(30, 300), rand(30,300), 55,44, $color);

                            }

                   }

                   //輸出圖像

                   private function printimg() {

                            if (p_w_picpathtypes()& IMG_GIF) {

                                  header("Content-type: p_w_picpath/gif");

                                         p_w_picpathgif($this->img);

                            } elseif(function_exists("p_w_picpathjpeg")) {

                                  header("Content-type: p_w_picpath/jpeg");

                                  p_w_picpathgif($this->img);

                            } elseif(p_w_picpathtypes() & IMG_PNG) {

                                  header("Content-type: p_w_picpath/png");

                                         p_w_picpathgif($this->img);

                            }  else {

                                     die("No p_w_picpath support in this PHPserver");

                            }

                   }

                   //生成驗證碼字符串

                   private function createcode(){

                            $codes ="3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXY";

                            $code ="";

                            for($i=0; $i <$this->num; $i++) {

                                     $code.=$codes{rand(0, strlen($codes)-1)};   

                            }

                            return $code;

                   }

                   //用於自動銷燬圖像資源

                   function __destruct() {

                            p_w_picpathdestroy($this->img);

                   }

 

         }

 

6Php圖片處理

圖片背景管理

圖片縮放和裁剪

添加圖片水印

圖片旋轉和翻轉

 

圖片背景管理

從指定的圖片文件或 URL地址來新建一個圖像。成功則返回一個圖像標識符,失敗時返回一個空字符串,而且輸出一條錯誤信息。因爲格式不一樣,則須要分別使用對應圖片背景處理函數。

–resource p_w_picpathcreatefrompng ( string filename ) PNG 文件或 URL 新建一圖像

–resource p_w_picpathcreatefromjpeg ( string filename ) JPEG 文件或 URL 新建一圖像

–resource p_w_picpathcreatefromgif ( string filename GIF 文件或 URL 新建一圖像

–resource p_w_picpathcreatefromwbmp ( string filename ) WBMP 文件或 URL 新建一圖像

 

其餘圖像處理函數:

– intp_w_picpathsx ( resource p_w_picpath ) 取得圖像寬度

– intp_w_picpathsy ( resource p_w_picpath ) 取得圖像高度

– arraygetp_w_picpathsize ( string $filename [, array &$p_w_picpathinfo ] ) 取得圖像大小、類型等信息

代碼:

<?php

         function cimgstr($imgname, $string) {

                   list($width, $height, $type)=getp_w_picpathsize($imgname);

                   $types =array(1=>"gif", 2=>"jpeg", 3=>"png");

                   //變量函數

                   $createp_w_picpath ="p_w_picpathcreatefrom".$types[$type];

                   $img =$createp_w_picpath($imgname);

                   $red =  p_w_picpathcolorallocate($img, 0xFF, 0, 0);

                   $x =($width-p_w_picpathfontwidth(5)*strlen($string))/2;

                   $y =($height-p_w_picpathfontheight(5))/2;

                   p_w_picpathstring($img, 5, $x, $y,$string, $red);

                   //變量函數

                   $save ="p_w_picpath".$types[$type];

                   $save($img,"new_".$imgname);

                   p_w_picpathdestroy($img);

         }

         cimgstr("dx.jpg","meizi");

         cimgstr("map.gif","meizi");

         cimgstr("cx.png","meize");

 

7、圖片的縮放和剪切

bool p_w_picpathcopyresampled ( resource $dst_p_w_picpath , resource$src_p_w_picpath , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int$dst_h , int $src_w , int $src_h )

重採樣拷貝部分圖像並調整大小,是將一幅圖像中的一塊正方形區域拷貝到另外一個圖像中,平滑地插入像素值,所以,尤爲是,減少了圖像的大小而仍然保持了極大的清晰度。成功時返回 TRUE 或者在失敗時返回 FALSE。其中dst_p_w_picpath src_p_w_picpath 分別是目標圖像和源圖像的標識符。

縮放代碼:

<?php

         function thumb($imgname, $width,$height) {

                   list($swidth, $sheight,$type)= getp_w_picpathsize($imgname);

 

                   $types =array(1=>"gif", 2=>"jpeg", 3=>"png");

 

                   //變量函數

                   $createp_w_picpath ="p_w_picpathcreatefrom".$types[$type];

                   //$createp_w_picpath =p_w_picpathcreatefromipeg;

                   //原圖片

                   $imgsrc =$createp_w_picpath($imgname);

                   //目標資源

                   $dimg = p_w_picpathcreatetruecolor($width,$height);

                  

                   p_w_picpathcopyresampled($dimg,$imgsrc, 0, 0, 0, 0, $width, $height, $swidth, $sheight);

 

                   //變量函數

                   $save ="p_w_picpath".$types[$type];

                   $save($dimg,"new_".$imgname);

                   p_w_picpathdestroy($imgsrc);

                   p_w_picpathdestroy($dimg);

         }

         thumb("dx.jpg", 200, 200);

         thumb("map.gif", 200, 200);

         thumb("cx.png", 200, 200);

等比縮放代碼:

<?php

         function thumb($imgname, $width,$height) {

                   list($swidth, $sheight,$type)= getp_w_picpathsize($imgname);

 

                   $types =array(1=>"gif", 2=>"jpeg", 3=>"png");

 

                   //變量函數

                   $createp_w_picpath ="p_w_picpathcreatefrom".$types[$type];

                   //原圖片

                   $imgsrc =$createp_w_picpath($imgname);

                   //目標資源

                   $dimg =p_w_picpathcreatetruecolor($width, $height);

 

                   if ($width &&($swidth < $sheight)) {

                         $width = ($height / $sheight) * $swidth;

                   } else {

                       $height= ($width / $swidth) * $sheight;

                   }

 

                   p_w_picpathcopyresampled($dimg,$imgsrc, 0, 0, 0, 0, $width, $height, $swidth, $sheight);

        

                   //變量函數

                   $save ="p_w_picpath".$types[$type];

                   $save($dimg,"new_".$imgname);

                   p_w_picpathdestroy($imgsrc);

                   p_w_picpathdestroy($dimg);

         }

 

         thumb("dx.jpg", 200, 200);

         thumb("map.gif", 200, 200);

         thumb("cx.png", 200, 200);

 

剪切代碼:

<?php

         function cut($imgname, $x, $y, $width,$height) {

                   list($swidth, $sheight,$type)= getp_w_picpathsize($imgname);

 

                   $types =array(1=>"gif", 2=>"jpeg", 3=>"png");

 

                   //變量函數

                   $createp_w_picpath ="p_w_picpathcreatefrom".$types[$type];

                   //原圖片

                   $imgsrc =$createp_w_picpath($imgname);

                   //目標資源

                   $dimg =p_w_picpathcreatetruecolor($width, $height);

 

                   p_w_picpathcopyresampled($dimg,$imgsrc, 0, 0, $x, $y, $width, $height, $width, $height);

                  

                   //變量函數

                   $save ="p_w_picpath".$types[$type];

                   $save($dimg,"new_".$imgname);

                   p_w_picpathdestroy($imgsrc);

                   p_w_picpathdestroy($dimg);

         }

 

         cut("dx.jpg",50,50, 200,200);

         cut("map.gif",50,50, 200,200);

         cut("cx.png",50,50, 200,200);

 

8、添加圖片水印

bool p_w_picpathcopy( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x ,int $src_y , int $src_w , int $src_h )

拷貝圖像的一部分(也就是圖片合成)。

src_im 圖像中座標從 src_xsrc_y 開始,寬度爲 src_w,高度爲 src_h 的一部分拷貝到 dst_im 圖像中座標爲 dst_x dst_y 的位置上。

圖片水印代碼

<?php

         function watermark($imgname,$watername) {

                   list($swidth, $sheight,$type)= getp_w_picpathsize($imgname);    //原圖

                   list($wwidth, $wheight,$wtype)= getp_w_picpathsize($watername);  //水印

 

                   $types =array(1=>"gif", 2=>"jpeg", 3=>"png");

 

                   //變量函數

                   $createp_w_picpath = "p_w_picpathcreatefrom".$types[$type];

                   $createp_w_picpathw ="p_w_picpathcreatefrom".$types[$wtype];

                   //原圖片

                   $imgsrc =$createp_w_picpath($imgname);   //原圖

 

                   $wimg =$createp_w_picpathw($watername);  //水印

 

                   $x = rand(3,$swidth-$wwidth);

                   $y = rand(3,$sheight-$wheight);

 

                   p_w_picpathcopy($imgsrc, $wimg, $x,$y, 0, 0, $wwidth, $wheight);

                   //變量函數

                   $save ="p_w_picpath".$types[$type];

                   $save($imgsrc,"new_".$imgname);

                   p_w_picpathdestroy($imgsrc);

                   p_w_picpathdestroy($wimg);

         }

         watermark("dx.jpg","php.gif");

         watermark("cx.png","php.gif");

文字水印代碼:

<?php

         function watermark($imgname, $string) {

                   list($swidth, $sheight,$type)= getp_w_picpathsize($imgname);

 

                   $types =array(1=>"gif", 2=>"jpeg", 3=>"png");

 

                   //變量函數

                   $createp_w_picpath ="p_w_picpathcreatefrom".$types[$type];

                   //原圖片

                   $imgsrc = $createp_w_picpath($imgname);

 

                   $white =p_w_picpathcolorallocate($imgsrc, 200,200, 200);

                   $green =p_w_picpathcolorallocate($imgsrc, 0,200, 0);

 

                   $x = rand(3,$swidth-strlen($string)*p_w_picpathfontwidth(5));

                   $y = rand(3,$sheight-p_w_picpathfontheight(5)-2);

 

                   p_w_picpathstring($imgsrc, 5, $x, $y,$string, $white);

                   p_w_picpathstring($imgsrc, 5, $x+1,$y+1, $string, $green);

                  

                   //變量函數

                   $save ="p_w_picpath".$types[$type];

                   $save($imgsrc,"new_".$imgname);

                   p_w_picpathdestroy($imgsrc);

         //      p_w_picpathdestroy($dimg);

         }

         watermark("dx.jpg","meizi");

         watermark("cx.png","meize");

 

9、圖片旋轉和翻轉

resource p_w_picpathrotate ( resource $src_im , float $angle , int

$bgd_color[, int $ignore_transparent ] )

用給定角度旋轉圖像

src_im 圖像用給定的angle角度旋轉。bgd_color指定了旋轉後沒有覆蓋到的部分的顏色。

旋轉的中心是圖像的中心。旋轉後的圖像會按比例縮小以適合目標圖像的大小邊緣不被剪去。

若是 ignore_transparent 被設爲非零值,則透明色會被忽略(不然會被保留)。

旋轉代碼:

<?php

         function xz($imgname, $jd) {

                   list($swidth, $sheight,$type)= getp_w_picpathsize($imgname);    //原圖

        

                   $types =array(1=>"gif", 2=>"jpeg", 3=>"png");

 

                   //變量函數

                   $createp_w_picpath ="p_w_picpathcreatefrom".$types[$type];

                   $imgsrc =  $createp_w_picpath($imgname);

        

                   //旋轉

                   $new = p_w_picpathrotate($imgsrc,$jd, 0);

 

                   //變量函數

                   $save ="p_w_picpath".$types[$type];

                   //保存

                   $save($new,"new_".$imgname);

                   p_w_picpathdestroy($imgsrc);

                   p_w_picpathdestroy($new);

         }

         xz("php.gif", 90);

 

y軸旋轉代碼:

<?php

         //y軸翻轉

         function trun_y($imgname) {

                   list($width, $height, $type)=getp_w_picpathsize($imgname);    //原圖

        

                   $types =array(1=>"gif", 2=>"jpeg", 3=>"png");

 

                   //變量函數

                   $createp_w_picpath ="p_w_picpathcreatefrom".$types[$type];

                   $imgsrc =  $createp_w_picpath($imgname);

 

                   $new =p_w_picpathcreatetruecolor($width, $height);   

                   //循環每次一個像素

                   for($x=0; $x<$width; $x++){

                            p_w_picpathcopy($new,$imgsrc, $width-$x-1, 0, $x,0, 1, $height);        

                   }

 

                   //變量函數

                   $save ="p_w_picpath".$types[$type];

                   //保存

                   $save($new, "new_".$imgname);

                   p_w_picpathdestroy($imgsrc);

                   p_w_picpathdestroy($new);

         }

         trun_y("cx.png");

x軸旋轉代碼:

<?php

         //x軸翻轉

         function trun_x($imgname) {

                   list($width, $height, $type)=getp_w_picpathsize($imgname);    //原圖

        

                   $types =array(1=>"gif", 2=>"jpeg", 3=>"png");

 

                   //變量函數

                   $createp_w_picpath ="p_w_picpathcreatefrom".$types[$type];

                   $imgsrc =  $createp_w_picpath($imgname);

 

                   $new =p_w_picpathcreatetruecolor($width, $height);   

                   //循環每次一個像素

                   for($y=0; $y<$height;$y++) {

                            p_w_picpathcopy($new,$imgsrc, 0, $height-$y-1, 0,$y, $width, 1);        

                   }

                  

                   //變量函數

                   $save ="p_w_picpath".$types[$type];

                   //保存

                   $save($new,"new_".$imgname);

                   p_w_picpathdestroy($imgsrc);

                   p_w_picpathdestroy($new);

         }

         trun_x("cx.png");

        

10、圖像處理類

使用類代碼:

<?php

         //包含這個類p_w_picpath.class.php

         include "p_w_picpath.class.php";

 

         $img = new Image("./p_w_picpaths");

/*

         echo $img->thumb("dx.jpg",1000, 1000, "th1_")."<br>";

         echo $img->thumb("dx.jpg",800, 800, "th2_")."<br>";

 */

/*

         echo$img->watermark("dx.jpg", "php.gif", 0,"wa0_")."<br>";  

         echo$img->watermark("dx.jpg", "php.gif", 1,"wa1_")."<br>";            

 */  

 

         // 122   104   104 108

         echo $img->cut("cx.png",122, 104, 104, 108);

驗證碼類:

<?php

         /**

                   file: p_w_picpath.class.php 類名爲Image

                   圖像處理類,能夠完成對各類類型的圖像進行縮放、加圖片水印和剪裁的操做。

         */

         class Image {

                   /* 圖片保存的路徑 */

                   private $path;                                  

 

                   /**

                    * 實例圖像對象時傳遞圖像的一個路徑,默認值是當前目錄

                    * @param      string        $path        能夠指定處理圖片的路徑

                    */

                   function__construct($path="./"){

                            $this->path =rtrim($path,"/")."/";

                   }

                  

                   /**

                    * 對指定的圖像進行縮放

                    * @param      string        $name      是須要處理的圖片名稱

                    * @param      int    $width               縮放後的寬度

                    * @param      int    $height              縮放後的高度

                    * @param      string        $qz            是新圖片的前綴

                    * @return      mixed                          是縮放後的圖片名稱,失敗返回false;

                    */

                   function thumb($name, $width,$height,$qz="th_"){

                            /* 獲取圖片寬度、高度、及類型信息 */

                            $imgInfo =$this->getInfo($name);                                

                            /* 獲取背景圖片的資源 */

                            $srcImg = $this->getImg($name,$imgInfo);                         

                            /* 獲取新圖片尺寸 */

                            $size =$this->getNewSize($name,$width, $height,$imgInfo);      

                            /* 獲取新的圖片資源 */

                            $newImg =$this->kidOfImage($srcImg, $size,$imgInfo);  

                            /* 經過本類的私有方法,保存縮略圖並返回新縮略圖的名稱,以"th_"爲前*/

                            return$this->createNewImage($newImg, $qz.$name,$imgInfo);   

                   }

                  

                   /**

* 爲圖片添加水印

* @paramstring $groundName背景圖片,即須要加水印的圖片,暫只支持GIF,JPG,PNG格式

* @param string$waterName       圖片水印,即做爲水印的圖片,暫只支持GIF,JPG,PNG格式

* @param        int    $waterPos                 水印位置,有10種狀態,0爲隨機位置;

*                                                                       1爲頂端居左,2爲頂端居中,3爲頂端居右;

*                                                                       4爲中部居左,5爲中部居中,6爲中部居右;

*                                                                        7爲底端居左,8爲底端居中,9爲底端居右;

* @param        string        $qz            加水印後的圖片的文件名在原文件名前面加上這個前綴

* @return        mixed                                   是生成水印後的圖片名稱,失敗返回false

*/

                   functionwaterMark($groundName, $waterName, $waterPos=0, $qz="wa_"){

                            /*獲取水印圖片是當前路徑,仍是指定了路徑*/

                            $curpath= rtrim($this->path,"/")."/";

                            $dir= dirname($waterName);

                            if($dir== "."){

                                     $wpath= $curpath;

                            }else{

                                     $wpath= $dir."/";

                                     $waterName= basename($waterName);

                            }

                           

                            /*水印圖片和背景圖片必須都要存在*/

if(file_exists($curpath.$groundName)&& file_exists($wpath.$waterName)){

                   $groundInfo= $this->getInfo($groundName);                        //獲取背景信息

                   $waterInfo= $this->getInfo($waterName, $dir);                //獲取水印圖片信息

                   /*若是背景比水印圖片還小,就會被水印所有蓋住*/

                   if(!$pos= $this->position($groundInfo, $waterInfo, $waterPos)){

                                               echo'水印不該該比背景圖片小!';

                                               returnfalse;

                                     }

 

                   $groundImg= $this->getImg($groundName, $groundInfo);   //獲取背景圖像資源

                   $waterImg= $this->getImg($waterName, $waterInfo, $dir); //獲取水印圖片資源  

                                    

                   /*調用私有方法將水印圖像按指定位置複製到背景圖片中 */

                   $groundImg= $this->copyImage($groundImg, $waterImg, $pos, $waterInfo);

         /*經過本類的私有方法,保存加水圖片並返回新圖片的名稱,默認以"wa_"爲前綴 */

         return$this->createNewImage($groundImg, $qz.$groundName, $groundInfo);

                                    

                            }else{

                                     echo'圖片或水印圖片不存在!';

                                     returnfalse;

                            }

                   }

                   /**

                   *在一個大的背景圖片中剪裁出指定區域的圖片

                   *@param        string        $name      須要剪切的背景圖片

                   *@param        int    $x                        剪切圖片左邊開始的位置

                   *@param        int    $y                        剪切圖片頂部開始的位置

                   *@param        int    $width               圖片剪裁的寬度

                   *@param        int    $height              圖片剪裁的高度

                   *@param        string        $qz            新圖片的名稱前綴

                   *@return        mixed                          裁剪後的圖片名稱,失敗返回false;

                   */

function cut($name, $x, $y, $width, $height,$qz="cu_"){

$imgInfo=$this->getInfo($name);                 //獲取圖片信息

/* 裁剪的位置不能超出背景圖片範圍 */

if( (($x+$width) >$imgInfo['width']) || (($y+$height) > $imgInfo['height'])){

echo "裁剪的位置超出了背景圖片範圍!";

return false;

}

 

$back = $this->getImg($name,$imgInfo);         //獲取圖片資源

/* 建立一個能夠保存裁剪後圖片的資源 */

$cutimg = p_w_picpathcreatetruecolor($width,$height);

/* 使用p_w_picpathcopyresampled()函數對圖片進行裁剪 */

p_w_picpathcopyresampled($cutimg, $back,0, 0, $x, $y, $width, $height, $width, $height);

p_w_picpathdestroy($back);

/* 經過本類的私有方法,保存剪切圖並返回新圖片的名稱,默認以"cu_"爲前綴 */

return $this->createNewImage($cutimg,$qz.$name,$imgInfo);

}

 

/* 內部使用的私有方法,用來肯定水印圖片的位置 */

private function position($groundInfo,$waterInfo, $waterPos){

/* 須要加水印的圖片的長度或寬度比水印還小,沒法生成水印 */

         if(($groundInfo["width"]<$waterInfo["width"]) ||($groundInfo["height"]<$waterInfo["height"]) ) {

                                     returnfalse;

                            }

                            switch($waterPos){

                                     case1:                        //1爲頂端居左

                                               $posX= 0;

                                               $posY= 0;

                                               break;

                                     case2:                        //2爲頂端居中

                                               $posX= ($groundInfo["width"] - $waterInfo["width"]) / 2;

                                               $posY= 0;

                                               break;

                                     case3:                        //3爲頂端居右

                                               $posX= $groundInfo["width"] - $waterInfo["width"];

                                               $posY= 0;

                                               break;

                                     case4:                        //4爲中部居左

                                               $posX= 0;

                                               $posY= ($groundInfo["height"] - $waterInfo["height"]) / 2;

                                               break;

                                     case5:                        //5爲中部居中

                                               $posX= ($groundInfo["width"] - $waterInfo["width"]) / 2;

                                               $posY= ($groundInfo["height"] - $waterInfo["height"]) / 2;

                                               break;

                                     case6:                        //6爲中部居右

                                               $posX= $groundInfo["width"] - $waterInfo["width"];

                                               $posY= ($groundInfo["height"] - $waterInfo["height"]) / 2;

                                               break;

                                     case7:                        //7爲底端居左

                                               $posX= 0;

                                               $posY= $groundInfo["height"] - $waterInfo["height"];

                                               break;

                                     case8:                        //8爲底端居中

                                               $posX= ($groundInfo["width"] - $waterInfo["width"]) / 2;

                                               $posY= $groundInfo["height"] - $waterInfo["height"];

                                               break;

                                     case9:                        //9爲底端居右

                                               $posX= $groundInfo["width"] - $waterInfo["width"];

                                               $posY= $groundInfo["height"] - $waterInfo["height"];

                                               break;

                                     case0:

                                     default:             //隨機

                                               $posX= rand(0,($groundInfo["width"] - $waterInfo["width"]));

                                               $posY= rand(0,($groundInfo["height"] - $waterInfo["height"]));

                                               break;

                            }

                            returnarray("posX"=>$posX, "posY"=>$posY);

                   }

 

                  

                   /*內部使用的私有方法,用於獲取圖片的屬性信息(寬度、高度和類型) */

                   privatefunction getInfo($name, $path=".") {

                            $spath= $path=="." ? rtrim($this->path,"/")."/" :$path.'/';

                           

                            $data= getp_w_picpathsize($spath.$name);

                            $imgInfo["width"]   = $data[0];

                            $imgInfo["height"]  = $data[1];

                            $imgInfo["type"]      = $data[2];

 

                            return$imgInfo;               

                   }

 

                   /*內部使用的私有方法, 用於建立支持各類圖片格式(jpg,gif,png三種)資源  */

                   privatefunction getImg($name, $imgInfo, $path='.'){

                           

                            $spath= $path=="." ? rtrim($this->path,"/")."/" :$path.'/';

                            $srcPic= $spath.$name;

                           

                            switch($imgInfo["type"]) {

                                     case1:                                           //gif

                                               $img= p_w_picpathcreatefromgif($srcPic);

                                               break;

                                     case2:                                           //jpg

                                               $img= p_w_picpathcreatefromjpeg($srcPic);

                                               break;

                                     case3:                                           //png

                                               $img= p_w_picpathcreatefrompng($srcPic);

                                              break;

                                     default:

                                               returnfalse;

                                               break;

                            }

                            return$img;

                   }

                  

                   /*內部使用的私有方法,返回等比例縮放的圖片寬度和高度,若是原圖比縮放後的還小保持不變 */

                   privatefunction getNewSize($name, $width, $height, $imgInfo){         

                            $size["width"]= $imgInfo["width"];         //原圖片的寬度

                            $size["height"]= $imgInfo["height"];        //原圖片的高度

                           

                            if($width< $imgInfo["width"]){

$size["width"]=$width;  //縮放的寬度若是比原圖小才從新設置寬度

                            }

 

                            if($height< $imgInfo["height"]){

                                     $size["height"]= $height;  //縮放的高度若是比原圖小才從新設置高度

                            }

                            /*等比例縮放的算法 */

if($imgInfo["width"]*$size["width"]> $imgInfo["height"] * $size["height"]){

         $size["height"]=round($imgInfo["height"]*$size["width"]/$imgInfo["width"]);

                            }else{

         $size["width"]= round($imgInfo["width"]*$size["height"]/$imgInfo["height"]);

                            }

                            return$size;

                   }       

                  

                   /*內部使用的私有方法,用於保存圖像,並保留原有圖片格式 */

                   privatefunction createNewImage($newImg, $newName, $imgInfo){

                            $this->path= rtrim($this->path,"/")."/";

                            switch($imgInfo["type"]) {

                                  case1:                                 //gif

                                               $result= p_w_picpathGIF($newImg, $this->path.$newName);

                                               break;

                                     case2:                                 //jpg

                                               $result= p_w_picpathJPEG($newImg,$this->path.$newName); 

                                               break;

                                     case3:                                 //png

                                               $result= p_w_picpathPng($newImg, $this->path.$newName); 

                                               break;

                            }

                            p_w_picpathdestroy($newImg);

                            return$newName;

                   }

 

                   /*內部使用的私有方法,用於加水印時複製圖像 */

private function copyImage($groundImg,$waterImg, $pos, $waterInfo){

     p_w_picpathcopy($groundImg,$waterImg, $pos["posX"], $pos["posY"], 0, 0,$waterInfo["width"],$waterInfo["height"]);

                            p_w_picpathdestroy($waterImg);

                            return$groundImg;

                   }

 

/* 內部使用的私有方法,處理帶有透明度的圖片保持原樣 */

                   privatefunction kidOfImage($srcImg, $size, $imgInfo){

                            $newImg= p_w_picpathcreatetruecolor($size["width"], $size["height"]);              

                            $otsc= p_w_picpathcolortransparent($srcImg);                                           

                            if($otsc >= 0 && $otsc < p_w_picpathcolorstotal($srcImg)) {                 

                                    $transparentcolor = p_w_picpathcolorsforindex($srcImg, $otsc );

                                     $newtransparentcolor = p_w_picpathcolorallocate(

                                            $newImg,

                                             $transparentcolor['red'],

                                          $transparentcolor['green'],

                                            $transparentcolor['blue']

                                    );

                                    p_w_picpathfill( $newImg, 0, 0, $newtransparentcolor);

                                    p_w_picpathcolortransparent( $newImg,$newtransparentcolor );

                            }

p_w_picpathcopyresized( $newImg, $srcImg, 0, 0, 0,0, $size["width"], $size["height"],$imgInfo["width"], $imgInfo["height"] );

                            p_w_picpathdestroy($srcImg);

                            return$newImg;

                   }

         }

微信掃一掃 、關注公衆號

   不定時分享資料視頻

20191030160339365.jpg

相關文章
相關標籤/搜索