Installing Perl Modules on Windows with PPMFriday, July 10th, 2009 with No Comments »

If you installed Perl on Windows with ActivePerl, you have a tool known as PPM to install modules.

Links:

Example usage from command prompt:
ppm search Image
ppm install Image-Size

Bulk Image Upload and Insert in WordPressSaturday, February 21st, 2009 with No Comments »

Occasionally I’ll need to upload and insert more than 100 photos into a single WordPress post. I haven’t seen any good bulk image upload and insert plugins, so it has been a gruesome task. However, with the use of Perl, BIMP Lite, and FileZilla the annoying aspects of this task have been automated.

Requirements
1. BIMP Lite - a free tool for Windows which I found from Smashing Magazine’s 15 Useful Batch Image Processors
2. An installation of Perl on your computer
3. FileZilla, or any FTP client
4. The Image-Size Perl Module, created by Randy R Jay, installed on your computer

The Basic Idea
The idea is to have a folder with all the images you need to upload. First, you use BIMP Lite to generate the thumbnails for you into a subdirectory called “thumb.” Next, upload all these images to your WordPress image uploads directory using your FTP client. Finally, use the Perl script below to generate the WordPress HTML.

Generator.pl

# Generates the HTML for bulk image upload and insertion for WordPress
#
# @author Cody (codelol.com)
# @date February 21, 2009

use Image::Size;

# The directory where the images are stored
$image_dir = ".";

# The directory relative to $image_dir where the thumbnails are stored
$thumb_dir = '/thumb/';

# The WordPress image uploads directory
# e.g. http://example.com/wp-contents/uploads/2009/02/
$wp_image_dir = "http://example.com/wp-contents/uploads/2009/02/";

# Currently only uses JPEG.  Update the regular expression as needed
opendir(DIR, $image_dir);
@files = grep(/\.jpg$/,readdir(DIR));
@files = sort(@files);

opendir(DIR, $image_dir.$thumb_dir);
@files_thumb = grep(/\.jpg$/,readdir(DIR));
@files_thumb = sort(@files_thumb);

$output = "";

$size = @files;
$size_thumb = @files_thumb;

# A very BASIC check to ensure there is the correct number of thumbnails
if($size != $size_thumb)
{
  # If the size does not match, exit
  die "The number of thumbnails (".$size_thumb.
  ") does not match the number of images. (".$size.")\n";
}

# Generate the WordPress code
for($i=0;$i<$size;$i++)
{
  # Determine the dimensions of the thumbnail
  ($width, $height) = imgsize($image_dir.$thumb_dir.$files_thumb[$i]);

  # Escape a single space by using HTML URL encoding
  # You may need to escape additional characters if necessary
  $file_escaped = $files[$i];
  $file_escaped =~ s/ /%20/g;
  $files_thumb[$i] =~ s/ /%20/g;

  $output .= '<a href="'.$wp_image_dir.$file_escaped.'">';
  $output .= '<img src="'.$wp_image_dir.$files_thumb[$i].'" ';
  $output .= 'alt="'.$files[$i].'" title="'.$files[$i].'" ';

  $output .= 'width="'.$width.'" height="'.$height.'" ';

  # You may need to edit the class attribute if you aren't using
  # algincenter or size-medium
  $output .= 'class="aligncenter size-medium" /></a>';
}
print $output;

print "WordPress code generated successfully!\n";
open(FILE, ">generate.txt") or die("Error");
print FILE $output;

Instead of manually uploading and inserting each image, these steps will basically automate most of these tasks…so you can go drink a beer and watch TV while waiting for the images to be uploaded by FTP.

Instructions
1. Make sure you meet the requirements
2. Run BIMP Lite to create thumbnails in a subdirectory in your images directory
3. Upload the original images and thumbnails to your WordPress images upload directory
4. Copy the Generator.pl into your images directory
5. Update $wp_image_dir in Generator.pl to your image upload directory
5. Execute the Perl file by double clicking on it
6. The generated HTML will be put into a new file called generate.txt
7. Open generate.txt and copy this code into your WordPress post

Using Perl to Generate Code for SHJS SetupSaturday, January 31st, 2009 with No Comments »

Using SHJS for syntax colouring requires includes for each language. Here’s a simple Perl script to generate these includes if you downloaded all the files to a directory and was too lazy to manually type them out

opendir(DIR, ".");
@jsFiles = grep(/\.js$/,readdir(DIR));

$output = '<script type="text/javascript" src="sh_main.js"></script>';
$output .= "\n";

foreach $file(@jsFiles)
{
  $output .= '<script type="text/javascript"';
  $output .= 'src="lang/'.$file.'"></script>'."\n";
}

$output .= '<link type="text/css" rel="stylesheet"';
$output .= 'href="css/sh_style.css">'."\n";

open(FILE, ">generate.txt") or die("Error");
print FILE $output;
Get updates as often as we post! Subscribe to our full feed or comments feed.