doubian0284
doubian0284
2015-06-17 01:43

Imagick将外部图像解释为HTML

  • imagick
  • php
  • imagemagick
已采纳

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条回答

  • duanliexi1052 duanliexi1052 6年前

    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.

    点赞 评论 复制链接分享
  • duanniubeng2265 duanniubeng2265 6年前

    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.

    点赞 评论 复制链接分享

为你推荐