Making an image smaller in size

I have a number of images, I want to reduce the size of them ( some are 2MB), but, I dont want to reduce the dimensions of the images. Is there a way to do this using PHP ?

Thanks in advance.

You can use imagemagick to strip the EXIF data etc. from the image; if you are working on jpg images jpg compression will take place. You can also save as a different quality.

exec("convert input.jpg -strip -quality 80 output.jpg");

Note: You could also change the density and resample but I have not tried this and am not sure what the effect would be.
If you do not have IM installed and want to know what the effect of the above command would be post a link to an image or send me a URL via PM.

Thats cool, thanks for that.

Bear in mind that playing with ImageMagick is not as easy as with playing with GD. You can simply enable GD using php.ini but for ImageMagick you have to install it.

Unless and until ImageMagick is installed you cannot run following command:

exec("convert input.jpg -strip -quality 80 output.jpg"); 


Doh, I cant install imagemagic, is there anyway to get GD to reduce the filesize of an image?

Here is a class that resizes the images i just modified for you which was originally written for different purposes.

class ResizeImage {
	public $image_to_resize;
	public $new_width;
	public $new_height;
	public $ratio;
	public $new_image_name;
	public $save_folder;
	public function __construct($nw, $nh, $src, $ratio = true){
		$this->new_width 	= $nw;
		$this->new_height 	= $nh;
		$this->image_to_resize = $src;
		$this->ratio 		= $ratio;
		$this->save_folder 	= true;
	public function resize($image_name = ''){
		$info = getimagesize($this->image_to_resize);
		$width 		= $info[0];
		$height 	= $info[1];
		$mime 		= $info['mime'];
			if (isset($this->new_width)):
				$factor = (float)$this->new_width / (float)$width;
				$this->new_height = $factor * $height;
			elseif (isset($this->new_height)):
				$factor = (float)$this->new_height / (float)$height;
				$this->new_width = $factor * $width;
				exit("New heidth and width have to be set.");
		// What sort of image?
		$type = substr(strrchr($mime, '/'), 1);
		switch ($type){
			case 'jpeg':
				$image_create_func = 'imagecreatefromjpeg';
				$image_save_func = 'imagejpeg';
				$new_image_ext = 'jpg';
			case 'png':
				$image_create_func = 'imagecreatefrompng';
				$image_save_func = 'imagepng';
				$new_image_ext = 'png';
			case 'bmp':
				$image_create_func = 'imagecreatefrombmp';
				$image_save_func = 'imagebmp';
				$new_image_ext = 'bmp';
			case 'gif':
				$image_create_func = 'imagecreatefromgif';
				$image_save_func = 'imagegif';
				$new_image_ext = 'gif';
				$image_create_func = 'imagecreatefromjpeg';
				$image_save_func = 'imagejpeg';
				$new_image_ext = 'jpg';
		$image_c = imagecreatetruecolor($this->new_width, $this->new_height);		
		$new_image = $image_create_func($this->image_to_resize);
		imagecopyresampled($image_c, $new_image, 0, 0, 0, 0, $this->new_width, $this->new_height, $width, $height);
		$save_path = basename($this->image_to_resize) . '_resized.' . $new_image_ext;
		$image_save_func($image_c, $save_path);
$src = "DSCF1697.JPG";
list($w, $h) = getimagesize($src);
$image = new ResizeImage($w, $h, $src);

I don’t guarantee for the quality of the resized images because you cannot expect the same quality as original image. Imagemagick can give a better quality.

The OP wants to reduce the filesize not the image size. the way I was doing this in ImageMagick was by removing the EXIF data and thumbnail.

Here is a link to the GD functions you may want to check out imagecopyresampled but I have not tried it.

Have searched google and the second item on this page is supposed to remove EXIF data but again I have not tried it:

Are you sure that IM is not installed already ?

Technically I am not sure whether my script removes the EXIF or not but I have also used the function ‘imagecopyresampled’ just to resize the image to the same size as original one and it reduces the size. Can you please try once and see the resized the image whether it does the work what OP wants?