Jump to content

[SOLVED] GD resized(smaller) image has larger filesize than original


Darkstar

Recommended Posts

I'm currently playing with the idea of resizing images on the fly for certain applications and my original thought process was that if I pass a smaller size image to the browser (or flash or anything else) it should load faster because it will theoretically be a smaller filesize as well.  I have a "full quality" 2000x2000px image and that's what I'd use as my base for resizing.  Somehow this 1.56MB file gets a filesize of ~1.8MB when it's sized down to 1800x1800 and at a mere 300x300 it's a still 1.6MB.

 

Has anybody else noticed this?  I tried setting the quality on imagejpeg() and this gave me a minimal change in size even when set to about 50 (even though it looked horrible).

Is there anyway you can convert to a GIF on the fly? or if you wanna risk compatibility convert to PNG.

 

(I've not messed with GD that much but i know for a fact GIF files or exponentially smaller than other formats)

I made this function

 

function resize_image($File, $NewWidth, $NewHeight, $Type)
{
header('Content-type: image/gif');

list($width, $height) = getimagesize($File);

if($Type == "ratio")
	{
	$Ratio = $NewWidth;
	$NewWidth = $width * $Ratio;
	$NewHeight = $height * $Ratio;
	}

$image_p = imagecreatetruecolor($NewWidth, $NewHeight);
$image = imagecreatefromgif($File);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $NewWidth, $NewHeight, $width, $height);


return($image_p);
}

 

$Type can either be "exact" or "ratio". If it's exact, the width and height will be exactly $NewWidth $NewHeight, if it's ratio then the current size will be multiplied by $NewWidth

Have you tried phpThumb()? It's awesome!

http://phpthumb.sourceforge.net/

 

I've taken 2MB full-size images and created thumbnails (and smaller representations) of it, and it loads WAY quicker than a 2MB file would. You can also set the quality, which affects speed.

I have looked at phpThumb and it does, in fact look awesome.

 

Well, folks, I'm very glad you all helped out so quickly but as it turns out it was just plain stupidity.  There was a rogue block of code (forgot the else around it because it's just 2 lines long) and it was outputting the full image after the resized image.  The browser would show me the resized image but still load the full image in the background.

 

My function is very similar to Garethp except in an attempt to save computing power I put an if block around the actual resizing portion of the function.  If the desired size is greater or equal to the current image size, simply display the image instead of bothering with GD.  The function was only to be used for downsizing images so I didn't have to worry about making them bigger.  Like I said, I forgot the else and it was ALWAYS outputting the full sized image but only showing the resized one.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.