ImageRedirection.class.php

This PHP class is based on ImgRed.com, the following description is borrowed from that site. If you wish to know why I post a script that does the same thing, scroll down to "about".

When posting on message boards or blogs, often you want to show or link to an image on another website. Directly showing or linking to that image is called hot-linking. It's generally bad because it leeches bandwidth from the host site, and as a result many webmasters have their servers set up to prohibit hot-linking.

The typical work-around is to do the following:

  • Save the image to your hard drive
  • Navigate to a free image-hosting website
  • Enter the path to the image on your hard drive
  • Wait for it to upload
  • Copy the new URL and paste it into your message-board or blog post

That's a rather tedious process, instead (just like ImgRed.com) this class can simply let you enter the original URL but with a prefix that could be like: http://imagebase.example.com/image/.

Examples

This is an example of hot-linking to an image from mozilla.com:

That's bad, and some websites will even prevent the image from being displayed. So instead, do this:

To show thumbnails, you can do this:

Updates

  • Version 0.91
    • Replaced md5() with urlencode() for readability and reversing of filenames.
  • Version 0.9
    • Now uses CURL instead of file_get_contents thanks to Patrick Havens.
    • Improved and restructured alot of functions.
  • Version 0.8
    • Initial public release

Requirements

  • PHP 5.x
  • Linux server (or similar)

Installation

Edit the configuration variables in the file called *ImageRedirection.class.php*

PHP:
  1. <?php
  2. private $cachePath = '/home/bob/public_html/images/cache/';
  3. private $maxFileSize = 2097152;
  4. private $thumbSuffix = 'thumb';
  5. private $maxHeight = 320;
  6. private $maxWidth = 320;
  7. ?>

You also have to create an *.htaccess* in the same dir and write the following:

CODE:
  1. <IfModule mod_rewrite.c>
  2. RewriteEngine On
  3. RewriteBase /
  4. RewriteCond %{REQUEST_FILENAME} !-f
  5. RewriteCond %{REQUEST_FILENAME} !-d
  6. RewriteRule . /index.php [L]
  7. </IfModule>

Finally create a folder named *cache* or something like that and chmod it to 777.

About

I first tried the source I found at ImgRed.com, but it didn't work out-of-the-box and the code looked as if it was written in a haze. I wanted still to use it so I decided to rewrite it as a class to make further improvements easier.

Changes I made:

  • Cleaned up it alot and refactored it into a class.
  • Removed all database connections, they didn't serve any purpose, just slowed down the script.
  • All images are named after an MD5 hash of their URL, that way I can keep track of images that already exists in the cache.
  • Removed all error images. There are plans to include them again in a future release.
  • Removed watermark ability. There are plans to include it again in a future release.

Todo

  • Open URL's on different ports like 8080.
  • Add watermark option
  • Display a list of all chached images
  • Option to support other files than images

Download

Visit the repository here to download

4 Responses to “ImageRedirection.class.php”
  1. Patrick Havens Says:

    Thanks for working on this, I’ve been looking for a bit better written code for my won use. My only issue is that my host (Dreamhost) doesn’t allow file_get_contents, instead they suggest using curl. And my hacking at your code hasn’t been able to get it to work yet. I’ll post my changes if I work it out, it’s late so I’ll start fresh tomorrow.

  2. Patrick Havens Says:

    OK.. I got it to somewhat work. But tis currently not making the thumb, and at times it returns the URL, rather then the image (Shift refresh seems to work).


    private function addImage($url) {
    $ch = curl_init();
    $timeout = 5; // set to zero for no timeout
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $contents = curl_exec($ch);
    curl_close($ch);
    if ($contents) {

  3. SubZane Says:

    Neat! I replaced file_get_contents with your code in version 0.9

  4. imagesafari Says:

    This is pretty awesome, Thanks!

Leave a Reply

You must be logged in to post a comment.