3

我正在尝试使用 imagemagick 的函数“thumbnailImage”来调整图像的大小。现在,我之后没有对图像做任何事情,只是呼应新的尺寸,看看它是否有效。到目前为止,它不起作用。这是我的代码。注意:它确实与原始尺寸相呼应,而不是新尺寸。

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

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

4

2 回答 2

3

此代码将起作用

$image = $_FILES["file"]["tmp_name"];  

从你想要的任何地方获取文件等等。你正在使用一个有返回值的函数,但从来没有设置一个 var 让它返回。您还需要保存文件才能使用 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">
于 2015-07-03T02:07:31.983 回答
0

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

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

var_dump($width, $height);

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

于 2012-07-09T19:27:56.760 回答