Jump to content

image resize


newbtophp

Recommended Posts

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?

 

:-\

Link to comment
https://forums.phpfreaks.com/topic/188794-image-resize/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.