I have a code snippet someone posted in a forum that is supposed to prevent right-click saving of images in the browser. Its in Java. Can anyone see if it looks okay as I am not Java-fluent.

Secondly, I have server generated images on my site. I think this means that they won't exist in the users Temporary Internet Files (which is nice), but how/where do I incorporate the java disable thing into my PHP graphic routine? which is along the lines of <IMG SRC = MakePicture.php!ThisPicture> (I forget the precise syntax) but it works a treat.

Does this java thing need to be downloaded for every picture or just once when the user logs in? That'd make it good..

<script language=JavaScript> 
function clickIE() {
if (document.all) {
return false;
}
} 
function clickNS(e) {
if (document.layers||(document.getElementById&&!document.all)) { 
if (e.which==2||e.which==3) {
return false;
}
}
} 
if (document.layers) {
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS;
} 
else{
document.onmouseup=clickNS;
document.oncontextmenu=clickIE;
} 
document.oncontextmenu=new Function("return false") 
</script>

    dont even make me start, there is a BIIIG difference between java and javaScript!

    anyway, if the user really wants to get the image, all he/she has to do is go to view->source and look for the html img tag... not worth putting the code it...

      ....But, if you really want the script, try this:

      <SCRIPT language=JavaScript>
      function click() {
      
      if (event.button==2) {
      alert('Right click is disabled on this site!!')
      }
      }
      document.onmousedown=click
      </SCRIPT>
      

        that kinda works batman, but if you press both the left and right mouse buttons at the same time, the context menu still comes up... 😉

        btw, to the starter of this thred, if you just put that in the header of the page, then it will disable the context menu anyway, so just put it in the header...

          🙂 Yes your right. But how many people are going to know that? I mean, if their smart enough to do that, then what would stop them from just taking the picture many other ways? There is probably many many ways to steal an image. 🙂 So....I think that what I wrote will work just fine for the noobs that come to his site, and there is no stoping the others that know what their doing. but oh well!!!! 😉

            I tend to agree that there's little point in doing this, aside from the fact that a lot of dumb people use the WWW. Heh, I'm probably one of them, but none of these JS things stop me. If your webserver can display it, I can save it.

            There are lots of sites that use this feature. Bah! you didn't give us much info on how you're using the JS code. But, then again, this is a PHP forum anyway.

            And, if you don't deign to give us your name, why should we help you? 😉

            😃 😃 😃

              Ben, thanks for the sound code btw - works a treat.

              if you don't deign to give us your name, why should
              we help you? 🙂
              Um.. er.. 🙂

              if you just put that in the header of the page, then it will
              disable the context menu anyway, so just put it in the
              header...

              I'll try not to sound like a doofus when I say this: What's a header? (Doh! failed)

              Doesn't sound like this disable thing is worth the effort. I don't want the users to feel they are being denied anything unneccesarily with java trickery. Its just a shame the images the server will have to labour to produce with such toil using code that was worked on so hard will be saved so easily... when by jove they should be worshiped from afar 🙂

              Well, seriously, I know its stingy but my piccy's are a proud part of the sight for me.

              Bah! you didn't give us much info on how you're using the
              JS code.
              I don't know how to use Java JS code. I'm calling a php file within an <IMG> tag that returns an image using ImageJpeg().

                uhh... you do know HTML, dont you?

                the header is:

                <html>
                <head> 
                <!-- this is the header area -->
                <title>bob</title>
                <!-- you put scripts like your anti-context menu script in this area -->
                
                <script language=JavaScript>  
                function clickIE() { if (document.all) { return false; } }
                function clickNS(e) { if (document.layers||(document.getElementById&&!document.all)) {
                if (e.which==2||e.which==3) { return false; } } }
                if (document.layers) { document.captureEvents(Event.MOUSEDOWN); document.onmousedown=clickNS; }
                else{ document.onmouseup=clickNS; document.oncontextmenu=clickIE; }
                document.oncontextmenu=new Function("return false")
                </script> </head> <body> <!-- this is the body area --> </body> </html>

                and... you dont want the javascrip code to be sent along with the image generated by a php script, otherwise it will be a broken image...

                but if you really dont want people selecting things, use that code, and put this in the body tag:

                <body ondragstart="return false" onselectstart="return false">

                  Thanks for the code again

                  I understand basic HTML I use

                  <HTML>
                  <TITLE></TITLE>
                  <BODY>
                  </BODY>
                  </HTML>

                  I can do images that open other web pages and IMG tags which run php files with return values. Also tables. I am learning what I need as I go along. I code already so I have to learn the html side of things. I probably need a book on HTML and web development in general.

                    Someone can press the Print Screen key and then copy and paste it to paint or another image program and voila they have the image. Don't waste too much time on this.

                      what he said...

                      btw, you dont want to ommit the <head> tag, even the most basic page should have this format:

                      <html>
                      <head>
                      <title>This is a title</title>
                      </head>
                      <body>
                      </body>
                      </html>

                        Ta 😉

                        I was considering splitting the final outputted image into two, so if someone click-saves they only get the image, not the text title which is GD'd onto the image (ooh, don't ask me about the problems I had before discovering ImageColorAllocate() !)
                        The reason is, at a stretch these images could be saved and used for personal use, documentation, tuition etc. I guess a determined user with reasonable skill is gonna get them no matter what I do.

                          Originally posted by darknuke
                          Someone can press the Print Screen key and then copy and paste it to paint or another image program and voila they have the image. Don't waste too much time on this.

                          Or choose "Save As" from the file menu - most browsers (rather, the most widely used modern browsers) will save all the ancillary stuff (including images) as well as the HTML page itself.

                            In General:
                            If you are afraid somebody is going to use it, don't place it on the web. Because IF it IS any GOOD.. People WILL grab it from your site.

                            No tricks to prevent this.
                            Sorry.

                            But you could of course place it on the web at low resolution: For any decent printing you need appr. 200 dpi. Web needs only 72 dpi..

                            An other option is to place a watermark in the image..?

                            J.

                              But you could of course place it on the web at low resolution: For any decent printing you need appr. 200 dpi. Web needs only 72 dpi..

                              Is this to do with the display quality? I am using ImageJpeg() to output it, the image quality argument is 1-100.

                                No, dpi stands for "dots per inch" - it describes how big the pixels in the image are displayed. At 72dpi, 72 pixels make a line 1 inch long.

                                Since monitors have much lower dpi than print, if you take a picture scaled for monitors and print it you either
                                (a) end up with a small image (because the individual pixels are smaller), or (b) a poorer-quality full-sized image (because the computer or the printer had to guess what colour most of the printed pixels should have been). Printers of reasonable quality however are often pretty good at guessing (or at least, they're good at faking it).

                                The JPEG quality setting affects the amount of compression employed on the image by adjusting how much information is thrown away during the compression process. It is independent of dpi.

                                  Originally posted by leatherback
                                  In General:
                                  If you are afraid somebody is going to use it, don't place it on the web. Because IF it IS any GOOD.. People WILL grab it from your site.

                                  No tricks to prevent this.
                                  Sorry.



                                  Since there are so many internet users that are dumb enough to open an .exe in an email, I doubt that they know any other way than "Save Target As...", "Save Image", or a similar method.

                                    a month later

                                    It is true that there is no real way to hide any of your source code. If all else fails the use can always click file | Save and then view it in an editor. If you want to get rid of right-clicking, here is the best code I have found yet. It never lets the context menu pop up.

                                    <script language=JavaScript>
                                    document.oncontextmenu=new Function("return false")

                                    </script>

                                      Originally posted by skingdon
                                      It is true that there is no real way to hide any of your source code. If all else fails the use can always click file | Save and then view it in an editor.

                                      Pretty much what I pointed out.

                                      If you want to get rid of right-clicking, here is the best code I have found yet. It never lets the context menu pop up.

                                      <script language=JavaScript>
                                      document.oncontextmenu=new Function("return false")

                                      </script>

                                      But note that this is specific to Internet Explorer. But hey, we're talking about doofuses here so I guess that's okay. 🙂

                                        Write a Reply...