2012-06-14 251 views
0

我目前正试图通过Imagemagick做一个小型图像处理类型项目来教自己PHP。为了从基础开始,我写了一些简单的代码来读取图像并将其转换为PNG。当在URL上使用Imagick的readImageFile时,如何解决“检测到无效的CRT参数”错误?

但是,虽然我能够从本地图像文件读取,但我完全无法从URL中读取图像,因为它在URL上调用readImageFile()时崩溃,并且出现以下错误:

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

我花了最后一个小时的谷歌搜索的方式来解决这个问题,但没有成功,我已经能够找到的唯一的领先是Error in using readImage function (Imagick)。然而,与这个问题不同,我完全可以使用readImage,甚至可以在本地文件上使用readImageFile,而不是在图像URL上。

从那里唯一的评论,它似乎可能是一个特定于Windows的错误,但我想知道如果任何人碰巧能够确认/否认这一点和/或建议一种方法来解决CRT参数错误?

作为参考,我写的代码如下:

<?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(); 
?> 

此外,我运行64位Windows 7,以及我使用XAMPP 1.7.7(其使用PHP 5.3.8),我最初使用these instructions安装Imagemagick 6.6.4。 (虽然我用Imagemagick 6.6.2替换了6.6.4版本,但是根据评论者here的建议,它没有固定任何东西。)

回答

0

感谢一些友好的人在另一个编码论坛,我终于想通了了解如何停止获取错误并使此代码正常工作。不幸的是,我仍然不确定是什么原因导致了CRT参数错误,但是从fopen切换到file_get_contents解决了我的问题。

工作更新的代码:

<?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(); 
?> 

然而,根据在论坛上别人我张贴这种上,

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.

所以为了安全起见,我可能会改变使用curl相反,看看是否有效,但至少我现在已经解决了我的问题!

+0

哥们我也有同样的问题:) – GorillaApe

相关问题