Tyler Hughes
Tyler Hughes

Reputation: 612

PHP: Image Resize and Crop to Portrait

I need to the re size and crop image to center. The end result image needs to be 250W x 330H.

I need to re size the image uploaded to 330 height but leave the correct ratio with the width. Then check to see if width is 250 or over, after the re size. If it is not, then I need to re size the image from the original to 250 width but leave the correct ratio with the height.

So if it did re size to 330 height and the width was 250 or over, then I need to crop the image to the center on width to 250. But if it re sized to 250 width, with height being 330 or over, then I need to crop image to the center on height to 330.

I was trying to create it myself but I'm so confused by the crop to center part.

Upvotes: 0

Views: 2785

Answers (3)

FstaRocka
FstaRocka

Reputation: 278

here is a function ive just completed to force an exact pixel size - I cant guarantee it 100% but ive tested it with many options and got perfect results so far, it gives the closest result imo. First it resizes the SMALLEST difference between the source image and specified sizes by calculating ratios. Then trims off excess pixels. I have compensated for odd numbers, negative values, etc. I have had good results so far. Please let me know if ive missed something or if it breaks somehow:

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.' ');
            }
        }



    }

Feel free to use / make comments. Php 5.4<, Imagemagick 6.8.8.1, Windows xampp.

Upvotes: 0

Jarrod
Jarrod

Reputation: 9465

I wrote a library to can do just that: 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);
?>

Upvotes: 1

Stormherz
Stormherz

Reputation: 376

With the use of Wideimage library (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');

Upvotes: 1

Related Questions