- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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']);
}
关于php - 使用 PHP imagecreatefromstring 时如何捕获无效图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24214685/
尝试在 wordpress 管理界面中上传时,我收到主题中提到的错误。 我的服务器使用 php-fpm 运行 nginx,PHP 版本是 5.3.0。 我在谷歌上搜索了很多关于这个问题的信息,但我找不
我在 PHP 中调整图像大小时遇到问题;似乎从我使用 imagecreatefromstring 或 imagecreatefrompng 加载图像的那一刻起,颜色就会改变并褪色。 我知道我必须使
这段代码有什么问题? 我收到这些错误: 警告:imagecreatefromstring():gd-png:致命的 libpng 错误:读取错误:第 5 行 D:\wamp\www\img\inde
最好的使用方法是什么imagecreatefromstring()或 .bmp 的替代方法文件? 这样: $data = file_get_contents($path); $image = imag
我一直在努力让我的应用程序变得透明(它在存储图像之前动态调整图像大小),我认为在对 imagealphablending 和 imagesavealpha。源图像从未以适当的透明度加载! // Wit
通常我使用 imagecreatefromjpeg() 然后使用 getimagesize(),但是对于 Firefox 3 我需要绕过这个不同。所以现在我正在使用 imagecreatefromst
假设我的代码看起来像这样: "; // // more stuff here // ?> 我要替换什么/* 这里是什么? */这样我的图像数据就会显示出来? 谢谢。 最佳答案 What do I re
这是我的代码: $data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl' . 'BMVEUAAAD///+l2Z/dAAA
我正在使用 imagecreatefromstring,目前正在验证正确的图像文件格式。 所以一个链接: swqkdwfibqwfwf 不会工作,因为它不是有效的文件类型。但我刚刚发现了这一点: si
我正在使用 imagecreatefromstring,目前正在验证正确的图像文件格式。 所以一个链接: swqkdwfibqwfwf 不会工作,因为它不是有效的文件类型。但我刚刚发现了这一点: si
我正在使用axios通过ajax发送base64字符串。使用下面的方法,当它从 Base64 数据编码回 jpg 时,我会以某种方式丢失大量数据。我怎样才能在不丢失数据的情况下发送此信息? 我从输入中
以下是我获取图片的方式: $coverurl = 'https://api.someurl/api/v1/img/' . $somenumber . '/l'; //$iheaders contai
我在 Javascript 和 PHP 之间以字符串形式发送图像时遇到问题。到目前为止,我有: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAA
我有一个简单的服务,它使用通过 URL 传递的参数生成任何文本的 PNG 图像。参数之一是文本本身,其余参数是“字体”、“颜色”、“字体粗细”等。 这种 URL 的一个例子是: http://pics
有没有办法使用 imagecreatefromstring() 并以某种方式获取图像类型? 最佳答案 当您使用 imagecreatefrom... 方法时,图像将作为未压缩的位图加载到内存中。此时并
我正在尝试从我无法控制的外部站点加载图像。 大多数时候它工作正常(到目前为止测试了数百张图像)。 它现在给我一个特定图像的错误: imagecreatefromstring():gd-jpeg、lib
目前,如果用户将照片发布/上传到我的 PHP 脚本,我会从这样的代码开始 getimagesize($_FILES['picture1']['tmp_name']); 然后我对它做了更多的事情,但我也
我创建了一个脚本,其中我必须在运行时使用 64 位编码的字符串创建图像。我使用 PHP 的 imagecreatefromstring 函数,但它在我的基于 Windows XAMPP 的 PHP 中
以下 JavaScript 代码会将 SVG 字符串转换为图像 blob,然后可用于在浏览器中显示: var data = '' + '' + '' +
当我尝试在不正确的 png 图像上使用 php-gd 函数时,出现致命的 PHP 错误。这似乎是某种错误,因为根据函数文档(例如 imagecreatefrompng): * @return reso
我是一名优秀的程序员,十分优秀!