peterbarone Posted March 3, 2006 Share Posted March 3, 2006 I would like to know :After I query my database and call a pic I want to make it 125X125. The loaded image is 400X400I have tried so many ways and keep loseing image quailty BAD any advise ? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 3, 2006 Share Posted March 3, 2006 Without seeing your code it's hard to help you...Here's some code that I use to get thumbnail pictures (final size of thumbs ~120 x 160 px):[code]<?php function makethumb($fn,$rt=true,$dbg=false) // $fn is the file containing the image // $rt -- true: return the thumbnail size, etc // false: display the thumbnail // $dbg -- debug on/off { if ($dbg) global $fp; $ok = false; if ($dbg) fwrite($fp,date('m/d/Y G:i a') . ' Getting thumbnail for ' . $fn . ' $rt: ' . var_export($rt,true) . "\r\n"); list($ow, $oh, $type, $attr) = getimagesize($fn); if ($dbg) fwrite($fp,date('m/d/Y G:i a') . ' Original width: ' . $ow . ' original height: ' . $oh . "\r\n"); $pct = 75; while (!$ok) { $th = $oh * ($pct/100); $tw = $ow * ($pct/100); if (($oh > $ow) && ($th <= 160 && $w <= 120)) $ok = true; if (($ow >= $oh) && ($tw <= 160 && $th <= 120)) $ok = true; if ($dbg) fwrite($fp,date('m/d/Y G:i a') . ' Percent: ' . $pct . ' Ok: ' . var_export($ok,true) . ' Computed width: ' . $tw . ' computed height: ' . $th . "\r\n"); $pct -= 1; if ($pct < 5) { $ok = true; $th = 120; $tw = 160; } } $w = round($tw); $h = round($th); $tn = imagecreatetruecolor($w, $h); $img = imagecreatefromjpeg($fn); imagecopyresampled($tn, $img, 0, 0, 0, 0, $w, $h, $ow, $oh); if ($rt) { if ($dbg) fwrite($fp, date('m/d/Y G:i a') . ' Returning type: ' . $type . ' width: ' . $w . ' height: ' . $h . "\r\n"); return (array($tn, $type, $w, $h)); } else { if ($dbg) fwrite($fp, date('m/d/Y G:i a') . ' Writing mime type: ' . image_type_to_mime_type($type) . "\r\n"); header('Content-type: ' .image_type_to_mime_type($type)); imagejpeg($tn,'tmp.tmp'); readfile('tmp.tmp'); } }?>[/code]Of course if the images come from a fairly recent digital camera, most of those images have thumbnails embedded in the EXIF data and you can use the [a href=\"http://www.php.net/exif_thumbnail\" target=\"_blank\"]exif_thumbnail[/a]() function to extract them.Ken Quote Link to comment Share on other sites More sharing options...
peterbarone Posted March 4, 2006 Author Share Posted March 4, 2006 [!--quoteo(post=351438:date=Mar 3 2006, 04:52 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Mar 3 2006, 04:52 PM) [snapback]351438[/snapback][/div][div class=\'quotemain\'][!--quotec--]Without seeing your code it's hard to help you...Here's some code that I use to get thumbnail pictures (final size of thumbs ~120 x 160 px):[code]<?php function makethumb($fn,$rt=true,$dbg=false) // $fn is the file containing the image // $rt -- true: return the thumbnail size, etc // false: display the thumbnail // $dbg -- debug on/off { if ($dbg) global $fp; $ok = false; if ($dbg) fwrite($fp,date('m/d/Y G:i a') . ' Getting thumbnail for ' . $fn . ' $rt: ' . var_export($rt,true) . "\r\n"); list($ow, $oh, $type, $attr) = getimagesize($fn); if ($dbg) fwrite($fp,date('m/d/Y G:i a') . ' Original width: ' . $ow . ' original height: ' . $oh . "\r\n"); $pct = 75; while (!$ok) { $th = $oh * ($pct/100); $tw = $ow * ($pct/100); if (($oh > $ow) && ($th <= 160 && $w <= 120)) $ok = true; if (($ow >= $oh) && ($tw <= 160 && $th <= 120)) $ok = true; if ($dbg) fwrite($fp,date('m/d/Y G:i a') . ' Percent: ' . $pct . ' Ok: ' . var_export($ok,true) . ' Computed width: ' . $tw . ' computed height: ' . $th . "\r\n"); $pct -= 1; if ($pct < 5) { $ok = true; $th = 120; $tw = 160; } } $w = round($tw); $h = round($th); $tn = imagecreatetruecolor($w, $h); $img = imagecreatefromjpeg($fn); imagecopyresampled($tn, $img, 0, 0, 0, 0, $w, $h, $ow, $oh); if ($rt) { if ($dbg) fwrite($fp, date('m/d/Y G:i a') . ' Returning type: ' . $type . ' width: ' . $w . ' height: ' . $h . "\r\n"); return (array($tn, $type, $w, $h)); } else { if ($dbg) fwrite($fp, date('m/d/Y G:i a') . ' Writing mime type: ' . image_type_to_mime_type($type) . "\r\n"); header('Content-type: ' .image_type_to_mime_type($type)); imagejpeg($tn,'tmp.tmp'); readfile('tmp.tmp'); } }?>[/code]Of course if the images come from a fairly recent digital camera, most of those images have thumbnails embedded in the EXIF data and you can use the [a href=\"http://www.php.net/exif_thumbnail\" target=\"_blank\"]exif_thumbnail[/a]() function to extract them.Ken[/quote]wow that is aot of code for a thumbnail !!! But thanks and yes all pics are from a good quality digital camra. So I guess that I'll do some reasearch on exif_thumbnail() that sounds like what I could be looking for Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 4, 2006 Share Posted March 4, 2006 I may have extra code because I may not know the original size of the picture and I always wanted the thumbnails to be approximately the same size, so I reduce the width and height by a decreasing percentage. You can see it in action at [a href=\"http://www.ny-njdogtrainer.com/gallery.php\" target=\"_blank\"]http://www.ny-njdogtrainer.com/gallery.php[/a] and [a href=\"http://www.rutgerspromenaders.org/pictures/\" target=\"_blank\"]http://www.rutgerspromenaders.org/pictures/[/a] (click on the menus to the left -- not all have pictures yet.)Ken 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.