duangua5742 2011-03-01 17:23
浏览 69
已采纳

将全景图像调整为固定大小

I want to resize the images to fixed width and height (i.e. 150px). However, theres a problem, if there is lots of difference in height and width of original photo (for example, panoramic photo), the resized thumbnail looks bad. Is there any any smart solution to resize the photos to a fixed width and height? For example, please have a look at this image: enter image description here

Here's my code:

<?php
    $params = getimagesize($tempFile);
    $width = $params[0];
    $height = $params[1];

    $newwidth=150;
    $newheight= 150;
    $tmp=imagecreatetruecolor($newwidth,$newheight);

    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
    imagejpeg($tmp,$img_name,80);

    imagedestroy($src);
    imagedestroy($tmp); 
?>

Is there any smart way to resize the images in smart way? Thanks.

  • 写回答

4条回答 默认 最新

  • dongya5893 2011-03-01 17:26
    关注

    There's a smart solution, it's called Seam Carving, and if your server supports ImageMagick, you do it like this:

    <?php
    $im = new Imagick( 'image.jpg' );
    $im->liquidRescaleImage( 600, 100, 3, 25 );
    header( 'Content-Type: image/jpg' );
    echo $im;
    ?>
    

    Or alternatively, if it doesn't support, use exec() (carefully) in order to pass image as an argument to executable which can perform seam carving.

    BTW it looks like twitpic just crop's the squared image extract. In one of my previous projects I used following code:

    if ($image->width > $image->height){
        //crop image in proportions 4/3, then resize to 500x300 (or proportionally lower resolution), 
        //sharp it a little and decrease quality. 
        //I used one of the Yii framework extensions.
        $image->crop($image->width, $image->width/4*3)->resize(500, 300, Image::WIDTH)->sharpen(15)->quality(75);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?