GD庫處理映像

來源:互聯網
上載者:User

標籤:ges   charset   瀏覽器   縮小   ext   fill   數組   height   new   

在PHP5中,動態圖象的處理要比以前容易得多。PHP5在php.ini檔案中包含了GD擴充包,只需去掉GD擴充包的相應注釋就可以正常使用了。PHP5包含的GD庫正是升級的GD2庫,其中包含支援真彩影像處理的一些有用的JPG功能。

一般產生的圖形,通過PHP的文檔格式存放,但可以通過HTML的圖片插入方式SRC來直接擷取動態圖形。比如,驗證碼、浮水印、微縮圖等。

 

一.建立映像

建立映像的一般流程:

1).設定標題,告訴瀏覽器你要產生的MIME類型。

2).建立一個映像地區,以後的操作都將基於此映像地區。

3).在空白映像地區繪製填充背景。

4).在背景上繪製圖形輪廓輸入文本。

5).輸出最終圖形。

6).清除所有資源。

7).其他頁面調用映像。

 

  //1 設定標題  //header("Content-type:text/html;charset=utf-8");  //不寫就是預設,預設的是html文檔  header("Content-type:image/png;");    //2 建立一個繪圖區域,映像背景  //兩種方式  $im = imagecreatetruecolor(200, 200); //返回的是資源控制代碼    //3 在空白映像地區繪製填充背景  //填充顏色換掉,首相要有個顏色填充器  $blue = imagecolorallocate($im, 0, 102, 255);  //將顏色填充到背景上去,imagefill();  imagefill($im, 0, 0, $blue);    //4 在背景上繪製圖形輪廓輸入文本  $white = imagecolorallocate($im, 255, 255, 255);  //畫一條線imageline();  imageline($im, 0, 0, 200, 200, $white);  imageline($im, 0, 200, 200, 0, $white);  //繪製文本imagestring();二號字型,x y位置,文本,顏色  imagestring($im, 5, 70, 20, ‘Mr.dang‘, $white);    //5 輸出最終圖形  imagepng($im);  //輸出png格式圖片  //6 清空資源  imagedestroy($im);

 

7).其他頁面調用映像:只需要在另一個php檔案調用即可,輸出img標籤,src寫上邊建立的映像的檔案地址

 

  //demo2.php  echo ‘<img src="demo.php" alt="圖形" title="圖形" />‘;

 

二.簡單小案例

1 驗證碼

/*簡單驗證碼案例  */    //1 擷取四位元字字母混編  //mt_rand();擷取隨機數  //dechex();十進位轉換為十六進位  for($i=0;$i<4;++$i){      $nmsg .= dechex(mt_rand(0, 15));  }    //2 將上邊的變成圖片  header(‘Content-type:image/png‘);  $im = imagecreatetruecolor(75, 25);  $blue = imagecolorallocate($im, 0, 102, 255);  $white = imagecolorallocate($im, 255,255,255);  imagefill($im, 0, 0, $blue);  imagestring($im, 5, 20, 5, $nmsg, $white);  imagepng($im);  imagedestroy($im);

2 載入已有映像:這個映像事先防止於和php檔案同一目錄下,可以用相對或絕對位址調用,建議用絕對因為更快,我們的樣本中用的是絕對,用魔法常量賴實現的

/*載入已有映像  *///  ////   define(‘URL‘, dirname(__FILE__).‘\\‘);//   header(‘Content-type:image/png‘);//   $im = imagecreatefrompng(URL.‘2.png‘);//   $white = imagecolorallocate($im, 255, 255, 255);//   imagestring($im, 5, 10, 10, ‘http://www.dang.com‘, $white);//   imagepng($im);//   imagedestroy($im);

3 採用本機系統中的字型

/*採用系統提供字型  *///   $font = ‘C:\Windows\Fonts\msyh.ttc‘; //把路徑傳進去即可//   define(‘URL‘, dirname(__FILE__).‘\\‘);  //   header(‘Content-type:image/png‘);//   $im = imagecreatefrompng(URL.‘2.png‘);//   $white = imagecolorallocate($im, 255, 255, 1);//   imagettftext($im,40,10,50,100,$white,$font,‘hello,黨興明‘); //第一個數字字型大小,第二個旋轉角度//   imagepng($im);//   imagedestroy($im);

4 微縮圖

/*微縮圖問題  */  define(‘URL‘, dirname(__FILE__).‘\\‘);  header(‘Content-type:image/png‘);  //1 取得映像大小  list($width,$height) = (getimagesize(URL.‘2.png‘));  //返回的是數組,裡邊包含尺寸  //2 將原圖的寬高縮小百分比  $_width = $width*0.4;  $_height = $height*0.4;    //3 建立一個新圖  $im = imagecreatetruecolor($_width, $_height);    //4 載入原圖,將原圖複製到新圖上去  //載入  $_im = imagecreatefrompng(URL.‘2.png‘);  //將原圖重新採樣,拷貝到新圖上,最後按0.4的比例輸出  //imagecopyresampled();重新採樣拷貝圖片並調整大小  imagecopyresampled($im, $_im, 0, 0, 0, 0, $_width, $_height, $width, $height);    //5 輸出銷毀  imagepng($im);  imagedestroy($im);  imagedestroy($_im);

注意:以上樣本中都用的png格式的圖片,可以用其他格式的(只要把函數中的png替換就好)。但jpg在函數中要寫jpeg,bmp要寫wbmp。

 

GD庫處理映像

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.