【发布时间】:2012-09-10 17:14:46
【问题描述】:
我有这个 JPEG,它给我正在使用的 Resizer library 中的函数 imagesx($this->image) 带来了问题。我可以使用浏览器查看图像,但在尝试调整大小时出现错误:
imagesx() expects parameter 1 to be resource, boolean given
如果要抛出错误,我可以不处理此文件。如何使用 PHP 来检查这个图像是否可以被 PHP 的图像函数正确处理?
调用库的代码
// Download the photo
$img_content = file_get_contents($url);
if($img_content !== FALSE) {
file_put_contents($img_documentroot . $img_subpath . $img_filename . '_tmp.jpg',
$img_content);
}
echo $url . '<br>';
echo $img_documentroot . $img_subpath . $img_filename . '_tmp.jpg<br>';
ob_flush();
flush();
// Resize photo
Resizer::open( $img_documentroot . $img_subpath . $img_filename . '_tmp.jpg' )
->resize(300, 300, 'landscape' )
->save($img_documentroot . $img_subpath . $img_filename . '.jpg' , 90 );
// Thumbnail photo
Resizer::open( $img_documentroot . $img_subpath . $img_filename . '_tmp.jpg' )
->resize(100, 100, 'crop' )
->save($img_documentroot . $img_subpath . $img_filename . '.jpg' , 90 );
输出
我还回显了正在调整大小的图像的完整路径。
http://www.ApartmentsInAllstonMA.com/Images/Apts/132847_kn1.jpg
/home/photos/public_html/2012/0917/2516539_7_tmp.jpg
resource(127) of type (gd)
resource(130) of type (gd)
http://www.ApartmentsInMedford.com/Images/Apts/132847_lv2.jpg
/home/photos/public_html/2012/0917/2516539_11_tmp.jpg
resource(163) of type (gd)
resource(166) of type (gd)
http://www.AllstonApartmentX.com/images/agents/61.jpg
/home/photos/public_html/2012/0917/2516539_12_tmp.jpg
bool(false)
更新
这是导致库返回 false 值的代码 sn-p。
private function open_image( $file )
{
// If $file isn't an array, we'll turn it into one
if ( !is_array($file) ) {
$file = array(
'type' => File::mime( strtolower(File::extension($file)) ),
'tmp_name' => $file
);
}
$mime = $file['type'];
$file_path = $file['tmp_name'];
switch ( $mime )
{
case 'image/pjpeg': // IE6
case File::mime('jpg'): $img = @imagecreatefromjpeg( $file_path ); break;
case File::mime('gif'): $img = @imagecreatefromgif( $file_path ); break;
case File::mime('png'): $img = @imagecreatefrompng( $file_path ); break;
default: $img = false; break;
}
return $img;
}
【问题讨论】:
-
您将错误传递给您正在使用的库或您正在使用的库在内部已损坏。错误清楚地说明了错误是什么,错误的参数类型。您可能需要重新考虑更改库或向我们展示您用于调用实际函数的代码。
-
听起来
$this->image是一个布尔值,而它应该是一个图像资源。试试var_dump($this->image)看看会发生什么。 -
我在一组图像上做了一个 var_dump($this->image)。对于正确的图像,我得到类似
resource(64) of type (gd)的信息,但对于给出错误的图像,我得到bool(false)。此图片确实存在,我可以使用浏览器访问它。
标签: php apache imagemagick gd laravel