0

我需要重新调整大小并将图像裁剪到中心。最终结果图像需要为 250W x 330H。

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

因此,如果它确实将大小重新调整为 330 高度并且宽度为 250 或更大,那么我需要将图像裁剪到宽度为 250 的中心。但如果它重新调整为 250 宽度,高度为 330 或更大,那么我需要将图像裁剪到高度为 330 的中心。

我试图自己创建它,但我对裁剪到中心部分感到很困惑。

4

3 回答 3

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);
?>
于 2013-03-07T01:55:30.363 回答
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');
于 2013-03-05T18:38:32.970 回答
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<,Imagemagick 6.8.8.1,Windows xampp。

于 2014-01-05T01:27:27.770 回答