PHP: create thumbnail with cropped rectangle

I am trying to resize and crop the image with one @imagecopyresampledthat supports the original aspect ratio.

The idea is this: 1) I fix the size of the thumbnail (i.e. 300x40) 2) Harvest starting at the center of height

enter image description hereenter image description here

I tried to read the documentation and many other questions about stackoverflow, but with no result. Can anybody help me? My actual code is as follows:

//$img_height, $img_width [original size of the image]

$thumb_width  = 300;
$thumb_height = 40;
$new_img = @imagecreatetruecolor($thumb_width, $thumb_height);
$middle = floor($img_height/2);

$src_x = 0;
$src_y = $middle-($thumb_width/2);

$src_w = $img_width;
$aspectRatio = $img_width/$thumb_width;

//$src_h = ?????

$imgCopyRes = @imagecopyresampled(
                  $new_img, $src_img,
                  0, 0, 
                  $src_x, $src_y, 
                  $thumb_width, $thumb_height, 
                  $src_w, $src_h);

EDIT:

Thank you so much @Joshua Burns, after reading your class and editing my code, I found a solution, not including the whole file.

The code:

$target_width  = 300;
$target_height = 40;
$new_img = @imagecreatetruecolor($target_width, $target_height);

$width_ratio  = $target_width  / $img_width;
$height_ratio = $target_height / $img_height;
if($width_ratio > $height_ratio) {
    $resized_width  = $target_width;
    $resized_height = $img_height * $width_ratio;
} else {
    $resized_height = $target_height;
    $resized_width  = $img_width * $height_ratio;
}
// Drop decimal values
$resized_width  = round($resized_width);
$resized_height = round($resized_height);

// Calculations for centering the image
$offset_width  = round(($target_width  - $resized_width) / 2);
$offset_height = round(($target_height - $resized_height) / 2);

$imgCopyRes = @imagecopyresampled(
                  $new_img, $src_img, 
                  $offset_width, $offset_height, 
                  0, 0, 
                  $resized_width, $resized_height, 
                  $img_width, $img_height);  
+5
source share
3 answers

Well, it's probably a little bloated for your needs, but it does its job, and all is well.

, PHP-: http://pastebin.com/dnmiUVmk

:

<?php
// Replace 'picture' w/ whatever the name of the file upload.
// Alternately, specify an absolute path to an image already on the server.
$upload_image_tmp_filename = $_FILES['picture']['tmp_name'];
$saveas_image_filename = 'my_resized_image.png';
$max_width = 300;
$max_height = 40;

// If there was a problem, an exception is thrown.
try {
    // Load the image
    $picture = New Image($upload_image_tmp_filename);
    // Save the image, resized.
    $picture->saveFile($saveas_image_filename, $max_width, $max_height, True);
} catch(Exception $e) {
    print $e->getMessage();
}
+2

Imagick (http://www.php.net/manual/en/class.imagick.php)

//instantiate the image magick class
$image = new Imagick($image_path);

//crop and resize the image
$image->cropThumbnailImage(100,100);

// save
$image->writeImage($your_file);
0

You can use the following code to calculate cropping parameters:

$target_width = 300;
$target_height = 40;
$test_case = array(
    array(233,350),
    array(350,233),
    array(300,40)
);
foreach ($test_case as $test) {
    list($source_width, $source_height) = $test;
    $source_ar = $source_width / $source_height;
    $target_ar = $target_width / $target_height;
    if ($source_ar > $target_ar) {
        $temp_height = $target_height;
        $temp_width = (int) ($target_height * $source_ar);
    } else {
        $temp_width = $target_width;
        $temp_height = (int) ($target_width / $source_ar);
    }
    $temp_crop_hori = (int) (($temp_width - $target_width) / 2);
    $temp_crop_vert = (int) (($temp_height - $target_height) / 2);
    echo "==================
source image: ${source_width}x${source_height}
temp image:   ${temp_width}x${temp_height}
target image: crop ${target_width}x${target_height} from ${temp_crop_hori}, ${temp_crop_vert}
";
}

He prints:

==================
source image: 233x350
temp image:   300x450
target image: crop 300x40 from 0, 205
==================
source image: 350x233
temp image:   300x199
target image: crop 300x40 from 0, 79
==================
source image: 300x40
temp image:   300x40
target image: crop 300x40 from 0, 0

You can use this information in:

  • imagecopyresampledto resize an image 233x350 to 300x450 .
  • imagecopyto copy 300x40 part of 300x450 starting at 0, 205
0
source

All Articles