tags:

views:

156

answers:

3

Here is my current code:

$image  = 'img.jpg';
$source = imagecreatefromjpeg($image);

list($origWidth, $origHeight) = getimagesize($image);

$imgH   = 75;
$imgW   = $origWidth / $origHeight * $imgH;
$thumb  = imagecreatetruecolor($imgW, $imgH);

imagecopyresampled($thumb, $source, 0, 0, 0, 0, $imgW, $imgH, $origWidth, $origHeight);

This allows me to output an image with a fixed height of 75 pixels. What I would like to do is have a constant image size of 99x75 pixels. Portrait images that don't fit into this will be cropped from the center (so the center of the original remains the center of the thumbnail - if that makes sense).

How can I do this?

+2  A: 

Well, it's pure math. You want to achieve a 99x75 size, and you want to only cut from the width. So first, you resize to fit the height. That's what you did, but did it to fit a height of 75. Switch it to 99. Then you, check for the width to be <= 75. If it's not then you do this:

if( $imgW > 75 )
{
   $diff   = $imgw - 75; // this is what's extra
   $srcX   = ceil( $diff / 2 ); // this is where your img starts from
   $imgW   = 75;
}
imagecopyresampled($thumb, $source, 0, 0, $srcX, 0, $imgW, $imgH, $origWidth, $origHeight); // notice that src X changed accordingly.

So, if the width after the first "resize" is 100 and you wanted 75, you compute the diff = 25, split it by 2 and ceil it => 13, then you tell the GD function to start copying the image from 13, instead of 0, and still keep 75 height. This means it will copy from width 13 to witdh 88 => the center.

Hope this is what you wanted. Regards, Gabriel

Gabriel Poama-Neagra
A: 

(untested)

define('THUMB_WIDTH', 99);
define('THUMB_HEIGHT', 75);

$image  = 'img.jpg';
$source = imagecreatefromjpeg($image);
$thumb  = imagecreatetruecolor(THUMB_WIDTH, THUMB_HEIGHT);

$cutX = imagesx($source) > THUMB_WIDTH;
$cutY = imagesy($source) > THUMB_HEIGHT;

$source_x = $cutX ? imagesx($source) / 2 - (THUMB_WIDTH/2) : 0;
$source_y = $cutY ? imagesx($source) / 2 - (THUMB_HEIGHT/2) : 0;
$source_width = $cutX ? THUMB_WIDTH : imagesx($source);
$source_height = $cutY ? THUMB_HEIGHT : imagesy($source);

imagecopyresampled($thumb, $source, 0, 0, $source_x, $source_y, $source_width, $source_height, THUMB_WIDTH, THUMB_HEIGHT);
VolkerK
A: 
define( 'THUMB_WIDTH',  99 );
define( 'THUMB_HEIGHT', 75 );

$image = imagecreatefromjpeg('img.jpg');
$thumb = imagecreatetruecolor(THUMB_WIDTH, THUMB_HEIGHT);

list($image_width, $image_height) = getimagesize($image);
$x0 = ( $image_width  - THUMB_WIDTH  ) / 2;
$y0 = ( $image_height - THUMB_HEIGHT ) / 2;
imagecopy(
    $thumb,      // resource $dst_im
    $image,      // resource $src_im
    0,           // int $dst_x
    0,           // int $dst_y
    $x0,         // int $src_x
    $y0,         // int $src_y
    THUMB_WIDTH, // int $src_w
    THUMB_HEIGHT // int $src_h  
);

This code uses imagecopy function to copy 99x75px region from the source image. Source width - 99 / 2 returns the x coordinate from which to start copying, Source height - 75 / 2 returns the y coordinate. If you are interested in generating fixed size thumbnails from arbitrary size images, have a look at this article.

Salman A