0

I'm currently trying to teach myself PHP by doing a small image-processing type project with Imagemagick. To start off with the basics, I wrote some simple code to read in an image and convert it to a PNG.

However, while I'm able to read from local image files, I am completely unable to read in images from URLs, as it crashes when I call readImageFile() on the url, and I get the following error:

Fatal error: Uncaught exception 'ImagickException' with message 'Invalid CRT parameters detected' in C:\xampp\htdocs\imagepractice\imagemagicktest.php:8 Stack trace: #0 C:\xampp\htdocs\imagepractice\imagemagicktest.php(8): Imagick->readimagefile(Resource id #3) #1 {main} thrown in C:\xampp\htdocs\imagepractice\imagemagicktest.php on line 8

I've spent the last hour Googling for a way to fix this, to no avail, and the only lead I have been able to find is Error in using readImage function (Imagick). However, unlike that issue, I am perfectly able to use readImage, and I'm even able to use readImageFile on local files, just not on image URLs.

From the only comment there, it seems that it may possibly be a bug specific to Windows, but I was wondering if anyone happens to be able to confirm/deny this and/or suggest a way to fix the CRT parameter error?

For reference, the code I wrote is below:

<?php
$im = new Imagick();

//$im->newPseudoImage(1000, 1000, "magick:rose"); //this works!

//$im->readImage("images\\wheels.jpg"); // this works!

$handle = fopen("http://www.google.com/images/srpr/logo3w.png", "rb");
$im->readImageFile($handle); //this line crashes!
fclose($handle);

$im->setImageFormat("png");
$type = $im->getFormat();
header("Content-type: $type");
echo $im->getImageBlob();
?>

In addition, I am running 64-bit Windows 7, and I am using XAMPP 1.7.7 (which uses PHP 5.3.8), and I initially installed Imagemagick 6.6.4 using these instructions. (Although I replaced the 6.6.4 version with Imagemagick 6.6.2 instead, as per the suggestion of a commenter here, which hasn't fixed anything.)

Community
  • 1
  • 1
L. Zhang
  • 1
  • 1
  • 2

1 Answers1

0

Thanks to some friendly folks at another coding forum, I finally figured out how to stop the getting the error and make this code work. Unfortunately, I'm still not sure what was causing the CRT Parameter error in the first place, but switching from fopen to file_get_contents solved the issue for me.

Working updated code:

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);

$im = new Imagick();

//$im->newPseudoImage(1000, 1000, "magick:rose"); //this works!
//$im->readImage("images\\wheels.jpg"); //this works!

$url = "http://www.google.com/images/srpr/logo3w.png";
$source = @file_get_contents($url);
if(!$source){
    throw new Exception("failed to retrieve contents of $url");
}

$im->readImageBlob($source);

$im->setImageFormat("png");
$type = $im->getFormat();
header("Content-type: $type");
echo $im->getImageBlob();
?>

However, according to someone else at the forum I posted this on,

Use curl, some places have allow_url_fopen disabled in PHP. I do some development on my Windows 7 64-bit machine using XAMPP and curl works everytime no matter what I am doing.

so just to be safe, I'll probably change to using curl instead and seeing if that works, but at least I've solved my issue for now!

L. Zhang
  • 1
  • 1
  • 2