#!/usr/local/bin/php
# EXAMPLE OUPTUT FILENAME:
# 07-16-04_XXX_XXXX_126w_227h.gif
# Set the PATH
putenv("PATH=" . $_SERVER['PATH'] . ":/usr/local/bin");
# Set the LD_LIBRARY_PATH
# Must put the library path in for ImageMagic/Ghostscript
putenv("LD_LIBRARY_PATH=" . $_SERVER['LD_LIBRARY_PATH'] . ":/usr/local/lib");
# Get the first argument
# die unless argument was supplied
# EXAMPLE INPUT FILENAME:
# /PDF/08-25-04_XXX_XXXX_XX_XXX_1_1.pdf
if (array_key_exists('argv', $_SERVER) && array_key_exists(1,$_SERVER['argv'])) {
$filename = $_SERVER['argv'][1];
} else {
die("Must supply atleast one file via command line\n");
}
# Globalish variables
#
# $filecomponents = an array of each part of the base filename
$filecomponents = array();
# Make a new thumbnails element for each desired size
$thumbnails[] = array(
"width" => 126,
"height" => 227
);
$thumbnails[] = array(
"width" => 240,
"height" => 433
);
$thumbnails[] = array(
"width" => 74,
"height" => 131
);
# Setup FTP Connections
$ftpconfig[] = array(
"host" => "host1.foo.bar",
"user" => "ftpuser",
"password" => "ftppass",
"path" => "/any/place/you/like",
"retries" => 5,
"retry_delay" => 10,
);
$ftpconfig[] = array(
"host" => "host2.foo.bar",
"user" => "ftpuser",
"password" => "ftppass",
"path" => "/any/other/place/you/like",
"retries" => 5,
"retry_delay" => 10,
);
$ftpconfig[] = array(
"host" => "host3.foo.bar",
"user" => "ftpuser",
"password" => "ftppass",
"path" => "/even/an/offsite/backup/location",
"pasv" => true,
"retries" => 5,
"retry_delay" => 10,
);
###
# Begin Functions
# Generate the new thumbnail name
function new_thumb_name($filename,$width,$height) {
# Get just the filename, cut off the extension
$base = basename($filename,".pdf");
$filecomponents = explode("_",$base);
# Generate the file name using the agreed upon standard
$newfilename = sprintf("%s_%s_%s_%sw_%sh.gif",$filecomponents[0],$filecomponents[1],$filecomponents[2],$width,$height);
# Return the filename
return $newfilename;
}
# Generate the ftp destination name
function send_via_ftp(&$files,&$ftpconfig) {
foreach ($ftpconfig as $ftp) {
# Reset the loop counter to 1
$i = 0;
# Do the loop x number of times
while (++$i <= $ftp['retries']) {
# set up basic connection
$conn_id = ftp_connect(strval($ftp['host']));
# If we successfully connected...
if ($conn_id) {
# login with username and password
$login_result = ftp_login($conn_id, $ftp['user'], $ftp['password']);
# Needs PASV?
# If PASV is set in the ftpconfig block use it.
if (array_key_exists('pasv',$ftp) && $ftp['pasv']) {
ftp_pasv($conn_id,true);
}
}
# check connection
if ((!$conn_id) || (!$login_result)) {
fwrite(STDERR,"FTP connection has failed! Attempt #$i of " . $ftp['retries'] . "\n");
fwrite(STDERR,"Attempted to connect to " . $ftp['host'] . " for user " . $ftp['user'] . "\n");
if ($conn_id) {ftp_close($conn_id);}
sleep($ftp['retry_delay']);
continue;
} else {
foreach ($files as $file) {
# upload the file
$upload = ftp_put($conn_id, basename($file), $file, FTP_BINARY);
# check upload status
if (!$upload) {
fwrite(STDERR,"FTP upload of $file to " . $ftp['host'] . " has failed!\n");
reset($files);
ftp_close($conn_id);
sleep($ftp['retry_delay']);
continue;
}
}
reset($files);
}
# close the FTP stream
ftp_close($conn_id);
break;
}
}
}
# End Functions
###
###
# Begin Main Code
foreach ($thumbnails as $thumbnailarray) {
$destinations = array();
$width = $thumbnailarray['width'];
$height = $thumbnailarray['height'];
$newfilename = new_thumb_name($filename,$width,$height);
$newfile = "/PDF/Thumbnails/" . $newfilename;
# Use ImageMagic+Ghostscript to do the thumbnail generation
# at desired height/width
$error = `/usr/local/bin/convert -trim -thumbnail ${width}x${height} "$filename" $newfile`;
$files[] = $newfile;
}
send_via_ftp(&$files,&$ftpconfig);
?>