Imagick Interprets external image as HTML

145 views Asked by At

The problem I'm having is that when saving an image from a specific external website (not all websites), I get an error where the image is interpreted as html and not as another image format.

So far, this website is the only site that I've experienced this problem on, but I definitely need to find a solution.

Link to the images: http://www.nycvelo.com/bike-of-the-week-nicks-geekhouse-commuter/

An image to test with: http://www.nycvelo.com/wp-content/uploads/2013/10/Nicks-Geekhouse-7.jpg

Code from line 86:

$imageURL = 'http://www.nycvelo.com/wp-content/uploads/2013/10/Nicks-Geekhouse-7.jpg';

// Get image file from imagick
$ImagickImage = new Imagick($imageURL);

Error:

Fatal error:  Uncaught exception 'ImagickException' with message 'no decode delegate for this image format `HTML' @ error/constitute.c/ReadImage/535' in uploader.php:86
Stack trace:
#0 uploader.php(86): Imagick->__construct('http://www.nycvelo.com/wp-content/uploads/2013/10/Nicks-Geekhouse-7.jpg')

As far as I can tell, there is no "html" delegate in the first place, nor would it make sense to have one, so perhaps this requires a solution outside of ImageMagick/Imagick?

Update

I ran a test on the file to see what headers were sent back and this is what I got after using file_get_contents.

array(9) {
  [0]=>
  string(15) "HTTP/1.1 200 OK"
  [1]=>
  string(23) "Content-Type: text/html"
  [2]=>
  string(17) "Connection: close"
  [3]=>
  string(23) "Cache-Control: no-cache"
  [4]=>
  string(17) "Connection: close"
  [5]=>
  string(20) "Content-Length: 6195"
  [6]=>
  string(81) "X-Iinfo: 5-12048231-0 0CNN RT(1434509018550 6) q(1 -1 -1 -1) r(1 -1) B10(4,289,0)"
  [7]=>
  string(163) "Set-Cookie: visid_incap_264353=osbiSagORl2MGqb2cebUn9regFUAAAAAQUIPAAAAAAAUiLr5I31XtpMPuiNCWNQH; expires=Thu, 15 Jun 2017 13:43:05 GMT; path=/; Domain=.nycvelo.com"
  [8]=>
  string(118) "Set-Cookie: incap_ses_239_264353=XJXubnJwpRuBGVGGahpRA9regFUAAAAAbY2asXWHPVkKb5NEpRJyXg==; path=/; Domain=.nycvelo.com"
}
2

There are 2 answers

1
Danack On BEST ANSWER

I strongly recommend not using Imagick for downloading images from other servers. Either use CURL or some other library that gives you adequate control over the download (including being able to see redirects, incorrect content types, etc), and then process the image with Imagick once it is downloaded correctly.

Source - I'm the maintainer for Imagick.

0
Verox On

The server may be redirecting your request to some html page somewhere, check the contents of the response to find out if this is the case.