Page 1 of 1

General Web Optimisation

Posted: 2012-05-03T02:16:53-07:00
by davidb2002
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:

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);
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 :)

Re: General Web Optimisation

Posted: 2012-09-02T10:50:27-07:00
by tk1
If your site maintains photos for web viewing only then you may consider to reduce resolution. Most computer screens are able to display anything in 72 dpi. Most cameras make photos in bigger resolution (300 dpi or more). See Imagick::setImageResolution and Imagick::setResolution. Reducing resolution significantly reduces image sizes.

Re: General Web Optimisation

Posted: 2012-09-02T10:58:01-07:00
by fmw42
Setting the density won't help the file size.

I may have missed it but I do not see what output image format you are using? JPG, PNG, other?

If JPG, all you can do is use -quality and find a a tolerable result for the file size. If you are using PNG, you can save to PNG8: format so that it is 8-bit palette rather than 24-bit color.

Other tools besides IM may optimize different format better than IM. see http://www.imagemagick.org/Usage/formats/