Printing thumbnails out of a loop ?
Posted: Wed Feb 03, 2010 9:41 am
Hi all,
I've got a multiple upload script which does this:
Upload 5 images to a dir. Count the images already in the dir and rename the new images incrementally (1.jpg, 2.jpg etc)
Make a thumbnail of each image. (sml_1.jpg, sml2.jpg etc))
I have a small problem which I can't seem to solve.
At the end of the script I want to print the 5 new thumbnails for the user to see.
I'm doing this (wrong) by declaring a variable "$thumbToPrint" but it's inside the loop so when the script finishes the html
prints the last thumb created 5 times.
Any ideas ?
Best wishes
Monty
I've got a multiple upload script which does this:
Upload 5 images to a dir. Count the images already in the dir and rename the new images incrementally (1.jpg, 2.jpg etc)
Make a thumbnail of each image. (sml_1.jpg, sml2.jpg etc))
I have a small problem which I can't seem to solve.
At the end of the script I want to print the 5 new thumbnails for the user to see.
I'm doing this (wrong) by declaring a variable "$thumbToPrint" but it's inside the loop so when the script finishes the html
prints the last thumb created 5 times.
Any ideas ?
Best wishes
Monty
Code: Select all
for ($i=0;$i<5;$i++)
{
if ((!empty($_FILES['userfile'])) && ($_FILES['userfile']['error'][$i] == 0)){
$image = $_FILES['userfile']['name'][$i];
$filename = stripslashes($_FILES['userfile']['name'][$i]);
$extension = getExtension($filename);
$extension = strtolower($extension);
//Filetype check
if (!in_array($extension,$acceptableextensions)){
exit("Upload failed.<BR>Unacceptable file type.<br. Use only jpg, jpeg, png or fig formats");
echo '<h1>Only try to upload .jpg, .jpeg, .png and .gif files!</h1>';
$errors=1;
//Filesize check
} else if ($_FILES['userfile']['size'][$i] > $maxfilesize ) {
$error_msg = 'Ooops - your image was too large,<br> 250kb Maximum. Please try again.';
unlink($_FILES['userfile']['tmp_name'][$i]);
} else {
// rename the picture incrementally
$extension = getExtension($filename);
$extension = strtolower($extension);
$groovy = sizeof(glob("members/$id/images/course_pics/*"));
$groovy = ++$groovy;
$image_name=$groovy.'.'.$extension;
$newname="".$image_name;
// NOW make the resize call !
//img_resize ($_FILES ['fileField'] [ 'name'], $_FILES [ 'fileField'] [ 'tmp_name'], 25, $newname);
// INSERT image into the DB
//$sql = mysql_query("INSERT INTO pictures
//(UID,filename,dirpath,gallery,dtadded)
//VALUES ('$id','$newname','/members/$id/images/$newname','$gowhere',now())")
//or die (mysql_error());
$place_file = move_uploaded_file( $_FILES['userfile']['tmp_name'][$i], "members/$id/images/course_pics/".$newname);
chmod ("members/$id/images/course_pics/$newname", 0644);
////////// MY RESIZE EFFORT !!
$save = "members/$id/images/course_pics/" .$newname; //This is the new file you saving
$file = "members/$id/images/course_pics/" .$newname; //This is the original file
list($width, $height) = getimagesize($file) ;
$modwidth = 750;
$diff = $width / $modwidth;
$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
imagejpeg($tn, $save, 100) ;
///////// NOW MAKE A THUMBNAIL OF THE IMAGE
$save = "members/$id/images/course_pics/sml_" . $newname; //This is the new file you saving
$file = "members/$id/images/course_pics/" . $newname; //This is the original file
list($width, $height) = getimagesize($file) ;
$modwidth = 80;
$diff = $width / $modwidth;
$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
imagejpeg($tn, $save, 100) ;
$thumbToPrint = "<img src='members/$id/images/course_pics/sml_".$newname."'>";
$success_msg = '<span class="Big_Orange_Times">Your image has been uploaded. it may take a few moments to appear in your gallery, please be patient.<br>';
} else if ((!empty($_FILES['userfile'])) && ($_FILES['userfile']['error'][$i] == 4)){
//Error code 4: No image uploaded
$error_msg = 'Please browse for an image before you press Go.';
}
}
