[TIP] On the fly keyword rich images for autoblogs

Discussion in 'Making Money' started by maineyak, Aug 27, 2011.

  1. maineyak

    maineyak Power Member

    Mar 27, 2008
    Likes Received:
    Here's a trick I use to insert keyword rich images into my autoblog posts on the fly.

    Create an empty directory, I named mine "images".
    Create this .htaccess and put it in that folder:
    RewriteEngine On
    RewriteBase /images/
    RedirectMatch "\.jpg$" http://www.yourdomain.com/2/rand.jpg
    Then create another folder ABOVE the one you just created, I named mine "2". Create another .htaccess file and place it in this new folder:
    RewriteEngine On
    RewriteBase /2/
    <Files rand.jpg>
                    ForceType application/x-httpd-php
    Now, take the following code, and name it rand.jpg. It's php, but the .htaccess you just created will make it a .jpg file:
    Header('Cache-Control: no-cache');
    Header('Pragma: no-cache');
    $dh = opendir(".");
    while (false !== ($file = readdir($dh))) 
      if (preg_match('/\.jpg$/i', $file) and $file != "rand.jpg") // scanning for .jpg files only and stopping rand.jpg being loaded (you will run into errors if this isnt here)
             $filelist[] = $file;
    $picnum = rand(0, sizeof($filelist) - 1);
    header("Location: " . $filelist[$picnum]);
    Now, dump a bunch of jpg files in directory 2 that are related to your niche, along with rand.jpg and the second .htaccess you created. So now, whenever you call an image like this, http://www.yourdomain.com/images/blahblahblah.jpg, it will point to the rand.jpg, and rotate through the jpg files you have in folder 2.

    Create a list of your keyword, one per line:
    keyword phrase 1
    keyword phrase 2
    keyword phrase 3
    save it as keywords.txt

    Createa ANOTHER file named keywords2.txt, and using search and replace, make it like so:
    Upload both those to your root directory.

    Still 2 more files to go....
    Create a file and name it keywords.php:
    <? $textfile ="keywords.txt"; $items = file("$textfile"); $item = rand(0, sizeof($items)-1); echo $items[$item]; ?>
    Create almost the same file again, but name it keywords2.php:
    <? $textfile ="keywords2.txt"; $items = file("$textfile"); $item = rand(0, sizeof($items)-1); echo $items[$item]; ?>
    Upload those to your root directory as well.
    For this to work in WP, you'll need this plugin: WP exec PHP, it allows you to execute php in widgets, templates, pages, etc. Install and activate this plugin

    In the template that renders your post, some themes call it single.php, mine is called index.php, place this code:
    <img src=/images/<? include_once "keywords2.php"; ?> width=250 alt="<? include_once "keywords.php"; ?>">
    You'll have to figure out where to place the code, each template is different. I placed mine at the bottom of the post, you may prefer to have it near the top.
    You can set the width to whatever you like. Now, everytime your post is loaded, it will take a random keyword and name your photo: keyword-phrase-1.jpg, keyword-phrase-2.jpg, etc.... and give each a keyword alt text as well. It will rotate through all the images you have in folder 2. I've noticed more traffic to my site since implementing this, G likes images, and keyword rich images are even better.

    If for some reason you can't get this to work, you may need to tweak the .htaccess files, this works fine on my server.

    Hope this has helped someone. :cool:
    Last edited: Aug 27, 2011