本文執行個體分析了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 輸出,可以通過如下的方式調用:
更多關於PHP相關內容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《php檔案操作總結》、《PHP數組(Array)操作技巧大全》、《PHP基本文法入門教程》、《PHP運算與運算子用法總結》、《php物件導向程式設計入門教程》、《PHP網路編程技巧總結》、《php字串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》
希望本文所述對大家PHP程式設計有所協助。