I'm trying to write a php script that does three main things:
Parses a directory and opens the 3 most recently modified files
Resizes thee images into thumbnail-size images and saves them in a separate directory
Prints the the three resized images to the browser.
In its current form the script sucessfully does all three steps for the first image, but the other two seem to get echoed as a raw data stream rather than as an image the browser can read.
I'm relatively new to php, and I can't seem to find the flaw in the scripting. PErhaps someone can help me out?
<?php
//Scripted by Floyd Wright 2002 floyd@unfocused.org
function RecentFiles ($dir)
{
chdir ($dir);
$handle = opendir(".");
$c = 0;
$date = array();
$files = array();
while ($file = readdir ($handle))
{
if ($file != "." && $file != ".." && $file != "index.php" && $file != "index2.php")
{
$date[$c] = filemtime ($file);
$files[$date[$c]] = $file;
$c++;
}
}
rewinddir ($handle);
closedir ($handle);
rsort ($date);
ob_start();
for ($c=0; $c<=2; $c++)
{
$file = $files[$date[$c]];
$srcImage = ImageCreateFromJPEG ($file);
$srcWidth = ImageSX ($srcImage);
$srcHeight = ImageSY ($srcImage);
$ratioWidth = $srcWidth/100;
$ratioHeight = $srcHeight/100;
if ($ratioWidth < $ratioHeight)
{
$destWidth = $srcWidth/$ratioHeight;
$destHeight = 100;
}
else
{
$destWidth = 100;
$destHeight = $srcHeight/$ratioWidth;
}
$destImage = ImageCreate ($destWidth, $destHeight);
ImageCopyResized ($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight);
ImageJPEG ($destImage, "", 115);
ImageDestroy ($srcImage);
ImageDestroy ($destImage);
$resizedImage = ob_get_contents();
$output="/home/unfocused/www/spas/features/feature$c.jpg";
touch ($output);
if (unlink ($output))
{
if (touch ($output))
{
$fp = fopen ($output, "w");
if (!$fp)
{
print ("failed to open file");
}
fwrite ($fp, $resizedImage);
fclose ($fp);
}
}
ob_end_clean();
$location = $dir;
$location = str_replace ("/home/unfocused/www/spas/", "", $location);
print ("<A HREF=\"view.php?$location$file\"><IMG SRC=\"features/feature$c.jpg\" BORDER=\"2\" BORDERCOLOR=\"BLACK\"></A><BR><BR>");
}
}
?>
<?PHP RecentFiles ("/home/unfocused/www/spas/aaron/a001/"); ?>