Um this is my first ever attempt at a php script that does something kindof useful. I would apreciate it if some of you more experienced types could poke some holes in it that I havent spotted, and any major security flaws, preferably with fixes 🙂
Thanks a lot
mDm
<script language='javascript'>
function display(myimage) {
html = "<HTML><HEAD><TITLE>FullSize</TITLE>" +
"</HEAD><BODY LEFTMARGIN=0 " +
"MARGINWIDTH=0 TOPMARGIN=0 MARGINHEIGHT=0><CENTER>" +
"<IMG SRC='" + myimage + "' BORDER=0 NAME=image " +
"onload='window.resizeTo(document.image.width,document.image.height)'></CENTER></BODY></HTML>";
popup=window.open('','image','toolbar=0,location=0,directories=0,menuBar=0,scrollbars=0,resizable=1');
popup.document.open();
popup.document.write(html);
popup.document.focus();
popup.document.close()
};
</script>
<?PHP
/* xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
WHAT YOU NEED TO DO
all that really needs doing for this script is to change
$picDir to the pictures directory and $thumbDir to the
thumbnails directory.
If you don't have a thumbnails directory, you NEED to create
one, and make it writable by the webserver. It CANNOT be the
same as the pictures directory.
A temp directory is also required with write access granted,
at './temp/'.
$columns sets how many columns there are in each row of thumbnails
The .0 after the number is required. as are the / after the dir
names.
*/ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
$picDir = "./"; //where to check for pictures
$thumbDir = "./thumbnails/"; //where to check for thumbnails
$columns = 4.0;
function str_is_int($str) {
return 0 === strcmp($str , (int)$str);
}
if ( $handle = opendir($picDir) ) {
$picArray = array(); //if open directory ok, init array for filenames
while (false !== ($file = readdir($handle))) //read dir for pic files
{
if ($file != "." && $file != "..")
{ //filter only graphics files
if ( ereg(".jpg$",$file) || ereg(".JPG$",$file) || ereg(".gif$",$file)
|| ereg(".GIF$",$file) || ereg(".png$",$file) || ereg(".PNG$",$file ))
{
array_push($picArray, $file); //add filenames to array
}
}
}
$j = count($picArray);
echo("<table border=0><tr>");
$col_number = 0;
for ( $i=0 ; $i < $j ; $i++ )
{
$thumbnail = $thumbDir.$picArray[$i];
if( !file_exists($thumbnail) ) // check for existing thumbnail
{
$img_source = $picDir.$picArray[$i];
$img_dest = $thumbDir.$picArray[$i];
$img_quality = 75;
$size_limit = 190;
//load the original image and put its hieght/width in $img_info
$img_info = getimagesize( $img_source );
//$img_info ARRAY KEY
//1 = height
//2 = width
//3 = height and width string
$orig_height = $img_info[1]; //source height from $img_info
$orig_width = $img_info[0]; //source width from $img_info
$jpegQuality = $img_quality; //quality of the JPG
if(($orig_width > $size_limit) || ($orig_height > $size_limit)) //make sure the image isnt already resized
{
if ( $orig_height > $orig_width )
$scaledown = $orig_height;
else
$scaledown = $orig_width;
$newscale = $size_limit / $scaledown; //set the new scale size
//calculate the new aspect ratio
$new_w = abs($orig_width * $newscale);
$new_h = abs($orig_height * $newscale);
//create the blank limited-palette image
$base_image = imageCreate($new_w, $new_h);
//convert and save it to temp.jpg
imagejpeg($base_image, './temp/temp.jpg');
//get the image pointer to the temp jpeg
$image = imageCreateFromJpeg('./temp/temp.jpg');
$dir = $picDir; // dir("./$img_source"); //directory path of the orig image
// get the image pointer to the original image
$imageToResize = imageCreateFromJpeg("./$img_source");
//resize the original image over temp.jpg
// NOTE: of you have GD2, you could also use imageCopyResampled
imageCopyResized($image, $imageToResize, 0, 0, 0, 0, $new_w, $new_h, $orig_width, $orig_height);
//create the resized image
imageJpeg($image, "$img_dest", $jpegQuality); //image destination
unlink('./temp/temp.jpg'); //del the temp image file
}
}
else
{
$img_info = getimagesize( $picDir . $picArray[$i] );
}
$col_number++;
$newrowtest = ($col_number/$columns);
echo ( "<td><a href=\"javascript:display('$picDir$picArray[$i]')\"><img src='$thumbDir$picArray[$i]' border=0></a></td>" );
if ( str_is_int($newrowtest) ) //&& $col_number !== 5
{
echo ( "</tr><tr>" );
}
}
echo("</tr></table>");
closedir($handle);
}
else echo"can't open directory";
?>