Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

dwImageProtector Plugin for jQuery

40 Responses »

I've always been curious about the jQuery JavaScript library. jQuery has captured the hearts of web designers and developers everywhere and I've always wondered why. I've been told it's easy, which is probably why designers were so quick to adopt it NOT that designers aren't intelligent, but designers usually have enough design stuff to worry about and if they wanted to be programmers, they would've become programmers. jQuery being easy didn't convince me to try jQuery because Moo came pretty easy to me.

Last weekend I decided I needed to try jQuery. It would make me a more well-rounded developer and it might suit particular projects better than Moo. And there's nothing wrong with knowing both, right? I decided the first step would be porting over and existing MooTools class I'd written. What would be better than my much-misunderstood image protector class? Here it is, now in jQuery plugin format.

The jQuery Plugin JavaScript

jQuery.fn.protectImage = function(settings) {
	settings = jQuery.extend({
		image: 'blank.gif',
		zIndex: 10
	}, settings);
	return this.each(function() {
		var position = $(this).position();
		var height = $(this).height();
		var width = $(this).width();
		$('<img />').attr({
			width: width,
			height: height,
			src: settings.image
		}).css({
			border: '1px solid #f00',
			top: position.top,
			left: position.left,
			position: 'absolute',
			zIndex: settings.zIndex
		}).appendTo('body')
	});
};

The plugin accepts two parameters: the zIndex and the image you'd like to use as the protector.

The jQuery Usage

$(window).bind('load', function() {
	$('img.protect').protectImage();
});

It's important to run the plugin during the page's "load" event so that the dimensions are correct.

This plugin requires the jQuery Dimensions plugin. Click here to download the blank.gif overlay file.

Don't bother commenting about how you know 20 ways to get around this. Many non-technical persons will be fooled.

Look forward to reading my impressions of my first jQuery project tomorrow!

Discussion

  1. August 11, 2008 @ 8:31 am

    The jQuery Dimensions plugin is now integrated in the latest version of jQuery, so adding this is no longer necessary. Thanks for this useful plugin!

  2. August 11, 2008 @ 9:56 am

    Thanks!

    I dabble in Mootools but JQuery is my library of choice.

  3. anonymous coward
    August 11, 2008 @ 10:50 am

    ZOMG!!!!

    You cant protect images. I just use FireBugz and bam your img’s are belong to me.

    (joke :)

    thx for the post. :)

  4. August 11, 2008 @ 11:39 am

    @Steven: Thank you for that. I still consider myself a noob! :)

    @Anon: Thank you for being light-hearted!

  5. August 12, 2008 @ 2:16 am

    Hi,

    I’m using Firefox 3 and the image is not protected at all, I can right click on the image abd I haven’t got any messages from jquery???
    Just use Web developper toolbar/Images Informations the protection goes away …

    Is there anything I missed?

    Thx for the post

  6. miko
    August 12, 2008 @ 2:27 am

    Hilarious

  7. August 12, 2008 @ 7:32 am

    @Stef G: If you were to right-click and choose “Save Image As…”, you’d be downloading an image but the image would be the overlaying blank.gif file.

  8. August 14, 2008 @ 12:52 pm

    Hey, great idea but sometimes the body isn’t the only relative container of the image. I won’t post here the code to fix it I made but the point is:
    - Instead of appending the new to the body, I cache it into a variable and append it after the protect image.

  9. September 20, 2008 @ 4:13 am

    Hey dude. Thanks for the awrsome plugin. This was exactly what I was looking for!

    As of jQuery 1.2.x the position() function is replaced with offset(), so you need to update that in your code.

    var position = $(this).offset();

    Simple enough.

    The issue I have to figure out now is i used onhover events on the images getting replaced and now they don’t work anymore =( I’ll have to tie those events onto the replaced blank.gif onhovers.

    I’ll post code when I figure it out.

    Cheers,
    Jordan of http://CubicleCollective.com

  10. September 20, 2008 @ 6:38 am

    I fixed the issue with onhover and other things.

    Problem with this plugin is that if you have an image you want to protect and it’s wrapped around an anchor tag…you essentially lose the anchor tag.

    <a href=”/gohere”><img class=”protect” src=”iamprotected.jpg” /></a>

    Also you had to rebind certain events which I was using, particularly .hover() which I had to rebind to “mouseenter” and “mouseleave” as .hover() is a convenience function.

    Here’s the code snippet. or you can view my working demo at https://civil.workcube.com/content/burning-bridges

    return this.each(function() {
    var position = $(this).offset();
    var height = $(this).height();
    var width = $(this).width();
    var srcimg = this;

    var protect = $(‘<img />’).attr({
    width: width,
    height: height,
    src: settings.image
    }).css({
    /*border: ’1px solid #f00′,*/
    top: position.top,
    left: position.left,
    position: ‘absolute’,
    zIndex: settings.zIndex
    }).bind(
    ‘mouseenter’, function() {
    if($(srcimg).hasClass(‘imagecache-product_list’)) {
    $(srcimg).toggleClass(‘hovering’);
    }
    $(srcimg).trigger(“mouseenter”);
    }).bind(
    ‘mouseleave’, function() {
    if($(srcimg).hasClass(‘imagecache-product_list’)) {
    $(srcimg).toggleClass(‘hovering’);
    }
    $(srcimg).trigger(“mouseleave”);
    });

    if($(srcimg).parent().get(0).tagName == “A”) {
    link = $(srcimg).parent().clone();
    link.html(protect);
    link.appendTo(‘body’);
    }
    else {
    $(protect).appendTo(‘body’);
    }

    });

    Thanks again for the code,
    Jordan

  11. October 31, 2008 @ 3:48 am

    Its very nice plugin and working fine for me in firefox. My only problem is in IE 7 it creates two images, one in proper place and one may be displaced left or right. Its whired that i can’t fix this problem.

    has anyone got similar problem and has got a fix??

    please email me if you want me to show the page and code. cheers.

    kuikel

  12. November 1, 2008 @ 8:09 am

    Oops, I guess I should post the proper links to websites instead of posting my internal domains.

    The project I used image protector on is live and you can see it here:
    http://civilcitizen.com/catalog/2

    As for Michael, I do not believe my modified solution fails in IE7 (i think i tested it in that browser at least), so pop over and take a look at the jQuery there.

  13. November 11, 2008 @ 5:55 pm

    we can download the image, by just clicking the image before its downloaded completed. means click the image before it is downloaded full.

  14. February 8, 2009 @ 11:56 am

    There is just one way to protect image: don’t publish them ;)

    I use the web developper bar for Firefox and with it, I can download the image with no problem. Thanks anyway: I love jQuery

  15. February 24, 2009 @ 2:45 pm

    I used J0rd’s example above, and it’s working great. Thanks!

  16. erwan
    March 16, 2009 @ 12:31 pm

    Hi. It’s a very nice plugin you made there.

    Only one thing misses for my use, and I don’t manage to set it (noob at javasctipts).
    I’d like to copy the title & alt properties of my protected image on the blank image.
    Would you (or anyone) explain me how to do it ?

    (Thx)

  17. stephan
    April 9, 2009 @ 5:51 am

    Hi,

    I’m a beginner with jQuery. Currently, I’m developping a website with wordpress, and I can’t activate the image protection with your code. Please could you explain me how to do? Think I miss a step.

    Here is what I done :
    1 – download the js files in a js/jquery directory
    2 – check if I allready have jquery. Yes, with Next Gen Gallery Plug In. jQuery Version: 1.2.6.
    3 – place this code between tags :

    <script type=”text/javascript” src=”<?php bloginfo(‘wpurl’); ?>/wp-includes/js/jquery/jquery.dimensions.js”></script>
    <script type=”text/javascript” src=”<?php bloginfo(‘wpurl’); ?>/wp-includes/js/jquery/jquery.dwImageProtector.js”></script>
    <script type=”text/javascript”>
    $(window).bind(‘load’, function() {
    $(‘img.protect’).protectImage();
    });
    </script>

    4 – place the blank GIF in the jquery directory

    That’s it! But nothing on my website. I always can download my images. So, if you have a solution for me. It would be great.

  18. rich
    April 22, 2009 @ 10:07 pm

    I like this a bunch, but was thinking that it would also be a nice touch to have a watermark added to the image if it is downloaded. That is, when you view it on the site, there is no watermark, but if someone decides to right-click and save it, it would then overlay, say a trasnparent gif in the center of the image and merge the two. Is that possible?

    Rich

  19. April 23, 2009 @ 7:29 am

    @Rich: Sure — you could customize the class and inject an additional watermark element positioned to where you’d like it to be.

  20. rich
    April 27, 2009 @ 9:47 pm

    Thanks for the quick reply Dave. Is there any way you could point me in the right direction?..I only have a working knowledge of jQuery, and javascript for that matter. But I can more often than not work through an issue with a little nudge. — thanks.

  21. May 24, 2009 @ 11:27 am

    I’ve just tried this bit of jQuery code, and I love it. It was an almost perfect solution delivered to me in no time. However, I am still trying to overcome an issue with Firefox, when protecting images inside tags, the protected images are no longer clickable. I remove the protection and the ‘image links’ work again. This problem doesn’t arise in IE. I will post again when I have solved this.

  22. May 28, 2009 @ 1:54 am

    i did a little change to code. since i want the protection only on mouseover and remove the protection as mouse leaves.

    jQuery.fn.protectImage = function(settings) {
    settings = jQuery.extend({
    image: ‘blank.gif’,
    zIndex: 10
    }, settings);
    return this.each(function() {
    var position = $(this).offset();
    var height = $(this).height();
    var width = $(this).width();
    var protect = $(‘<img />’).attr({
    width: width,
    height: height,
    src: settings.image
    }).css({
    border: ’1px solid #f00′,
    top: position.top,
    left: position.left,
    position: ‘absolute’,
    zIndex: settings.zIndex
    }).bind(‘mouseleave’, function() { protect.remove() });

    $(protect).appendTo(‘body’);
    });

    };

    And i will append the event using

    $(‘img.priceChartImage’).bind(‘mouseenter’,function(){
    $(this).protectImage();
    })

    what do you guys think about this one?

    Also thanks for initial code david.
    Cheers Team!!!

  23. May 28, 2009 @ 2:08 am

    Even i replaced blank.gif with :) . My usecase was to block images dragged to html editor. if i use blank.gif it was getting dragged to html editor. Though we cannot view blank.gif it was making the html content dirty. So i changed my code to use div. Following is the code i used.

    var protect = $(‘<div>’).css({
    border: ’1px solid #f00′,
    top: position.top,
    left: position.left,
    position: ‘absolute’,
    zIndex: settings.zIndex,
    width:$(this).width(),
    height:$(this).height()
    }).bind(‘mouseleave’, function() { protect.remove() });

    Any comments???

    Cheers :)

  24. August 14, 2009 @ 5:55 am

    Amazing code..Thanks alot for it :-)

  25. dave
    October 8, 2009 @ 1:08 pm

    Um, I just tried the demo (in Safari 4) and this method simply doesn’t work. I right-clicked on the image and opened it in a new tab… I also simply dragged it off onto the desktop.

  26. nick
    December 20, 2009 @ 7:43 pm

    with Google chrome you can simply drag the image from the browser to the desktop, but right clicking and saving the image will save the blank.gif image.

  27. February 4, 2010 @ 9:36 pm

    Great idea but with Firefox 3.5.7 if you simply resize the webpage the blank.gif does not keep focused over the real image.

  28. nick
    March 22, 2010 @ 2:46 pm

    uhh, you know this is a total FAIL right? in chrome i can drag the image to the desktop, and it’s the actual image, i can also right click and save the image, and i’m not downloading a transparent image, it’s the actual image. maybe better luck next time.

  29. March 22, 2010 @ 2:51 pm

    @nick: Chief, you see this article is close to 2 years old, correct?

  30. nick
    March 26, 2010 @ 4:46 am

    @David Walsh:
    awww dude i’m a total FAIL.
    that’d be neat if it still worked. oh well.

  31. charly
    April 16, 2010 @ 4:46 pm

    si quiero una imagen simplemente agrando la imagen con CTRL y la tecla +, luego pulso la tecla PRINT SCREEN y paste in photshop se edita y punto! pues no hay imagen q no se pueda bajar!! jajajajaja

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!