Imagick 将外部图像解释为 HTML


Imagick Interprets external image as HTML

我遇到的问题是,当从特定的外部网站(不是所有网站)保存图像时,我收到一个错误,其中图像被解释为 html 而不是另一种图像格式。

到目前为止,这个网站是我遇到过这个问题的唯一网站,但我绝对需要找到解决方案。

图片链接:http://www.nycvelo.com/bike-of-the-week-nicks-geekhouse-commuter/

要测试的图像:http://www.nycvelo.com/wp-content/uploads/2013/10/Nicks-Geekhouse-7.jpg

第 86 行的代码:

$imageURL = 'http://www.nycvelo.com/wp-content/uploads/2013/10/Nicks-Geekhouse-7.jpg';
// Get image file from imagick
$ImagickImage = new Imagick($imageURL);

错误:

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')

据我所知,首先没有"html"委托,拥有一个也没有意义,所以也许这需要ImageMagick/Imagick之外的解决方案?

更新

我对文件进行了测试,以查看发回了哪些标头,这就是我在使用 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"
}

我强烈建议不要使用Imagick从其他服务器下载图像。使用 CURL 或其他一些库,让您充分控制下载(包括能够看到重定向、不正确的内容类型等),然后在正确下载后使用 Imagick 处理图像。

来源 - 我是Imagick的维护者。

服务器可能会将您的请求重定向到某个 html 页面的某个地方,请检查响应的内容以确定是否是这种情况。