php - 使用 php Imagemagick 调整图像大小

标签 php imagemagick

我正在尝试使用 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/

相关文章:

php - 如何从 php 将参数传递给 windows 命令行程序中的可执行文件?

php - 未捕获的异常 'PDOException',消息为 'SQLSTATE[HY093]: Invalid parameter number'

macos - 使用 ImageMagick 为图像批量添加空白

xcode - xcode-select --install失败

php - 使用 htaccess 删除尾部斜杠

php - Messenger bot - 定时响应

javascript - CKeditor 在粘贴事件时删除除 div、span 之外的所有 html 标签

ruby-on-rails - 如何对图像回形针进行像素化

imagemagick - 如何在 imagemagick 中将 1 个调整大小命令和 3 个复合命令合并为一个命令?

ruby - 使用 Ruby 和 imagemagick 获取或计算图像的熵