Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Imagick - Can't read image files from URL.

Tags:

php

imagick

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?

like image 741
superhero Avatar asked Jan 24 '14 10:01

superhero


2 Answers

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.

like image 42
superhero Avatar answered Oct 22 '22 13:10

superhero


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();
like image 129
Uday Hiwarale Avatar answered Oct 22 '22 14:10

Uday Hiwarale