Eller noget lidt kortere på denne måde:
<?php
ini_set("memory_limit","64M");
echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Strict //EN\" \"
http://www.w3.org/TR/html4/strict.dtd\">\n";echo "<html>\n";
echo "<head>\n";
echo "<title>Resize</title>\n";
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">\n";
echo "</head>\n";
echo "<body>\n";
echo "<div id=\"content\">\n";
echo "<h1>Resize</h1>\n";
function resize( $filename, $newfilename, $maxw, $maxh )
{
$result = false;
$srcim = imagecreatefromjpeg( $filename );
$ow = imagesx( $srcim );
$oh = imagesy( $srcim );
$wscale = $maxw / $ow;
$hscale = $maxh / $oh;
$scale = ( $hscale < $wscale ? $hscale : $wscale );
$nw = round( $ow * $scale, 0 );
$nh = round( $oh * $scale, 0 );
$dstim = imagecreatetruecolor( $nw, $nh );
imagecopyresampled( $dstim, $srcim, 0, 0, 0, 0, $nw, $nh, $ow, $oh );
$result = imagejpeg( $dstim, $newfilename, 85 );
imagedestroy( $dstim );
imagedestroy( $srcim );
return $result;
}
$dir = getcwd();
$allowExt = array("jpg","JPG");
// Open a known directory, and proceed to read its contents
if (is_dir($dir))
{
if ($dh = opendir($dir))
{
$fileArray = array();
while(($file = readdir($dh)) !== false)
{
$arr = explode (".", $file);
$extension = end($arr);
if(in_array($extension,$allowExt))
{
if(file_exists("thumb_" . $file) || preg_match('/^thumb_/', $file))
{
;
}
else
{
resize($file, getcwd() . "/thumb_" . $file, 300, 300);
}
if(!preg_match('/^thumb_/', $file))
{
$thumb = "thumb_" . $file;
$fileArray[$thumb] = $file;
}
}
}
closedir($dh);
}
}
if(count($fileArray)>0)
{
echo "<table id=\"centerTable\">\n";
foreach($fileArray as $thumb => $file)
{
echo "<tr><td><a target=\"_blank\" href=\"" . $file . "\"><img src=\"".$thumb."\"></a></td></tr>\n";
}
echo "</table>\n";
}
else
{
echo "Vi har ikke uploadet nogle billeder endnu, de kommer...";
}
echo "</div>\n";
echo "</body>\n";
echo "</html>\n";
?>
PS: Den tager kun jpeg billeder.