xface Posted January 6, 2007 Share Posted January 6, 2007 A friend wrote this code for me and it works in my application but the thumbnails that are displayed look stretched. I don't know how to do it but would like the images to be resized to just a width, keeping the hieght ratio intact. So like if I want my thumbs to be 133px wide, the height is adjusted automatically and the image isnt stretched. Here is the code:[code]function createthumbs($name,$filename,$new_w,$new_h) { global $extension; if(extension_loaded("gd2") || extension_loaded("gd")) { if($extension == "jpeg" || $extension == "jpg" || $extension == "gif" || $extension == "png"){ $system=explode(".",$name); if($extension == "jpeg" || $extension == "jpg"){ $src_img=imagecreatefromjpeg($name); } if($extension == "png"){ $src_img=imagecreatefrompng($name); } if($extension == "gif"){ $src_img=imagecreatefromgif($name); } $old_x=imageSX($src_img); $old_y=imageSY($src_img); $thumb_w=$new_w; $thumb_h=$new_h; $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); if ($extension == "png"){ imagepng($dst_img,$filename); } else { imagejpeg($dst_img,$filename); } imagedestroy($dst_img); imagedestroy($src_img); } } }[/code]Any help would be greatly appreciated!Thanks. Link to comment https://forums.phpfreaks.com/topic/33063-php-gd-help-please/ Share on other sites More sharing options...
EKINdesigns Posted January 6, 2007 Share Posted January 6, 2007 Give this a try:[code=php:0]if(($thumb_w < $old_x)){ $aspect_ratio = $thumb_w / $old_x; $height = round($old_y * $aspect_ratio); $width = $old_x;}[/code] Link to comment https://forums.phpfreaks.com/topic/33063-php-gd-help-please/#findComment-154118 Share on other sites More sharing options...
xface Posted January 6, 2007 Author Share Posted January 6, 2007 [quote author=EKINdesigns link=topic=121215.msg498056#msg498056 date=1168074647]Give this a try:[code=php:0]if(($thumb_w < $old_x)){ $aspect_ratio = $thumb_w / $old_x; $height = round($old_y * $aspect_ratio); $width = $old_x;}[/code][/quote]Cool, I pasted this to my exsisting code and now I get a fat red x. LOL I been playing around with it for a half hour changing this and that with no luck. Where would this code be pasted for it to work properly in the above code? Link to comment https://forums.phpfreaks.com/topic/33063-php-gd-help-please/#findComment-154279 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.