By compress, do you mean reduce the dimensions of the picture?
If so, here's a function taken from a user-contributed note on PHP.net I've used before that you can play around with:
function fastimagecopyresampled (&$dst_image, &$src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $quality = 1) {
// Plug-and-Play fastimagecopyresampled function replaces much slower imagecopyresampled.
// Just include this function and change all "imagecopyresampled" references to "fastimagecopyresampled".
// Typically from 30 to 60 times faster when reducing high resolution images down to thumbnail size using the default quality setting.
// Author: Tim Eckel - Date: 12/17/04 - Project: FreeRingers.net - Freely distributable.
//
// Optional "quality" parameter (defaults is 3). Fractional values are allowed, for example 1.5.
// 1 = Up to 600 times faster. Poor results, just uses imagecopyresized but removes black edges.
// 2 = Up to 95 times faster. Images may appear too sharp, some people may prefer it.
// 3 = Up to 60 times faster. Will give high quality smooth results very close to imagecopyresampled.
// 4 = Up to 25 times faster. Almost identical to imagecopyresampled for most images.
// 5 = No speedup. Just uses imagecopyresampled, highest quality but no advantage over imagecopyresampled.
if (empty($src_image) || empty($dst_image)) { return false; }
if ($quality <= 1) {
$temp = imagecreatetruecolor ($dst_w + 1, $dst_h + 1);
imagecopyresized ($temp, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w + 1, $dst_h + 1, $src_w, $src_h);
imagecopyresized ($dst_image, $temp, 0, 0, 0, 0, $dst_w, $dst_h, $dst_w, $dst_h);
imagedestroy ($temp);
} elseif ($quality < 5 && (($dst_w * $quality) < $src_w || ($dst_h * $quality) < $src_h)) {
$tmp_w = $dst_w * $quality;
$tmp_h = $dst_h * $quality;
$temp = imagecreatetruecolor ($tmp_w + 1, $tmp_h + 1);
imagecopyresized ($temp, $src_image, $dst_x * $quality, $dst_y * $quality, $src_x, $src_y, $tmp_w + 1, $tmp_h + 1, $src_w, $src_h);
imagecopyresampled ($dst_image, $temp, 0, 0, 0, 0, $dst_w, $dst_h, $tmp_w, $tmp_h);
imagedestroy ($temp);
} else {
imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
}
return true;
}
It's pretty self-explanatory... the first two parameters should be image resources... such as those returned by [man]imagecreatetruecolor/man and whatnot.
Also note that you may need to adjust the memory that PHP is allowed to use, as a 4MB photo can take up several times that amount when loaded (uncompressed) into memory; for this, I used a function (again taken from PHP.net) to dynamically adjust the memory limit just before I used the image() function to load it:
function setMemoryForImage( $filename ){
$imageInfo = getimagesize($filename);
$MB = 1048576; // number of bytes in 1M
$K64 = 65536; // number of bytes in 64K
$TWEAKFACTOR = 1.8; // Or whatever works for you
$memoryNeeded = round( ( $imageInfo[0] * $imageInfo[1]
* $imageInfo['bits']
* $imageInfo['channels'] / 8
+ $K64
) * $TWEAKFACTOR
);
$memoryLimitMB = ini_get('memory_limit');
$memoryLimit = $memoryLimitMB * $MB;
if (function_exists('memory_get_usage') &&
memory_get_usage() + $memoryNeeded > $memoryLimit)
{
$newLimit = $memoryLimitMB + ceil( ( memory_get_usage()
+ $memoryNeeded
- $memoryLimit
) / $MB
);
ini_set( 'memory_limit', $newLimit . 'M' );
return true;
} else {
return false;
}
}