HOME
Andrew Schwabe's Blog : PHP

Aptana Studio Released!

I am truly impressed with what this group has brought to the development community.  Long ago I loved a code editor called "HomeSite+" which existed in several interesting flavors, ColdFusion Studio (i think?), and JRun Studio were two other flavors I remember. 

Then came the Dreamweaver shift...  I never really got on that bandwagon.  I resisted for a LONG time, then I tried it out, and just never really was thrilled with it.  I am a coder at heart.

Then came Eclipse.  Great concept, just not for the masses.  Then came Eclipse 3, and then CFEclipse... then came Flex Builder and the RDS plugins, all good still, but not thrilling.

HomeSite was great at letting you develop remotely.  Thats a big no-no in the software world now, but for web developers it is a big deal.  You don't always have _everything_ you need running on your laptop!

Anyway, I got off track.  After all these tools, finally comes an attempt to bring it all together: Aptana Studio.  In its development stage as Aptana IDE, I fell in love with it (well, with MOST of it anyway...).  It all runs on Eclipse, and gives the basic developer everything he or she needs to code html, php, javascript, xml, css, etc.  All the things that were either missing, or painfully buggy or incomplete in Eclipse.  Not to mention an FTP (and secure FTP) system that actually works!  And you can create new empty files on remote ftp servers too (a lot of us have been waiting for this feature for a while).

I can now safely say that I have found my HomeSite+ replacement :)

Download it, you know you want to: http://www.aptana.com/ (its free)

FYI, Aptana  was founded by Paul Colton, formerly of Allair.  If you can remember back that far, Allair was the company that originally built ColdFusion, before it was bought by Macromedia (and subsequently by Adobe).

Image gallery in 5 minutes using ColdFusion and PHP

Ok, here's the scoop:  I needed a way to show thumbnails for some photos and I didn't want to spend any more than 15 minutes to do it.  Talk about deadline, huh?  Anyway, here is what I came up with, and yep, I managed to throw it together in [about] 15 minutes.  You get to copy my code and implement it in 5 minutes, hence the subject.

Overview:  This is running on a LINUX server running apache, with a modern PHP and ColdFusion 6.1.  Your server may not have the same specs, so this code won't work.  If so, sorry it didn't work out.

This solution is to create a directory with images in it (jpegs for example).  To make it a gallery, we throw in an index.cfm page that uses <cfdirectory> to get a list of images to display.  We then loop through the listing, and for each image, generate a link and thumbnail using a nifty php script.  The php script creates a thumbnail on the fly and stores it in a subdir, and then displays it.  Cool, huh? 

WARNING: This solution is not pretty-looking.  Thats something you can do with your precious "more than 15 minutes" allocated time slot.

Alright, here's the code:

index.cfm:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
        <title>Image Index</title>
</head>

<body style="font-family:sans-serif; font-size:9pt;">

<cfdirectory name="proof_files" action="LIST" directory="#ExpandPath(".")#">

<cfset cols="3">

<strong>Files by name:</strong><br>
<table>
<tr>
    <cfset idx = -1>
    <cfloop query="proof_files">
    <cfif (Name contains ".jpg")>
        <cfset idx = idx + 1>
        <cfif (idx mod cols) IS 0></tr><tr></cfif>
        <cfoutput>
        <td>
        <a href="#proof_files.name#">
        <img src="resize.php?main_file=#proof_files.name#"><br>
        #proof_files.name#</a></td>
        </cfoutput>
    </cfif>
    </cfloop>
</tr>
</table>

</body>
</html>

resize.php:

<?php

$image = "./" . $_GET['main_file'];
$savelocation = "./thumbs/";

if(!file_exists("$savelocation"))
{
  $oldumask = umask(0);
  mkdir("$savelocation", 0777);
  umask($oldumask);
}

if (file_exists($savelocation . "thumb_" . $_GET['main_file']))
{
  header('Location: thumbs/thumb_' . $_GET['main_file'] );
  exit();
}

if (!$max_width)
  $max_width = 125;
if (!$max_height)
  $max_height = 125;
$size = GetImageSize($image);
$width = $size[0];
$height = $size[1];
$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;
if ( ($width <= $max_width) && ($height <= $max_height) ) {
  $tn_width = $width;
  $tn_height = $height;
}
else if (($x_ratio * $height) < $max_height) {
  $tn_height = ceil($x_ratio * $height);
  $tn_width = $max_width;
}
else {
  $tn_width = ceil($y_ratio * $width);
  $tn_height = $max_height;
}
$src = ImageCreateFromJpeg($image);
$dst = ImageCreate($tn_width,$tn_height);
ImageCopyResized($dst, $src, 0, 0, 0, 0,
    $tn_width,$tn_height,$width,$height);
ImageJpeg($dst,"$savelocation"."thumb_" . $_GET['main_file'] );
ImageDestroy($src);
ImageDestroy($dst);

header('Location: thumbs/thumb_' . $_GET['main_file'] );
?>

Directions:

    1. Make a directory on your web server
    2. Upload some jpg's in the new folder
    3. Copy index.cfm and resize.php into the same directory
    4. Open a web browser and check it out

Gotchas:

The first time I ran the php script, it choked on memory because PHP has a maximum amount of memory that a script request can use.  I had to increase this limit for it to load larger images.  The setting was in /etc/php.ini and looked like so:

memory_limit = 32M      ; Maximum amount of memory a script may consume (default = 8MB)

Also, the color palletes of the thumbnails leave a little to be desired.  Oh well, at least it worked and we didnt waste too much time.

This script can bog down the server if you are generating a lot of thumbnails at once,  however the resize.php script does check to see if a thumbnail has been generated before, and if so, does not generate another one, so the overhead you experience should only be the first time a thumbnail is generated for a new image.