Hi, I'm in the process of making a forum system for my clan website.
I'm currently making a function that convert bbcode tags to html tags ready for echoing in the div. Additionally, it will check all the images posted in forum post to see if they are over a certain width, and if they are, to apply a 'width=xxx' attribute to the img tag.
Here is the working function I have.
function bbcode($input) {
// changes all below bbcode to their repsective html partners as html is blocked in the web forms
$start_arr = array('[b]', '[/b]', '[u]', '[/u]', '[i]', '[/i]', '[img]', '[/img]', '[center]', '[/center]');
$finish_arr = array('<b>', '</b>', '<u>', '</u>', '<i>', '</i>', '<img src="', '"/>', '<center>', '</center>');
$replaced = str_replace($start_arr, $finish_arr, $input);
// does the input have a URL within image tags?
$matchcount = preg_match('!<img (?:.*?)src=([\'"])(http://[^ ]+)\\1(/){0,1}>!i', $replaced, $matches);
if($matchcount >= 1) { // yes
$urlWithtags = $matches[2]; $urlWithtags = str_replace('<img src="', '', $urlWithtags); // strip off <img src="
$urlNotags = str_replace('"/>', '', $urlWithtags); // strip off "/> we now have the raw URL of image
$imgsize = getimagesize($urlNotags); // Check if image is too wide
if($imgsize[0] > 777) { // too wide
$doneString = str_replace($urlNotags, ''.$urlNotags.'" width="777', $replaced);
return $doneString; // send back the new imgtag
} else { // not too wide, dont add size limit
$doneString = str_replace($urlNotags, '<img src="'.$urlNotags.'"/>', $replaced);
return $doneString; // send back the new imgtag
}
} else { // no
return $replaced; // send back original string, no image was found
}
}
Now, here is the part we need to focus on
$imgsize = getimagesize($urlNotags); // Check if image is too wide
if($imgsize[0] > 777) { // too wide
$doneString = str_replace($urlNotags, ''.$urlNotags.'" width="777', $replaced);
return $doneString; // send back the new imgtag
} else { // not too wide, dont add size limit
$doneString = str_replace($urlNotags, '<img src="'.$urlNotags.'"/>', $replaced);
return $doneString; // send back the new imgtag
}
It works absolutely fine, but will only resize the FIRST image posted in the post.
So, if someone posted 2 images that were too wide, then it would only resize the first one.
I'm not sure how to go about making this script check all the img tags in the post to check if they are too big..
Can anyone help, I really need it :)