php操作映像(GD函數庫)

來源:互聯網
上載者:User

標籤:

PHP.ini裡修改;extension=php_gd2.dll

在PHP中建立一個映像應該完成這樣四個步驟:

•建立背景映像•在背景上繪製圖形或輸入文本•輸出最終圖形•清除所有資源

<?php

$width = 200; // 映像寬度

$height = 400; // 映像高度

$img = ImageCreateTrueColor( $width, $height );

?> 

通過ImageCreateFromPNG()、ImageCreateJPEG()或ImageCreateFromGIF()來讀取一個現有影像檔,然後對其進行過濾,再在它上面添加其他映像。 

<?php

$imgTemp = ImageCreateFromPNG(‘abc.png‘);

?> 

<?php

$width = 200; // 映像寬度

$height = 400; // 映像高度

$img = ImageCreateTrueColor( $width, $height );

$white = ImageColorAllocate($img, 255, 255, 255); //RGB值

$blue = ImageColorAllocate($img, 0, 0, 64);

ImageFill($img, 0, 0, $blue);  //往背景圖裡填充$blue

ImageLine($img, 0, 0, $width, $height, $white); /*這裡出現了2對座標,“0, 0”是起始點的座標,而$width, $height是終點的座標,最後一個參數就是顏色。*/

ImageString($img, 3, 100, 150, ‘abc‘, $white);//映像上書寫字串

?> 

<?php

$width = 200; // 映像寬度

$height = 400; // 映像高度

$img = ImageCreateTrueColor( $width, $height );

$white = ImageColorAllocate($img, 255, 255, 255); //RGB值

$blue = ImageColorAllocate($img, 0, 0, 64);

ImageFill($img, 0, 0, $blue);  //往背景圖裡填充$blue

ImageLine($img, 0, 0, $width, $height, $white); /*這裡出現了2對座標,“0, 0”是起始點的座標,而$width, $height是終點的座標,最後一個參數就是顏色。*/

ImageString($img, 3, 100, 150, ‘abc’, $white);//映像上書寫字串

header( ‘Content-type: image/png‘ ); //指定映像的MIME類型

imagepng($img);  //輸出映像資料

?> 

同樣的,如果你希望最終輸出JPEG映像,可以使用ImageJPEG()來輸出,同時還要將header()函數參數同步替換成image/jpeg。

  綜上所述,我們是將映像直接發送到瀏覽器,如果我們希望將自動建立的映像組建檔案,可以將在imagepng()函數中加上第二個參數,這個參數就是你希望產生的檔案名稱,如logo.png。注意,檔案尾碼名要保持格式一直,而且檔案名稱要用引號引起來。

-------------------------

釋放並銷毀資源

<?php

$width = 200; // 映像寬度

$height = 400; // 映像高度

$img = ImageCreateTrueColor( $width, $height );

$white = ImageColorAllocate($img, 255, 255, 255); //RGB值

$blue = ImageColorAllocate($img, 0, 0, 64);

ImageFill($img, 0, 0, $blue);  //往背景圖裡填充$blue

ImageLine($img, 0, 0, $width, $height, $white); /*這裡出現了2對座標,“0, 0”是起始點的座標,而$width, $height是終點的座標,最後一個參數就是顏色。*/

ImageString($img, 3, 100, 150, ‘abc’, $white);//映像上書寫字串

header( ‘Content-type: image/png‘ ); //指定映像的MIME類型

imagepng($img);  //輸出映像資料

ImageDestroy($img); //釋放並銷毀

?> 

調用自動產生的映像

<img src="image.php" height="400" width="200" /> 

總結:

建立背景映像:ImageCreateTrueColor()

 在背景上繪製圖形或輸入文本:ImageColorAllocate()、ImageFill()、ImageLine()、ImageString()

輸出最終圖形:header()、 imagepng()

清楚所有資源:imagedestroy()

php操作映像(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.