0

我目前正在尝试通过使用 Imagemagick 做一个小型图像处理类型的项目来自学 PHP。从基础开始,我编写了一些简单的代码来读取图像并将其转换为 PNG。

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

致命错误:在 C:\xampp\htdocs\imagepractice\imagemagicktest.php:8 中未捕获的异常 'ImagickException' 和消息 'Invalid CRT parameters detected' 堆栈跟踪:#0 C:\xampp\htdocs\imagepractice\imagemagicktest.php(8 ): Imagick->readimagefile(Resource id #3) #1 {main} 在第 8 行的 C:\xampp\htdocs\imagepractice\imagemagicktest.php 中抛出

我花了最后一个小时在谷歌上搜索解决这个问题的方法,但无济于事,我能找到的唯一线索是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),并且我最初使用这些说明安装了 Imagemagick 6.6.4 。(虽然我用 Imagemagick 6.6.2 替换了 6.6.4 版本,但根据此处评论者的建议,这并没有解决任何问题。)

4

1 回答 1

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

但是,根据我在论坛上发布的其他人的说法,

使用 curl,有些地方在 PHP 中禁用了 allow_url_fopen。我使用 XAMPP 在我的 Windows 7 64 位机器上进行了一些开发,无论我在做什么,curl 每次都可以工作。

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

于 2012-06-16T16:35:12.300 回答