PHP从url下载jpeg图像

时间:2014-05-07 23:55:56

标签: php jpeg gd

我有一个简单的测试代码

imagecreatefromjpeg("http://www.eventmag.ru/uploads/users/483/11483/event/logo/14632/5ee58096b7238c30.jpg");

但它无法正常工作

gd-jpeg: JPEG library reports unrecoverable error: Not a JPEG file: starts with 0x89 0x50
PHP Warning:  imagecreatefromjpeg(): 'http://www.eventmag.ru/uploads/users/483/11483/event/logo/14632/5ee58096b7238c30.jpg' is not a valid JPEG file in /var/www/site/releases/test.php on line 5
什么问题? PHP 5.4.4-14

gd

GD Support => enabled
GD Version => 2.0
FreeType Support => enabled
FreeType Linkage => with freetype
FreeType Version => 2.4.9
GIF Read Support => enabled
GIF Create Support => enabled
JPEG Support => enabled
libJPEG Version => unknown
PNG Support => enabled
libPNG Version => 1.2.49
WBMP Support => enabled

Directive => Local Value => Master Value
gd.jpeg_ignore_warning => 0 => 0

gettext

GetText Support => enabled

我通过php cli执行脚本。

1 个答案:

答案 0 :(得分:14)

您尝试下载的图片实际上是PNG,而不是JPEG。

enter image description here

是的,我知道网址以.jpg结尾,但这并不意味着它实际上是一张JPEG图片。

imagecreatefromstring()能够自动检测其标题中支持的图像类型。所以你应该能够做到这样的事情:

$image_data = file_get_contents('http://www.example.com/image.jpg_or_png_or_whatever');
$img = imagecreatefromstring($image_data);