PHP:图像调整大小并裁剪为纵向

时间:2013-03-05 18:17:43

标签: php image resize crop

我需要将尺寸和裁剪图像放在中心位置。最终结果图像需要为250W x 330H。

我需要调整上传到330高度的图像大小,但保留正确的宽度比例。然后在重新调整大小后检查宽度是否为250或更高。如果不是,那么我需要将图像从原始图像重新调整为250宽度,但保留正确的高度比率。

因此,如果尺寸达到330高度且宽度为250或更高,那么我需要将图像裁剪到宽度为250的中心。但是如果尺寸设置为250宽度,则高度为330或更高,然后我需要将图像裁剪到高度为330的中心。

我试图自己创造它,但我对作物到中心部分感到困惑。

3 个答案:

答案 0 :(得分:1)

使用Wideimage库(http://wideimage.sourceforge.net/):

$thumb = WideImage::load('uploaded_image.png')->resize(250, 330);
if ($thumb->getWidth() > 250 || $thumb->getHeight() > 330) {
    $thumb = $thumb->crop('center', 'center', 250, 330);        
}
$thumb->saveToFile('cropped_image.png');

答案 1 :(得分:1)

我写了一个库可以做到这一点: Php Image Magician

<?php
    require_once('../php_image_magician.php');

    $magicianObj = new imageLib('racecar.jpg');
    $magicianObj -> resizeImage(250, 330, 'crop');
    $magicianObj -> saveImage('racecar_cropped.jpg', 100);
?>

答案 2 :(得分:0)

这是一个刚刚完成的功能,以强制精确的像素大小 - 我不能保证它100%但我已经测试了很多选项,并得到了完美的结果到目前为止,它给出了最接近的结果imo。首先,它通过计算比率来调整源图像和指定大小之间的最小差异。然后修剪掉多余的像素。我已经补偿了奇数,负值等等。到目前为止,我已经取得了很好的成绩。如果我错过了什么或者它是否以某种方式破坏,请告诉我:

PHP:

    // set source/export paths and pixel sizes for final sizes
    $src="path/to/source.jpg";
    $exp="path/to/output.jpg";
    $crop_w=300;
    $crop_h=200;


    $size = getimagesize("$src");

    //check image sizes
    if( ($size[0] < $crop_w) || ($size[1] < $crop_h) ){
    echo 'Image not big enough to crop';
    exit();
    }

    //get differential ratios of image vs crop sizes - 
        //smaller ratio must be resized
    $ratio_w = $size[0]/$crop_w;
    $ratio_h = $size[1]/$crop_h; 

    //square or landscape - shave sides

    if($ratio_w >= $ratio_h){

        //resize width / shave top&bottom
        exec("convert $src -resize x".$crop_h." $exp ");
        $size = getimagesize("$exp");
        $diff=abs($crop_w-$size[1]);

        //dividing 1 by 2 will leave a zero on round down - just force resize
        if($diff < 2){

        // skip shave - diff too small
        exec('convert $exp -resize  '.$crop_h.'X! $exp ');
        }
        else{
            //divide difference by 2 for shave amount
            $shave = round($diff/2,0,PHP_ROUND_HALF_DOWN); //halve & round difference down to avoid cropping smaller

            exec('convert '.$exp.' -shave  '.$shave.'x0 '.$exp.' '); //shave sides

            //odd $diff leave a rounded down pixel - force height resize 
            if($diff%2 !==0){//if $diff was not divisible by two then 1 pixel is left from round down
            exec('convert '.$exp.' -resize  '.$crop_w.'x! '.$exp.' ');
            }
        }
    }

    //portrait - shave height
    else{

        //resize width / shave top&bottom
        exec("convert $src -resize ".$crop_w."x $exp ");
        $size = getimagesize("$exp");
        $diff=abs($crop_h-$size[1]);

        //dividing 1 by 2 will leave a zero on round down - just force resize
        if($diff < 2){

        exec('convert $exp -resize  x'.$crop_h.'! $exp ');
        }
        else{
            //divide difference by 2 for shave amount
            $shave = round($diff/2,0,PHP_ROUND_HALF_DOWN); //halve & round difference down to avoid cropping smaller

            exec('convert '.$exp.' -shave  0x'.$shave.' '.$exp.' '); //shave sides

            //odd $diff leave a rounded down pixel - force height resize 
            if($diff%2 !==0){//if $diff was not divisible by two then 1 pixel is left from round down

            exec('convert '.$exp.' -resize  x'.$crop_h.'! '.$exp.' ');
            }
        }



    }

随意使用/发表评论。 Php 5.4&lt;,Imagemagick 6.8.8.1,Windows xampp。