pea Posted January 7, 2007 Share Posted January 7, 2007 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 More sharing options...
cshireman Posted January 8, 2007 Share Posted January 8, 2007 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. Link to comment https://forums.phpfreaks.com/topic/33233-downsizing-an-image-size-if-over-x/#findComment-155633 Share on other sites More sharing options...
pea Posted January 8, 2007 Author Share Posted January 8, 2007 Not sure how that works, but if it scales every image, wouldn't it scale down images that are small anyway? I need it to downsize if the image is going to push the page out of shape. Link to comment https://forums.phpfreaks.com/topic/33233-downsizing-an-image-size-if-over-x/#findComment-155721 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.