newbtophp Posted January 17, 2010 Share Posted January 17, 2010 I'm having some trouble, im using the img bbcode on a forum page and a user page, the forum page layout is different too the user page, theirfore they have different widths etc. Currently the user can simply embed any image within the img bbcode, if the image is huge the webpage and layout will literally expload. (out of the width, destroying the layout). So i decided to use a image resize function to limit the image to a max width. If the image is on topic.php the max width is: 599px If the image is on user.php the max width is: 376px Heres the function: function scaleimage($location){ if($_SERVER['PHP_SELF'] == "user.php"){ $maxw=376; } else if($_SERVER['PHP_SELF'] == "topic.php"){ $maxw=599; } $maxh=200; $img = @getimagesize($location[1]); if($img){ $w = $img[0]; $h = $img[1]; $dim = array('w','h'); foreach($dim AS $val){ $max = "max{$val}"; if(${$val} > ${$max} && ${$max}){ $alt = ($val == 'w') ? 'h' : 'w'; $ratio = ${$alt} / ${$val}; ${$val} = ${$max}; ${$alt} = ${$val} * $ratio; } } $w = round($w); $h = round($h); return("<img src='{$location[1]}' width='{$w}' height='{$h}'>"); } } Usage: $Text = preg_replace_callback("/\[img\](.+?)\[\/img\]/", "scaleimage", $Text); The trouble is i have to currently define the height, how would i set it so it decides for it self and makes sure the height is within the proportion of the max width for the particular files; (topic.php and user.php), but staying appropriate at the same time? :-\ Quote Link to comment https://forums.phpfreaks.com/topic/188794-image-resize/ Share on other sites More sharing options...
RussellReal Posted January 17, 2010 Share Posted January 17, 2010 Quote Link to comment https://forums.phpfreaks.com/topic/188794-image-resize/#findComment-996868 Share on other sites More sharing options...
newbtophp Posted January 17, 2010 Author Share Posted January 17, 2010 Solved thanks RussellReal! Quote Link to comment https://forums.phpfreaks.com/topic/188794-image-resize/#findComment-996870 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.