如何在PHP中使用imagecreatefrompng()函式從PNG檔案或URL建立一個新影像?
在PHP中,imagecreatefrompng()是一個內建函式,用於從PNG檔案或URL建立一個新影像。imagecreatefrompng()返回一個影像識別符號,代表從給定檔名獲得的影像。
語法
resource imagecreatefrompng(string $filename)
引數
imagecreatefrompng()只接受一個引數,$filename。此引數包含影像名稱或PNG影像的路徑。
返回值
imagecreatefrompng() 函式成功時返回影像資源識別符號,失敗時返回錯誤。
示例1 - 在瀏覽器中顯示載入的PNG影像
<?php // Load an image from local drive/file $img = imagecreatefrompng('C:\xampp\htdocs\Images\img29.png'); // It will show the loaded PNG image in the browser header('Content-type: image/png'); imagejpeg($img); imagedestroy($img); ?>
輸出
示例2 - 載入並將PNG影像儲存到本地驅動器路徑
<?php // Load an image from local drive/file $img = imagecreatefrompng('C:\xampp\htdocs\Images\img29.png'); // Flip the image // imageflip($img,1); // Save the GIF image into the given local drive folder path. imagejpeg($img,'C:\xampp\htdocs\pic.gif'); imagedestroy($img); ?>
輸出
說明 - 在示例2中,使用imagecreatefrompng()函式從本地路徑載入png影像。然後,我們將png影像轉換為gif影像,並透過提供儲存gif影像的路徑將其儲存到本地驅動器。
我們也可以在瀏覽器中檢視影像(參見示例1)。
廣告