Jump to content

[SOLVED] Cropping pictures with GD stretching problem


skyer2000

Recommended Posts

I have the following code to convert pictures into thumbnails. It creates the thumbnail to the size 200x150 perfectly, but the image is stretched horizontally. Anyone have ideas on how to fix this code so it will not stretch everything?

 

$max_width=200; 
$max_height=200; 
$add="../images/temp/$filename.jpg";
$tsrc="$dir/th$filename.jpg";
$im=ImageCreateFromJPEG($add);
$width=ImageSx($im);
$height=ImageSy($im); 

if ($width > $height) {
$src_w = $src_h = $width;
$src_x = 0;
$src_y = round(($height - $width) / 2);
} else {
$src_w = $src_h = $width;
$src_x = 0;
$src_y = round(($height - $width) / 2);
}

$newimage=imagecreatetruecolor(200,150);
imageCopyResized($newimage,$im,0,0,$src_x,$src_y,200,150,$src_w, $src_h);
ImageJpeg($newimage,$tsrc, 70);

If you want to avoid the stretched look you need to scale the image keeping the original proportions of width to height.

 

try

<?php
$max_width=200; 
$max_height=200; 
$add="../images/temp/$filename.jpg";
$tsrc="$dir/th$filename.jpg";
$im=ImageCreateFromJPEG($add);
$width=ImageSx($im);
$height=ImageSy($im); 

/**
* get the larger dimension and calc required scale factor
* to fit in a 200x200 square
*/
$size = max($width, $height);
$scale_factor = $max_width/$size;

$new_w = $width * $scale_factor;
$new_h = $height * $scale_factor;

$newimage=imagecreatetruecolor($new_w, $new_h);
imageCopyResampled($newimage,$im, 0, 0, 0, 0, $new_w, $new_h, $width, $height);
ImageJpeg($newimage,$tsrc, 70);
?>

Sure, you're just going to have to figure out if you need to crop from top and bottom or from left and right.

$lg_x = 200;
$lg_y = 150;
$src_img = imagecreatefromjpeg($ph_link);
$s_x = imagesx($src_img);
$s_y = imagesy($src_img);

$temp = imagecreatetruecolor($lg_x, $lg_y);

if($s_x / $s_y > $lg_x / $lg_y) {
//trim x
$x = round(($s_x - ($lg_x * $s_y / $lg_y)) / 2);
$y = 0;
} else if ($s_x / $s_y > $lg_x / $lg_y) {
//trim y
$x = 0;
$y = round(($s_y - ($lg_y * $s_x / $lg_x)) / 2);
} else {
$x = 0;
$y = 0;
}
//create large image
imagecopyresampled($temp, $src_img, 0, 0, $x, $y, $lg_x, $lg_y, $s_x, $s_y);
imagejpeg($temp, $filename);

You then have to work it the other way round and grab as big a part of the source image as you can while retaining a 200x150 size ratio

 

<?php
$t_width=200; 
$t_height=150; 
$add="../images/temp/$filename.jpg";
$tsrc="$dir/th$filename.jpg";
$im=ImageCreateFromJPEG($add);
$width=ImageSx($im);
$height=ImageSy($im); 

/**
* calc required scale factors
*/
$scale_h= $height/$t_height;
$scale_w= $width/$t_width;

/**
* get MIN scale factor
*/
$scale_factor = min($scale_w, $scale_h);

$src_w = $t_width * $scale_factor;
$src_h = $t_height * $scale_factor;

$newimage=imagecreatetruecolor($new_w, $new_h);
imageCopyResampled($newimage,$im, 0, 0, 0, 0, $t_width, $t_height, $src_w, $src_h);
ImageJpeg($newimage,$tsrc, 70);
?>

If his image is portrait format 1000x1500

 

then scale_w = 5, scale_h = 10 so I use the min value of 5.

 

This gives

 

imageCopyResampled($newimage,$im, 0, 0, 0, 0, 200, 150, 1000, 750); // top half of image

 

 

If OTOH it's landscape 2000x450

 

then scale_w = 10, scale_h = 3 so I use the min value of 3.

 

This gives

 

imageCopyResampled($newimage,$im, 0, 0, 0, 0, 200, 150, 600, 450); // left 600 pix of image

 

 

I'd say those were cropped.

 

I see what you're doing, we're just grabbing different parts of the image.

The code I pasted (which is why I was faster than you) was for thumbnailing portraits (head shots) so I always had to pull from the center of the image, so I didn't just grab the top of someone's head.

Good idea, in which case I just need to add a couple of lines

<?php

$t_width=200; 
$t_height=150; 
$add="../images/temp/$filename.jpg";
$tsrc="$dir/th$filename.jpg";
$im=ImageCreateFromJPEG($add);
$width=ImageSx($im);
$height=ImageSy($im); 

/**
* calc required scale factors
*/
$scale_h= $height/$t_height;
$scale_w= $width/$t_width;

/**
* get MIN scale factor
*/
$scale_factor = min($scale_w, $scale_h);

$src_w = $t_width * $scale_factor;
$src_h = $t_height * $scale_factor;

/**
* to centre the cropped area                                            //added code
*/
$sx = ($width - $src_w) / 2;
$sy = ($height - $src_h) / 2;

$newimage=imagecreatetruecolor($t_width, $t_height);       // edited
imageCopyResampled($newimage,$im, 0, 0, $sx, $sy, $t_width, $t_height, $src_w, $src_h);
ImageJpeg($newimage,$tsrc, 70);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.