我正在尝试使用 imagemagick 的函数“thumbnailImage”调整图像大小。现在,我之后没有对图像做任何事情,只是回应新的尺寸以查看它是否有效。到目前为止,它没有用。这是我的代码。注意:它确实反射(reflect)了原始尺寸,而不是新尺寸。
$image = $_FILES["file"]["tmp_name"];
//Get original dimensions
list($width, $height, $type, $attr) = getimagesize($image);
echo "<BR>";
echo "ORIGINAL:";
echo "<BR>";
echo "Image width $width";
echo "<BR>";
echo "Image height " .$height;
$max_height = 200;
$max_width = 150;
function thumbnail($image, $max_width, $max_height) {
$img = new Imagick($image);
$img->thumbnailImage($max_width, $max_height, TRUE);
return $img;
}
thumbnail($image, $max_width, $max_height);
//get new dimensions
list($width, $height, $type, $attr) = getimagesize($img);
echo "<BR>";
echo "NEW:";
echo "<BR>";
echo "Image width $width";
echo "<BR>";
echo "Image height " .$height;
它甚至不显示第二组 echo 。现在有错误。
最佳答案
这段代码可以工作
$image = $_FILES["file"]["tmp_name"];
从任何你想要的地方获取文件等。你正在使用一个带有返回值的函数,但从未为它设置一个返回值的变量。您还需要保存文件才能使用 getimagesize。
<?
// $image = $_FILES["file"]["tmp_name"]; // get the file from where ever you want etc
/*
you are using a function with a return value but never set up a var for it to return to
as well you need to save the file in order to use getimagesize
*/
$image = 'test.png';;
//Get original dimensions
list($width, $height, $type, $attr) = getimagesize($image);
echo "<BR>";
echo "ORIGINAL:";
echo "<BR>";
echo "Image width $width";
echo "<BR>";
echo "Image height " .$height;
$max_height = 200;
$max_width = 150;
function thumbnail($image, $max_width, $max_height) {
$img = new Imagick($image);
$img->thumbnailImage($max_width, $max_height, TRUE);
return $img;
}
// orginal line thumbnail($image, $max_width, $max_height);
$img=thumbnail($image, $max_width, $max_height);
file_put_contents('testmeResize.png',$img );
//get new dimensions
list($width, $height, $type, $attr) = getimagesize('testmeResize.png');
echo "<BR>";
echo "NEW:";
echo "<BR>";
echo "Image width $width";
echo "<BR>";
echo "Image height " .$height;
// we set it to display the image for proof it works etc
?>
<br>
<img alt="" src="testmeResize.png">
关于php - 使用 php Imagemagick 调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11401039/