tags:

views:

67

answers:

2

I want to be able to trim images, many of which are very long vertically...anywhere from 2000 to 4000px, always at 800. So only getting the top part of the image. I then want to output this to a page/report with PHP, without storring the resultant trimmed image.

Is $imagepng->trim the best way to do this?

+1  A: 

You'd do something like this:

$srcName = 'source.png';

$info = getimageinfo($srcName);
$src = imagecreatefrompng($srcName);

// Create a new image up to 800px tall
$dest = imagecreate($info[0], min($info[1], 800));
imagecopy($dest, $src, 0, 0, 0, 0, $info[0], min($info[1], 800));

// Output
header('Content-type: image/png');
imagepng($dest);
Greg
A: 

GD is what imagepng uses and it's the most widely-supported way of doing image manipulation in PHP, so it's a pretty safe bet, especially if you are looking to deploy your code on servers you don't control.

An alternative would be to look at ImageMagick, though I find GD is a little faster in most cases.

alxp