Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Dec 2003
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Few questions concerning strings

    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()
    aa.src=????;


    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);
    }

    document.post.message.value=s;
    }


    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!

    ~JSlime

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Code:
    <HTML>
    <HEAD>
    <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');
            }
    
      }
     //-->
    </script>
    </HEAD>
    
    <BODY>
    <form name="form">
    <input type="text" name="image" value="monkey.gif">
    <input type="button" value="Submit Image"
    onClick="chkImage(this.form.image.value)"> 
    </form>
    .....Willy

  • #3
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New Coder
    Join Date
    Dec 2003
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    ~JSlime

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •