Jump to content

downsizing an image size if over x


pea

Recommended Posts

Hello, i have a shoutbox which preg_replaces bbcode to html. What i'm trying to do is reduce an images size if it's too big. I came up with this:

[code]$imagepath = preg_replace("/\[img\](.+?)\[\/img\]/", "\\1", $c);

$imageInfo = getimagesize($imagepath);

$width = $imageInfo[0];
$height = $imageInfo[1];

if ($width >= 1300){
$width = (20 / 100) * $width;
$height = (20 / 100) * $height;
} else {
$width = $width;
$height = $height;
}

$c = preg_replace('/\[img\]/', '<a href="' . $imagepath . '" target="_blank" border="0"><img src="', $c);
$c = preg_replace('/\[\/img\]/', '" width="' . $width . '" height="' . $height . '"></a>', $c);[/code]

But for some reason it's putting the whole post into the getimagesize function, it works ok when there's only an image in the post. So for example if the post was 'test [ímg]http://www.example.com/image.gif[/ímg]' it would complain about not being able to find 'test http://www.example.com/image.gif'.

To me the script looks like it should work, can anyone else spot a fault?
Link to comment
https://forums.phpfreaks.com/topic/33233-downsizing-an-image-size-if-over-x/
Share on other sites

I am not an expert in regular expresion, so I am used to doing things the manual way, try this:

[code]
while(($start = strpos("[img]",$c)) !== false)
{
    $end = strpos("[\/img]",$c,$start);
    $start += strlen("[img]");

    $imagepath = substr($c,$start,$end-$start);

    //Put the rest of your code here.
}
[/code]

This should be able to replace every image in the message with a scaled image, as desired.

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.