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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.