![](/img/trans.png)
[英]Issue with creating an image with PHP GD `imagecreatefromstring` function
[英]GD - Getting the proper output function from imagecreatefromstring()
我已經通過imagecreatefromstring()
讀取了圖像路徑,並對其進行了處理,但是現在我需要輸出它,這就是我遇到的問題。
$imgpath = "test.jpg"; //Testing
$img = imagecreatefromstring(file_get_contents($imgpath));
//Do stuff to $img
imagejpeg($img);
imagedestroy($img);
無論類型如何,這都會將圖像加載到$img
資源變量中。 但是,我目前僅使用imagejpeg()
輸出圖像,如果源( $imgpath
)是jpeg,則可以正常工作。 但是,如果我將imagejpeg
更改為imagepng
,則服務器必須將jpeg轉換為png,這在圖像加載時間上非常明顯。
由於我永遠不會確切知道通過函數輸入的是哪種類型的圖像,我如何分辨它是哪種類型的圖像並相應地采取行動以進行輸出? 我試圖避免使用文件名檢查方法,但是如果GD無法返回正在處理的圖像類型,則似乎必須采用這種方法。
謝謝!
您可以使用php函數getimagesize()來查看要處理的圖像類型,並根據其使用適當的方法。 http://php.net/manual/zh/function.getimagesize.php
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.