View Full Version : Few questions concerning strings

03-19-2004, 11:11 PM
Okay, l've got a few problems l can't seem to figure out myself. So l thought l'd give some of you guys a crack at it. I'd like to add a customized javascript to my phpBB to prevent people from posting images that exceed a height/width of 500.

So let's say someone is attempting to post the following message on my board:

"hello <img>http://www.VERY_LARGE_IMAGE.com/big.jpg</img> everyone <img>hTTP://www.ANOTHER_BIG_IMAGE.com</img>!!"

So l need the script to do a couple things.

1.) read thru the message that they are posting, and create a string out of it.

function verifyImageSize(){
msg = document.post.message.value;
s = new String(msg);

2.) l want it to search the string, and find all instances of <img> and </img>, and assign a variable to all of the URL(s) between each instance of <img>, and </img> tags. That l don't know how to do.

aa = new Image()

3.) next l want it to check the height/width dimensions of the image URL(s), and if they're too big... replace them with a url of my choosing.

imageReplacement = "http://www.smaller_image.com/small.gif";

if(aa.width => "400"){
s = s.replace(???,imageReplacement);

if(aa.height => "400"){
s = s.replace(???,imageReplacement);


I don't know how to write the script to find all instances of <img> and </img> in a string. I don't know how to make it assign a variable to each of the URLs that are inbetween each set of tags that it finds, and l don't know how to have it check the height/width of each instance that it finds. Any help would be greatly appreciated!


Willy Duitt
03-20-2004, 03:31 AM
You will need to write a regular expression to .match and/or .test your string.
However, try these codes in regards to finding and verifying the image sizes.

<BASE HREF="http://www.andrewgray.com/">

<script type="text/javascript">
function chkImage(imgURL){
var img = new Image();
img.src = imgURL;
img.onload=function chkSize(imgURL){
if(this.width<=325 && this.height<=225){
alert('Good Image: '+this.width + 'x' + this.height);
if(this.width>325 || this.height>225){
document.form.image.value = 'myGoodImage.gif';
alert('Bad Image: '+this.width + 'x' + this.height);

img.onerror = function chkLoad(imgUrl){
document.form.image.value = 'myGoodImage.gif';
alert('Image Failed To Load');


<form name="form">
<input type="text" name="image" value="monkey.gif">
<input type="button" value="Submit Image"


03-20-2004, 03:54 AM
Just to point out, that if the user has javascript disabled or disables javascript, they will be able to use larger images, so if there is any way you can implement this server side it would probably be more reliable.

03-20-2004, 10:23 AM
Yes, that same thing crossed my mind and l've since scrapped the idea, for a php script which was much easier to do.

Thank you Willy, l've saved ur script and l've already learned from it.


03-20-2004, 10:39 AM
not really any need for the user to disable javascript he could simply upload a picture at the given url after he made the post.