Jump to content

Poor quality on thumbnail images


greens85

Recommended Posts

Hi all,

 

I am creating a thumbnail from an uploaded image (in this case a company logo).

 

The problem is that the thumbnail image is poor quality and not fit to display on the website!

 

I have attached the uploaded image and then then generated thumbnail, so you can see how much it is degrading.

 

I always want the thumb to be the same size, which is 129pxx44px.

 

I have included the thumb generation part of my script below:

 


// This is declared at the start of the script

$tdir = "images/agencylogos/thumbs/";
$twidth = "129";
$theight = "44";

// This is the actual part of the script that does the resizing

$simg = imagecreatefromjpeg($newname);   
		$currwidth = imagesx($simg);   
		$currheight = imagesy($simg);   
    	
		if ($currheight > $currwidth) {   
			$zoom = $twidth / $currheight;   
			$newheight = $theight;   
			$newwidth = $currwidth * $zoom;  
		} else {    
			$zoom = $twidth / $currwidth;   
			$newwidth = $twidth;   
			$newheight = $currheight * $zoom;  
		}
    
		$dimg = imagecreate($newwidth, $newheight); 
		imagetruecolortopalette($simg, false, 256);   
		$palsize = ImageColorsTotal($simg);

		imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);   

// Copy Resized Image To The New Image (So We Can Save It)
    
		imagejpeg($dimg, "$tdir" . $image_name, 100);
		imagedestroy($simg); 
		imagedestroy($dimg);

 

Can anyone see any ways of improving this script so that it would make the thumb image better quality?

 

Many thanks,

 

Greens85

 

[attachment deleted by admin]

Link to comment
Share on other sites

Hi there,

 

I am not sure  if the following is the reason for it's looks but here goes my thought:

 

your original image has the dimensions 349x118 and the created one 129x43

This isn't the correct if you would like to keep the original dimensions:

assuming the following.

Width:

349px | 129px

---=--------=----

100%  | 36.9627507%

Height:

129px | 44px

---=--------=----

100%  | 37.28813559322%    <---------- this is almost 0.4 % more than the percentage of the width.

 

as you can see the width and height differ in percentages assuming the starting point is 100 %

in other words your dimensions aren't correct. without looking at the code (because I am not this far in php) this probably is caused by rounding numbers

 

I am not sure how to do this yet in php, but I would try to get either a max width or a max height in pixels and use the percentage of that amount in adjusting the other dimension. That way you can still get the result you like within your design (which is either limited by height or width) without losing quality

Link to comment
Share on other sites

Hi,

 

Thanks for the replies!

 

@sastro

 

I've tried this but unfortunately it doesn't seem to have made any difference... assuming I have done it correctly which is:

 

imagecopyresampled($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); 

 

 

@fortnox007

 

I see your point about the ratios but I'm not sure I fully understand what you are suggesting... probably lack of knowledge on my part than your explanation!

 

I assume you are talking about getting the width and height of the original image when it's uploaded and resizing it according to ratio?

 

If so I would have no idea how to even begin this :(

Link to comment
Share on other sites

Hi,

 

thanks for your replies again!

 

@sastro

 

Unfortunately, that actually seemed to make the image worse  :(

 

@schilly

 

Your suggestion just left the image looking the same as it did with the other code :(

 

As for your script, it all seems good and seems to round which might help, but I'm not entirely sure how to integrate with mine... i.e. I need to thumbnail images from uploaded files but i notice you say your script doesn't do this...

 

Thanks for all your advice.

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.