![](/img/trans.png)
[英]How to catch invalid image from third party website when using PHP imagecreatefromstring
[英]How to catch invalid image when using PHP imagecreatefromstring
我正在使用imagecreatefromstring,目前正在驗證正確的圖像文件格式。
因此鏈接:
swqkdwfibqwfwf
無法工作,因為它不是有效的文件類型。 但是我剛剛發現了這一點:
sibdlsibiwbifw.png
從我的驗證中將無錯誤地發送。 對於未返回的圖片鏈接和圖片,我收到此錯誤:
Warning: file_get_contents(etwteet.png): failed to open stream: No such file or directory in /var/www/clients/client2/web3/web/process/addnewbuild.php on line 141
Warning: imagecreatefromstring(): Empty string or invalid image in /var/www/clients/client2/web3/web/process/addnewbuild.php on line 141
Warning: imagejpeg() expects parameter 1 to be resource, boolean given in /var/www/clients/client2/web3/web/process/addnewbuild.php on line 143
有沒有辦法我可以捕獲此錯誤,以便我可以停止代碼處理並通知用戶?
用於獲取URL的代碼:
$imagefile = image url;
$resource = imagecreatefromstring(file_get_contents($imagefile));
謝謝。 克雷格。
通過所有可實施的驗證,我相信最終需要捕獲imagecreatefromstring
錯誤。
包含PHP 5.3或更高版本上可用的語法。
set_error_handler(function ($no, $msg, $file, $line) {
throw new ErrorException($msg, 0, $no, $file, $line);
});
try {
$img = imagecreatefromstring(file_get_contents("..."));
} catch (Exception $e) {
echo $e->getMessage();
}
@
和error_get_last
... if (!$img = @imagecreatefromstring(file_get_contents("..."))) {
$e = error_get_last();
die($e['message']);
}
包含PHP 5.4或更高版本上可用的語法。
if (!$img = @imagecreatefromstring(file_get_contents("..."))) {
die(error_get_last()['message']);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.