student101 Posted April 4, 2009 Share Posted April 4, 2009 I hope this is the right section to post this topic /question? I have an image upload and resize function... I would like to know the proper ratio information to help resize the image and keep the proportions or aspect ratio. How does this work or How should it work? [code]$old_x=imagesx($src_img); //width $old_y=imagesy($src_img); //height $ratio1=$old_x/$new_h; $ratio2=$old_y/$new_w; if($ratio1>$ratio2){ $thumb_w=$new_w; $thumb_h=$old_y/$ratio1; }else{ $thumb_h=$new_h; $thumb_w=$old_x/$ratio2; } Quote Link to comment https://forums.phpfreaks.com/topic/152518-solved-ifratio1ratio2-how-do-you-know/ Share on other sites More sharing options...
Daniel0 Posted April 4, 2009 Share Posted April 4, 2009 You'll need to know within what size you need to be. $heightMax = 200; $widthMax = 200; $width = $widthOrig = imagex($src_img); $height = $heightOrig = imagey($src_img); if ($height > $heightMax) { $width = round(($heightMax / $height) * $width); $height = $heightMax; } if ($width > $widthMax) { $height = round(($widthMax / $width) * $height); $width = $widthMax; } echo "New size: {$width}x{$height}" . PHP_EOL; echo "Old size: {$widthOrig}x{$heightOrig}" . PHP_EOL; echo 'Ratio (before): ' . round($widthOrig/$heightOrig, 3) . PHP_EOL; echo 'Ratio (after): ' . round($width/$height, 3) . PHP_EOL; Quote Link to comment https://forums.phpfreaks.com/topic/152518-solved-ifratio1ratio2-how-do-you-know/#findComment-801095 Share on other sites More sharing options...
student101 Posted April 4, 2009 Author Share Posted April 4, 2009 Yes I have that in, you also added it in now... if ($height > $heightMax) { // WHY this $width = round(($heightMax / $height) * $width); // WHY this $height = $heightMax; } if ($width > $widthMax) { // WHY this $height = round(($widthMax / $width) * $height); // WHY this $width = $widthMax; } Quote Link to comment https://forums.phpfreaks.com/topic/152518-solved-ifratio1ratio2-how-do-you-know/#findComment-801101 Share on other sites More sharing options...
student101 Posted April 4, 2009 Author Share Posted April 4, 2009 Daniel0, I hope you understand what I mean... Why divide or multiply the widths and or heights? Quote Link to comment https://forums.phpfreaks.com/topic/152518-solved-ifratio1ratio2-how-do-you-know/#findComment-801107 Share on other sites More sharing options...
Daniel0 Posted April 4, 2009 Share Posted April 4, 2009 Well, if you make e.g. the width 20% smaller then you would need to make the height 20% smaller as well in order to retain the original proportions. Quote Link to comment https://forums.phpfreaks.com/topic/152518-solved-ifratio1ratio2-how-do-you-know/#findComment-801112 Share on other sites More sharing options...
student101 Posted April 4, 2009 Author Share Posted April 4, 2009 Ok, I see what you mean but don't fully understand it as such. Why not just use; $width = $width - 20%; $height = $height - 20%; Why the whole math system? $height = round(($widthMax / $width) * $height); $width = $widthMax; Is there like a hidden manual that only a select few have access to? Quote Link to comment https://forums.phpfreaks.com/topic/152518-solved-ifratio1ratio2-how-do-you-know/#findComment-801115 Share on other sites More sharing options...
Daniel0 Posted April 4, 2009 Share Posted April 4, 2009 Because you have to figure out what the growth rate is... I just picked 20% randomly, which would then have a growth rate of 0.8. The $widthMax / $width is that growth rate, and so you multiply it with the $height so the width and height will grow uniformly. Then you do the same again only with width and height switched. Quote Link to comment https://forums.phpfreaks.com/topic/152518-solved-ifratio1ratio2-how-do-you-know/#findComment-801130 Share on other sites More sharing options...
student101 Posted April 4, 2009 Author Share Posted April 4, 2009 Ok cool now it makes more sense, At least now I can make it do what I need to do. Thank you, I appreciate the time. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/152518-solved-ifratio1ratio2-how-do-you-know/#findComment-801145 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.