PHP載入映像imagecreatefrom_gif_jpeg_png系列函數用法分析_php技巧

來源:互聯網
上載者:User

本文執行個體分析了PHP載入映像imagecreatefrom_gif_jpeg_png系列函數用法。分享給大家供大家參考,具體如下:

imagecreatefrom 系列函數用於從檔案或 URL 載入一幅映像。

載入映像

imagecreatefrom 系列函數用於從檔案或 URL 載入一幅映像,成功返回映像資源,失敗則返回一個Null 字元串。

該系列函數有:

imagecreatefromgif():建立一塊畫布,並從 GIF 檔案或 URL 地址載入一副映像
imagecreatefromjpeg():建立一塊畫布,並從 JPEG 檔案或 URL 地址載入一副映像
imagecreatefrompng():建立一塊畫布,並從 PNG 檔案或 URL 地址載入一副映像
imagecreatefromwbmp():建立一塊畫布,並從 WBMP 檔案或 URL 地址載入一副映像
imagecreatefromstring():建立一塊畫布,並從字串中的映像流建立一副映像

文法:

resource imagecreatefromgif( string filename )resource imagecreatefromjpeg( string filename )resource imagecreatefrompng( string filename )resource imagecreatefromwbmp( string filename )resource imagecreatefromstring( string image )

例子:

<?header("Content-type: image/jpeg");//建立並載入一幅映像$im = @imagecreatefromjpeg("images/flower_1.jpg");//錯誤處理if(!$im){  $im = imagecreatetruecolor(150, 30);  $bg = imagecolorallocate($im, 255, 255, 255);  $text_color = imagecolorallocate($im, 0, 0, 255);  //填充背景色  imagefilledrectangle($im, 0, 0, 150, 30, $bg);  //以映像方式輸出錯誤資訊  imagestring($im, 3, 5, 5, "Error loading image", $text_color);} else {  //輸出該映像  imagejpeg($im);}?>

在該例子中,我們載入並輸出原圖。由於 PHP 對映像建立錯誤沒有友好的錯誤提示,因此我們自訂了錯誤處理資訊。

提示

對於 PHP 產生的圖片,如果要直接在普通網頁中顯示而不是通過 header 輸出,可以通過如下的方式調用:

<img src="pic.php" />

更多關於PHP相關內容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《php檔案操作總結》、《PHP數組(Array)操作技巧大全》、《PHP基本文法入門教程》、《PHP運算與運算子用法總結》、《php物件導向程式設計入門教程》、《PHP網路編程技巧總結》、《php字串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》

希望本文所述對大家PHP程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.