如何使用PHP中的imagecreatefromjpeg()函式從JPEG檔案建立一個新影像?
imagecreatefromjpeg() 是PHP中的一個內建函式,用於從JPEG檔案建立一個新影像。它返回一個影像識別符號,表示從給定檔名獲得的影像。
語法
resource imagecreatefromjpeg(string $filename)引數
imagecreatefromjpeg() 只使用一個引數,$filename,它包含影像的名稱或JPEG影像的路徑。
返回值
imagecreatefromjpeg() 成功時返回影像資源識別符號,失敗時返回錯誤。
示例1
<?php // Load an image from local drive/file $img = imagecreatefromjpeg('C:\xampp\htdocs\test\1.jpeg'); // it will show the loaded image in the browser header('Content-type: image/jpg'); imagejpeg($img); imagedestroy($img); ?>
輸出

示例2
<?php // Load a JPEG image from local drive/file $img = imagecreatefromjpeg('C:\xampp\htdocs\test\1(a).jpeg'); // Flip the image imageflip($img, 1); // Save the GIF image in the given path. imagejpeg($img,'C:\xampp\htdocs\test\1(b).png'); imagedestroy($img); ?>
輸入影像
.jpg)
輸出影像
.png)
解釋 − 在示例2中,我們使用imagecreatefromjpeg()函式從本地路徑載入jpeg影像。然後,我們使用imageflip()函式翻轉影像。
示例3 − 處理載入JPEG影像期間的錯誤
<?php function LoadJpeg($imgname) { /* Attempt to open */ $im = @imagecreatefromjpeg($imgname); /* See if it failed */ if(!$im) { /* Create a black image */ $im = imagecreatetruecolor(700, 300); $bgc = imagecolorallocate($im, 0, 0, 255); $tc = imagecolorallocate($im, 255,255, 255); imagefilledrectangle($im, 0, 0, 700, 300, $bgc); /* Output an error message */ imagestring($im, 20, 80, 80, 'Error loading ' . $imgname, $tc); } return $im; } header('Content-Type: image/jpeg'); $img = LoadJpeg('bogus.image'); imagejpeg($img); imagedestroy($img); ?>
輸出
.png)
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP