Preview images with DOM, CSS (and a dash of PHP)

Sometimes it might be a good idea to give visitors an insight of what is lurking behind a link. Normally this is achieved via a thumbnail, but what about inline links?

This article explains how you can enhance a link with a class named “preview” pointing to an image via DOM JavaScript. The enhanced link has a small clickable icon that will show a preview of the linked image. If you have PHP with the GD library enabled, it even creates the preview on the fly.

Read more about “Dynamic preview images with DOM, CSS and PHP“:http://icant.co.uk/articles/imagepreview/

Example of the preview functionality

17 Responses to “Preview images with DOM, CSS (and a dash of PHP)”

  1. Faruk Ate? Says:

    Question… why are you redeclaring var hasphp ? What’s the use of declaring it as either true or false when the next line declares it as a URL-string, anyway?

  2. Faruk Ate? Says:

    Additional:

    1) the JS creates three javascript warnings on each page load, caused by variable redeclarations;
    2) may I suggest adding some checks to see if an image has been popped up / shown as preview already? It’s very annoying being forced to manually click away each opened preview, much better (to me and others) would be to have an already opened preview be closed when you open a new picture for previewing.

  3. Faruk Ate? Says:

    Chris,

    1) ah, ok, well, it just confused me when I saw it. My PHP nature went “well that’s pointless!”

    2) I use the Javascript Console on Mozilla 1.7.6 – when I load the page, three “Warning: redeclaration of var …” notices appear

    3) For having them next to each other, it would make sense. Your example, however, has them overlapping each other, and if you go from bottom to top you can’t even see much of the second two images you open, as they get hidden underneath the third one.

    For those reasons, I’ve made my version of this concept (which I wrote a few months ago, see it in action here) make only one image appear at a time.

  4. Faruk Ate? Says:

    Addendum to last comment (sorry!): in the previous linked page, click on the small thumbnail to see my script in action

  5. John Says:

    Chris,

    Thanks for the great article. Here is a semi-related question. Have you ever been able to convert multi-image/page tiffs to a jpeg image using php. I have a site that uses the same concept of the mouseover, but I would like to view all pages of the tiff file.

    Thanks!

    John

  6. Hunox Says:

    It’d be better if click anywhere outside the image zone would hide the image. It’s default windows behaviour. I even tried doing it before reading.

    Edit
    Web sites != Windows

  7. monkey dancing shadow Says:

    N00b question: The images don’t display properly in Safari 2.0 – the loading image appears correctly, but the actual thumbnails display all chopped-up like. Is this because Safari lacks the DOM support required?

    Don’t know if this is related, but the camera icons do not appear in Faruk’s example as they do in the main example.

  8. Sarah Says:

    This is cool and works very well, I thought I had found just what I been looking for. But turns out It doesnt do what I need.
    First I have .JPG and .GIF images both Id like to provide previews for and second I need to link across directories. Are these options possible?
    Reguardless thought its a very nice piece of work :)

  9. Rainer Says:

    put in your css:
    a {outline : none;}
    and the outline is hidden …

  10. tercüme Says:

    Addendum to last comment (sorry!): in the previous linked page, click on the small thumbnail to see my script in action …

  11. freelancer Says:

    Would be good if images last clicked image was assigned greater z-index. Currently latest clicked image is partly covered by previously clicked. Great work anyway.

  12. nick Says:

    I recently came upon this “currently free” website for capture website screenshots, they mention that they are free while in beta and offer over 1000 sizes, i currently use them on my websites for website thumbnails and have had no problems at all! thumbnailspro.com

  13. Mike Says:

    Why is it the Bitmap files break the script? Bitmaps do not load…
    I would love it if bitmaps were displayed

  14. Andy Says:

    Looks great. One design issue comes to mind though, I reckon you should style the camera button links with ‘outline: none;’ otherwise it really ruins the look in browsers that utilise hyperlink outlines (like firefox for example).

  15. Moody Says:

    This script looks wonderful, I read the php file and I can fairly understand it, but Im wondering if it would be possible to display the thumbnail picture always without creating a hyperlink.
    for example, if we supply the element with
    class=”createpreview” for multiple pictures in the page.
    would it be possible ? I would like to use it in my website so let me know if that would work.

  16. Asya Says:

    Chris,
    Thanks for developing this. I am having a similar issue to Sarah. I am trying to use this across directories with various document types (PPT, PDF, Word) rather than JPGs and PNGs as the preview image format. I’ve tried testing this locally using your test page and it doesn’t work, so I wonder whether this script can accommodate different formats of link vs. preview.
    The page I am using it on is a corporate Intranet site with 200+ documents and it would be great to find something that doesn’t require the thumbs to load together.
    Thanks for your help, Asya

  17. Amit Says:

    How do you display the thumbnail on the page instead of opening it by clicking the url?

Wait till I come! is the blog of Christian Heilmann , a developer evangelist living and working in London, England. Download vcard.

Feed me, Seymour: Entries (RSS) and Comments (RSS).