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).

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.