Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP download jpeg image from url

Tags:

php

jpeg

gd

i have a simple test code

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

but it cant working

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

what a problem? 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

i execute script by the php cli.

like image 307
versh23 Avatar asked May 07 '14 23:05

versh23


1 Answers

The image you're trying to download is actually a PNG, not a JPEG.

enter image description here

Yes, I know the URL ends in .jpg but that doesn't mean it is actually a JPEG image.

imagecreatefromstring() is able to automatically detect supported image types from their header. So you should be able to do something like this:

$image_data = file_get_contents('http://www.example.com/image.jpg_or_png_or_whatever');
$img = imagecreatefromstring($image_data);
like image 159
Jonathon Reinhart Avatar answered Nov 08 '22 13:11

Jonathon Reinhart