使用php Imagemagick调整图像大小

问题描述:

我试图使用imagemagick的函数'thumbnailImage'来调整图像大小。现在,我没有对图像做任何事情,只是回应新的维度,看看它是否有效。到目前为止,它不起作用。继承我的代码。注意:它确实回显原始尺寸,而不是新尺寸。

Im trying to resize an image using imagemagick's function 'thumbnailImage'. Right now, im not doing anything with image afterwards, just echoing the new dimensions to see if it worked. And so far, it isnt working. Heres my code. NOTE: it does echo the original dimensions, just not the new ones.

$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;

它甚至不显示第二组回声。现在有错误。

It isnt even displaying the second set of echo's. There are now errors.

根据您的修改,您可以使用以下内容来获取宽度和高度:

With your modifications, you could use the following to get both the width and height:

$img = thumbnail($image, $max_width, $max_height);
$width = $img->getImageWidth();
$height = $img->getImageHeight();

var_dump($width, $height);

getSize 方法没有记录,它的返回值不是人们所期望的,所以要小心!

The getSize method is not documented and its return value is not the one one could expect, so be careful with that!