簡體   English   中英

如何使用file_get_contents php獲取圖像擴展?

[英]How to get image extension using file_get_contents php?

我正在嘗試將來自其他網站的圖像復制到我的服務器,我打算使用

<? 
file_put_contents($toPath, file_get_contents($fromPath));
?>

好的,但要構建$toPath我需要擴展文件。 我知道怎么做如果$fromPath是一個普通的路徑,文件的名稱在最后,但如果我使用像facebook這樣的路徑使用http://graph.facebook.com/user_id/picture?type=large how我可以獲得延期嗎?

謝謝你們。

$size = getimagesize($fromPath);
$extension = image_type_to_extension($size[2]);

獲取圖像大小,獲取文件類型。

在PHP 5.3中,您可以使用finfo_file() ,請參閱PHP參考

file_put_contents($toPath, file_get_contents($fromPath));
$handle = fopen($toPath,'r');
print finfo_file($handle);

這也適用於非圖像。

這比@Kolink的回答要快:

$extension= end(explode("/", image_type_to_mime_type(exif_imagetype($photoURL))));

無需猜測或使用mime類型或魔術字節。 facebook圖形api總是返回jpegs(有關討論,請參閱FB Profile Pic總是返回為“.jpg”? )。

但總的來說,這里是智能文件類型檢測的參考: http//designshack.net/articles/php-articles/smart-file-type-detection-using-php/

如果你只有一個字節流, finfo_file()就是你的朋友。

如果您有mime類型,請使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM