【问题标题】:Resize image using php Imagemagick使用 php Imagemagick 调整图像大小
【发布时间】:2012-07-09 18:37:42
【问题描述】:

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

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

【问题讨论】:

  • 取出“return $img”,因为它从此时停止脚本执行。它应该在之后工作。
  • @Diego 如果我这样做,我会收到错误 Warning: getimagesize(ÿØÿà) [function.getimagesize]: failed to open stream: No such file or directory in .............. on line 47
  • @DiegoAgulló 添加了一个函数,所以现在返回应该无关紧要.. 虽然同样的问题.. 函数后没有出现任何内容,但没有错误..
  • php.net/imagick.getsize 应该可以解决问题。
  • @DiegoAgulló Bingo。添加为答案并且不接受它

标签: php imagemagick


【解决方案1】:

此代码将起作用

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-24
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多