General Web Optimisation
Posted: 2012-05-03T02:16:53-07:00
I manage a photography website which has a user gallery. When someone uploads a photo we check if it is the correct size. If it isn't we resize it down using the following code:
When a photo is uploaded at the correct size, we just use php's copy() function. We are wondering if there is something we can do with these correctly sized photos to decrease the filesize without comprimising quality. Any suggestions welcome
Code: Select all
//don't touch density
$set_density = 0;
$image = new Imagick($temp);
if($square == 0) {
$image->scaleImage($width,$height,true);
}
else {
//so we are doing a sqaure image
//we need to scale down first - so whatever is smaller we need to shink that down and reduce the other as well by the same ratio
$image->scaleImage($width,$width);
$image->cropImage($width,$width,0,0);
}
//take the array and work out all the bits if given
for($i=0;$i<count($options);$i++) {
if($options[$i] == 'flatten') {
$image->flattenImages();
}
else if($options[$i] == 'strip_exif') {
//try and remove profiles
try {
$image->profileImage('exif',NULL);
$image->profileImage('xmp',NULL);
$image->profileImage('iptc',NULL);
}
catch(Exception $e) {
//catch any exception
}
}
else if($options[$i] == 'unsharp') {
$image->unsharpMaskImage(0,0.5,1,0.05);
}
else if($options[$i] == 'density') {
$set_density = 1;
}
else if($options[$i] == 'strip') {
$image->stripImage();
}
}
$image->setImageCompressionQuality($quality);
//set density
if($set_density == 1) {
$image->setImageResolution(72,72);
}
$image->writeImage($new);