PHP GD - How can I modify my Thumbnail Creator to crop portrait images from the center?

Posted by frank on Stack Overflow See other posts from Stack Overflow or by frank
Published on 2010-05-21T04:17:27Z Indexed on 2010/05/21 4:20 UTC
Read the original article Hit count: 290

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about php

Related posts about gd