General Web Optimisation

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
davidb2002
Posts: 37
Joined: 2008-09-01T08:31:26-07:00

General Web Optimisation

Post 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 :)
tk1
Posts: 17
Joined: 2011-07-27T05:42:54-07:00
Authentication code: 8675308
Location: Poland

Re: General Web Optimisation

Post 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.
cheers
tk1
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: General Web Optimisation

Post 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/
Post Reply