Imagick - Can't read image files from URL.

你。 提交于 2019-12-21 20:56:02

问题


I'm using this snippet for reading images on different websites:

$image = new Imagick('http://lp.hm.com/hmprod?set=key[source],value[/model/2012/P01 05156 06204 80 1175 4.jpg]&set=key[rotate],value[]&set=key[width],value[]&set=key[height],value[]&set=key[x],value[]&set=key[y],value[]&set=key[type],value[STILL_LIFE_FRONT]&call=url[file:/product/large]');

But sometimes, I get an error like this (about 20% of the time):

ImagickException

Unable to read the file: http://lp.hm.com/hmprod?set=key[source],value[/model/2012/P01 05156 06204 80 1175 4.jpg]&set=key[rotate],value[]&set=key[width],value[]&set=key[height],value[]&set=key[x],value[]&set=key[y],value[]&set=key[type],value[STILL_LIFE_FRONT]&call=url[file:/product/large]

Imagick->__construct()

The error seems to be consistent through this whole domain, but sometimes it's different from image to image on the same domain.

Questions

Why is this a problem?
How can we fix it?
Is there an alternative solution?


回答1:


STEP 1 : GET URL

$url = 'http://blablabla.com/blabla.jpeg';

STEP 2 : Save blog content to a variable

$image = file_get_contents($url);

STEP 3 : Create Imagick object

$img = new Imagick();

STEP 4 : Instruct imagick object to read blob content of the image

$img -> readImageBlob($image);

STEP 5 : Save (or do operations) on image

$img -> writeImage('/var/www/html/uploads/img.jpg');

STEP 6 : Destroy imagick instance

$img -> destroy();



回答2:


So I figured out I needed to encode the url properly. I'm not sure if this code is optimal, but it works.. and could hopefully help someone else.

$parsedUrl = parse_url('http://lp.hm.com/hmprod?set=key[source],value[/model/2012/P01 05156 06204 80 1175 4.jpg]&set=key[rotate],value[]&set=key[width],value[]&set=key[height],value[]&set=key[x],value[]&set=key[y],value[]&set=key[type],value[STILL_LIFE_FRONT]&call=url[file:/product/large]');
$info      = pathinfo($parsedUrl['path']);
$dirname   = explode('/', $info['dirname'] ?: '');
$dirname   = array_filter($dirname, 'strlen');
$dirname   = array_map('urlencode', $dirname);
$dirname   = implode('/', $dirname);
$basename  = urlencode($info['basename'] ?: '');
$path      = array_filter(array($dirname, $basename), 'strlen');
$path      = '/' . implode('/', $path);

$query = explode('&', $parsedUrl['query'] ?: '');
foreach ($query as &$set)
{
  $set = explode('=', $set, 2);
  $set = array_map('urlencode', $set);
  $set = implode('=', $set);
}

$query    = implode('&', $query);

$uri      = array_filter(array($path, $query), 'strlen');
$uri      = implode('?', $uri);

$fragment = urlencode($info['fragment'] ?: '');
$uri      = array_filter(array($uri, $fragment), 'strlen');
$uri      = implode('#', $uri);

$scheme   = $parsedUrl['scheme'] ?: '';
$host     = $parsedUrl['host']   ?: '';
$url      = array_filter(array($scheme, $host), 'strlen');
$url      = implode('://', $url);
$url     .= $uri;

$image    = new Imagick($url);

OBS!

This code will leave notifications.




回答3:


Try to use urlencode function for encode special chars of url:

$image = new Imagick(urlencode('http://lp.hm.com/hmprod?set=key[source],value[/model/2012/P01 05156 06204 80 1175 4.jpg]&set=key[rotate],value[]&set=key[width],value[]&set=key[height],value[]&set=key[x],value[]&set=key[y],value[]&set=key[type],value[STILL_LIFE_FRONT]&call=url[file:/product/large]'));

Or if not work, try this:

$content = file_get_contents(urlencode('http://lp.hm.com/hmprod?set=key[source],value[/model/2012/P01 05156 06204 80 1175 4.jpg]&set=key[rotate],value[]&set=key[width],value[]&set=key[height],value[]&set=key[x],value[]&set=key[y],value[]&set=key[type],value[STILL_LIFE_FRONT]&call=url[file:/product/large]'));
$image = new Imagick($content);



回答4:


I just encountered a similar issue. I was using file_get_contents() and was getting the same "ImagickException The filename is too long". The fix for me was finding the tmp directory and setting the correct permissions for it.



来源:https://stackoverflow.com/questions/21330242/imagick-cant-read-image-files-from-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!