jaxdevil Posted April 10, 2008 Share Posted April 10, 2008 Ok, my code below is not working for generating thumbnail images. The script uploads the files, creates the 'thumbs' directory, but the thumbnails are not created. Where did I go wrong? <?php mkdir("../ebay/$ebay_task/thumbs", 0777); ?> <?php $path1= "$ebay_task/".$HTTP_POST_FILES['ufile']['name'][0]; $path2= "$ebay_task/".$HTTP_POST_FILES['ufile']['name'][1]; $path3= "$ebay_task/".$HTTP_POST_FILES['ufile']['name'][2]; copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1); copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2); copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3); echo "File Name :".$HTTP_POST_FILES['ufile']['name'][0]."<BR/>"; echo "File Size :".$HTTP_POST_FILES['ufile']['size'][0]."<BR/>"; echo "File Type :".$HTTP_POST_FILES['ufile']['type'][0]."<BR/>"; echo "<img src=\"$path1\" width=\"150\" height=\"150\">"; echo "<P>"; echo "File Name :".$HTTP_POST_FILES['ufile']['name'][1]."<BR/>"; echo "File Size :".$HTTP_POST_FILES['ufile']['size'][1]."<BR/>"; echo "File Type :".$HTTP_POST_FILES['ufile']['type'][1]."<BR/>"; echo "<img src=\"$path2\" width=\"150\" height=\"150\">"; echo "<P>"; echo "File Name :".$HTTP_POST_FILES['ufile']['name'][2]."<BR/>"; echo "File Size :".$HTTP_POST_FILES['ufile']['size'][2]."<BR/>"; echo "File Type :".$HTTP_POST_FILES['ufile']['type'][2]."<BR/>"; echo "<img src=\"$path3\" width=\"150\" height=\"150\">"; /////////////////////////////////////////////////////// // Use this code to display the error or success. $filesize1=$HTTP_POST_FILES['ufile']['size'][0]; $filesize2=$HTTP_POST_FILES['ufile']['size'][1]; $filesize3=$HTTP_POST_FILES['ufile']['size'][2]; if($filesize1 && $filesize2 && $filesize3 != 0) { echo "Images Uploaded Successfully"; } else { echo "ERROR....."; } ////////////////////////////////////////////// // What files that have a problem? (if found) if($filesize1==0) { echo "There're something error in your first file"; echo "<BR />"; } if($filesize2==0) { echo "There're something error in your second file"; echo "<BR />"; } if($filesize3==0) { echo "There're something error in your third file"; echo "<BR />"; } ?> <?php function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth ) { // open the directory $dir = opendir( $pathToImages ); // loop through it, looking for any/all JPG files: while (false !== ($fname = readdir( $dir ))) { // parse path for the extension $info = pathinfo($pathToImages . $fname); // continue only if this is a JPEG image if ( strtolower($info['extension']) == 'jpg' ) { echo "Creating thumbnail for {$fname} <br />"; // load image and get image size $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" ); $width = imagesx( $img ); $height = imagesy( $img ); // calculate thumbnail size $new_width = $thumbWidth; $new_height = floor( $height * ( $thumbWidth / $width ) ); // create a new temporary image $tmp_img = imagecreatetruecolor( $new_width, $new_height ); // copy and resize old image into new image imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height ); // save thumbnail into a file imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" ); } } // close the directory closedir( $dir ); } createThumbs('/$ebay_task','$ebay_task/thumbs',100); ?> Quote Link to comment https://forums.phpfreaks.com/topic/100485-generate-thumbnails-of-images-after-uploading/ Share on other sites More sharing options...
Daniel0 Posted April 10, 2008 Share Posted April 10, 2008 Check out this post by craygo: http://www.phpfreaks.com/forums/index.php/topic,191541.msg861737.html Quote Link to comment https://forums.phpfreaks.com/topic/100485-generate-thumbnails-of-images-after-uploading/#findComment-513874 Share on other sites More sharing options...
jaxdevil Posted April 10, 2008 Author Share Posted April 10, 2008 Thanks, but I want to keep the original image and create the thumbnail in a new directory. I don't want to resize the image, and I can't draw the image dynamically I need to generate it at the same time of upload on the same page. Any other ideas? I appreciate the help. SK Quote Link to comment https://forums.phpfreaks.com/topic/100485-generate-thumbnails-of-images-after-uploading/#findComment-513884 Share on other sites More sharing options...
Daniel0 Posted April 10, 2008 Share Posted April 10, 2008 You could just save it instead of outputting it. Quote Link to comment https://forums.phpfreaks.com/topic/100485-generate-thumbnails-of-images-after-uploading/#findComment-513885 Share on other sites More sharing options...
jaxdevil Posted April 10, 2008 Author Share Posted April 10, 2008 But I am also doing three at once. The code I have above would resize all the images in that folder, and I will not know there filenames to put into the script. Any ideas? Thanks, SK Quote Link to comment https://forums.phpfreaks.com/topic/100485-generate-thumbnails-of-images-after-uploading/#findComment-513888 Share on other sites More sharing options...
craygo Posted April 10, 2008 Share Posted April 10, 2008 So if I am reading correctly. You are uploading images, you want to save the image as it is into a directory, then create a thumbnail of each image uploaded in another directory. Correct?? Ray Quote Link to comment https://forums.phpfreaks.com/topic/100485-generate-thumbnails-of-images-after-uploading/#findComment-513933 Share on other sites More sharing options...
jaxdevil Posted April 10, 2008 Author Share Posted April 10, 2008 Yes, thats exactly what i am trying to do. it doesn't necessarily 'have' to be in another directory, I can even use it appended with a tn_ or something on the filename in the same directory. Any idea? Thanks a ton, SK Quote Link to comment https://forums.phpfreaks.com/topic/100485-generate-thumbnails-of-images-after-uploading/#findComment-513944 Share on other sites More sharing options...
craygo Posted April 10, 2008 Share Posted April 10, 2008 Well I don't want to go messing with your code bu the script I wrote will work fine with a little fix <?php $image_path = "C:/Inetpub/wwwroot/phpforum/images/"; //Absolute path to where files are uploaded $thumb_path = "C:/Inetpub/wwwroot/phpforum/images/thumbs/"; //Absolute path to where thumbs are to be stored if you want this $size_limit = "yes"; //do you want a size limit yes or no. $limit_size = "2097152"; //How big do you want size limit to be in bytes $limit_ext = "yes"; //do you want to limit the extensions of files uploaded $ext_count = "8"; //total number of extensions in array below $extensions = array(".jpg", ".jpeg", ".png", ".gif", ".doc", ".pdf", ".txt", ".xls"); //List extensions you want files uploaded to be $im_array = array("image/x-png", "image/pjpeg", "image/gif", "image/jpeg"); //List of image types make sure image type extension is in above array function resampleimage($maxsize, $sourcefile, $destination, $filetype, $quality){ // CHECK TO SEE IF THE IMAGE EXISTS FIRST if(file_exists($sourcefile)){ // FIRST WE GET THE CURRENT IMAGE SIZE $g_is=getimagesize($sourcefile); /********* CALCULATE THE WIDTH AND THE HEIGHT ***************/ // CHECK TO SEE IF THE WIDTH AND HEIGHT ARE ALREADY SMALLER THAN THE MAX SIZE if($g_is[0] <= $maxsize && $g_is[1] <= $maxsize){ // LEAVE WIDTH AND HEIGHT ALONE IF IMAGE IS SMALLER THAN MAXSIZE $new_width=$g_is[0]; $new_height=$g_is[1]; } else { // GET VALUE TO CALCULATE WIDTH AND HEIGHT $w_adjust = ($maxsize / $g_is[0]); $h_adjust = ($maxsize / $g_is[1]); // CHECK TO WHICH DIMENSION REQUIRES THE SMALLER ADJUSTMENT if($w_adjust <= $h_adjust){ // CALCULATE WIDTH AND HEIGHT IF THE WIDTH VALUE IS SMALLER $new_width=($g_is[0]*$w_adjust); $new_height=($g_is[1]*$w_adjust); } else { // CALCULATE WIDTH AND HEIGHT IF THE HEIGHT VALUE IS SMALLER $new_width=($g_is[0]*$h_adjust); $new_height=($g_is[1]*$h_adjust); } } //SWITCHES THE IMAGE CREATE FUNCTION BASED ON FILE EXTENSION switch($filetype) { case 'image/pjpeg': $img_src = imagecreatefromjpeg($sourcefile); break; case 'image/jpeg': $img_src = imagecreatefromjpeg($sourcefile); break; case 'image/x-png': $img_src = imagecreatefrompng($sourcefile); break; case 'image/gif': $img_src = imagecreatefromgif($sourcefile); break; default: echo("Error Invalid Image Type"); die; break; } // CREATE THE TRUE COLOR IMAGE WITH NE WIDTH AND HEIGHT $img_dst=imagecreatetruecolor($new_width,$new_height); // RESAMPLE THE IMAGE TO NEW WIDTH AND HEIGHT imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $new_width, $new_height, $g_is[0], $g_is[1]); // OUTPUT THE IMAGE AS A JPEG. // THIS CAN BE CHANGED IF YOU WANT TRANSPARENCY OR PREFER ANOTHER FORMAT. MAKE SURE YOU CHANGE HEADER ABOVE. imagejpeg($img_dst, $destination, $quality); // DESTROY THE NEW IMAGE imagedestroy($img_dst); return true; } else { return false; } } if(!isset($_POST['submit'])){ $extens = ''; if (($extensions == "") or ($extensions == " ") or ($ext_count == "0") or ($ext_count == "") or ($limit_ext != "yes") or ($limit_ext == "")) { $extens = "any extension"; } else { $ext_count2 = $ext_count+1; for($counter=0; $counter<$ext_count; $counter++) { $extens .= " $extensions[$counter]"; } } if (($limit_size == "") or ($size_limit != "yes")) { $limit_size = "any size"; } else { $limit_size .= " bytes"; $mb_size = ($limit_size/1048576); } $pichead = "<li><font size=\"2\" color=660000>File extension must be $extens<b>"; $pichead .="</b></font> <li><font size=\"2\" color=660000>Maximum file size is $limit_size ($mb_size MB)</font></li> <li><font size=\"2\" color=660000>No spaces in the filename</font></li>"; ?> <html> <head> <title>HTML Form for uploading image to server</title> </head> <body> <form action="" method="post" enctype="multipart/form-data"> <html> <title>Upload Files</title> <body> <p><? echo $pichead; ?></p> <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> <p>Files:<br /> <?php $files = 3; // Change this line for the number of upload you want to allow for($i=1; $i<=$files; $i++){ echo "File $i <input type=\"file\" name=\"files[]\" /><br />\n"; } ?> <input type="submit" name=submit value="Send" /> </p> </form> <?php } else { $i=0; $quality = "75"; // change the quality here foreach ($_FILES["files"]["error"] as $key => $error) { $file_name = $_FILES["files"]['name'][$i]; // can call this anything you like this will take the original name $file = $_FILES["files"]['tmp_name'][$i]; $file_size = $_FILES["files"]['size'][$i]; $file_type = $_FILES["files"]['type'][$i]; $endresult = "<font size=\"4\" color=990000>$file_name uploaded successfully</font>"; if ($file_name == "") { $pic = $i+1; $endresult = "<font size=\"4\" color=990000>Pic#$pic Not selected</font>"; }else{ if(file_exists("$file_path/$file_name") || file_exists("$image_path/$file_name") || file_exists("$thumb_path/$file_name")) { $endresult = "<font size=\"4\" color=990000>File Already Existed in your home directory</font>"; } else { if (($size_limit == "yes") && ($limit_size < $file_size)) { $endresult = "<font size=\"4\" color=990000>File was to big</font>"; } else { $ext = strrchr($file_name,'.'); if (($limit_ext == "yes") && (!in_array($ext,$extensions))) { $endresult = "<font size=\"4\" color=990000>File is wrong type</font>"; }else{ move_uploaded_file($file, $image_path.$file_name); // Move the file to the folder just as it is // Save thumb image with max width/height of 200 resampleimage("200", $file, $thumb_path.$file_name, $file_type, $quality); // Comment this line out if you don't want to create a thumbnail, change the 200 to the size you want } } } } $i++; echo $endresult."<br>"; } } ?> </body> </html> Ray Quote Link to comment https://forums.phpfreaks.com/topic/100485-generate-thumbnails-of-images-after-uploading/#findComment-513957 Share on other sites More sharing options...
jaxdevil Posted April 10, 2008 Author Share Posted April 10, 2008 Thanks, I think I can work with that no prob Thank you! SK Quote Link to comment https://forums.phpfreaks.com/topic/100485-generate-thumbnails-of-images-after-uploading/#findComment-513987 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.