greens85 Posted October 19, 2010 Share Posted October 19, 2010 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] Quote Link to comment Share on other sites More sharing options...
fortnox007 Posted October 19, 2010 Share Posted October 19, 2010 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 Quote Link to comment Share on other sites More sharing options...
sastro Posted October 19, 2010 Share Posted October 19, 2010 use imagecopyresampled instead imagecopyresized Quote Link to comment Share on other sites More sharing options...
greens85 Posted October 19, 2010 Author Share Posted October 19, 2010 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 Quote Link to comment Share on other sites More sharing options...
sastro Posted October 19, 2010 Share Posted October 19, 2010 Try to remove imagetruecolortopalette($simg, false, 256); $palsize = ImageColorsTotal($simg); Quote Link to comment Share on other sites More sharing options...
schilly Posted October 19, 2010 Share Posted October 19, 2010 Try this: $dimg = imagecreatetruecolor($newwidth, $newheight); imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); You can also see my script here: http://www.damnsemicolon.com/php/dynamically-create-thumbnails-in-php-with-gd Quote Link to comment Share on other sites More sharing options...
greens85 Posted October 20, 2010 Author Share Posted October 20, 2010 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. Quote Link to comment Share on other sites More sharing options...
greens85 Posted October 20, 2010 Author Share Posted October 20, 2010 I have solved this via a thread on another forum... In the hope of helping others with the same problem, here it is: http://www.phpbuilder.com/board/archive/index.php/t-10371811.html Many thanks for everyones help and advice, Greens85 Quote Link to comment Share on other sites More sharing options...
schilly Posted October 20, 2010 Share Posted October 20, 2010 so it was a scaling issue? Quote Link to comment Share on other sites More sharing options...
greens85 Posted October 21, 2010 Author Share Posted October 21, 2010 Hi Schilly, According the guy who found the solution, the issue was: "I hadn't used the correct formula to ensure that the aspect ratio was maintained when resizing." So I guess it was an aspect ratio/resizing issue yeah. Thanks, Greens85 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.