scoobyr1 Posted November 11, 2007 Share Posted November 11, 2007 Hi, I am fairly new to PHP and I am having problems with an image upload script that resizes the image, saving the original and a thumbnail - I end up with a thumbnail as expected, however the main image does not get resized and has the wrong read write attributes (600) rw------ preventing the image from displaying! I have tried using chmod however without success. My upload script has an include which handles this topic; I have listed this first, the upload.php follows. Thanking you in advance! Include.php <?php ######################################## # include the getNextFilename function # ######################################## include('../includes/getNextFilename5.php'); ######################################## // define constants define('THUMBS_DIR', '../images/thumbs/'); define('MAX_WIDTH', 120); define('MAX_HEIGHT', 90); // process the uploaded image if (is_uploaded_file($_FILES['image']['tmp_name'])) { $original = $_FILES['image']['tmp_name']; // begin by getting the details of the original list($width, $height, $type) = getimagesize($original); // calculate the scaling ratio if ($width <= MAX_WIDTH && $height <= MAX_HEIGHT) { $ratio = 1; } elseif ($width > $height) { $ratio = MAX_WIDTH/$width; } else { $ratio = MAX_HEIGHT/$height; } // strip the extension off the image filename $imagetypes = array('/\.gif$/', '/\.jpg$/', '/\.jpeg$/', '/\.png$/'); $name = preg_replace($imagetypes, '', basename($_FILES['image']['name'])); ######################################################## # getNextFilename() requires the file type as a string # # so begin by converting it to a string assigned to $t # ######################################################## switch($type) { case 1: $t = 'gif'; break; case 2: $t = 'jpg'; break; case 3: $t = 'png'; } ################################ # get new name for upload file # ################################ $newName = getNextFilename5(UPLOAD_DIR, $name, $t); // use getNextFilename4() on a PHP 4 server ################################################ # use the new name instead of the existing one # ################################################ // move the temporary file to the upload folder $moved = move_uploaded_file($original, UPLOAD_DIR.$newName); if ($moved) { $result = "$newName successfully uploaded; "; $original = UPLOAD_DIR.$newName; } else { $result = 'Problem uploading '.$_FILES['image']['name'].'; '; } ################################################ // create an image resource for the original switch($type) { case 1: $source = @ imagecreatefromgif($original); if (!$source) { $result = 'Cannot process GIF files. Please use JPEG or PNG.'; } break; case 2: $source = imagecreatefromjpeg($original); break; case 3: $source = imagecreatefrompng($original); break; default: $source = NULL; $result = 'Cannot identify file type.'; } // make sure the image resource is OK if (!$source) { $result = 'Problem copying original'; } else { // calculate the dimensions of the thumbnail $thumb_width = round($width * $ratio); $thumb_height = round($height * $ratio); // create an image resource for the thumbnail $thumb = imagecreatetruecolor($thumb_width, $thumb_height); // create the resized copy imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height); // save the resized copy switch($type) { case 1: ###################################################### # use the new name as the basis for the thumb's name # ###################################################### $name = basename($newName, '.gif'); ###################################################### if (function_exists('imagegif')) { $success = imagegif($thumb, THUMBS_DIR.$name.'.gif'); $thumb_name = $name.'.gif'; } else { $success = imagejpeg($thumb, THUMBS_DIR.$name.'.jpg', 50); $thumb_name = $name.'.jpg'; } break; case 2: ###################################################### # use the new name as the basis for the thumb's name # ###################################################### $name = basename($newName, '.jpg'); ###################################################### $success = imagejpeg($thumb, THUMBS_DIR.$name.'.jpg', 100); $thumb_name = $name.'.jpg'; break; case 3: ###################################################### # use the new name as the basis for the thumb's name # ###################################################### $name = basename($newName, '.png'); ###################################################### $success = imagepng($thumb, THUMBS_DIR.$name.'.png'); $thumb_name = $name.'.png'; } if ($success) { $result .= "$thumb_name created"; } else { $result .= 'Problem creating thumbnail'; } // remove the image resources from memory imagedestroy($source); imagedestroy($thumb); } } ?> upload.php <?php include('../includes/timeout.inc.php'); ?> <?php // if session variable not set, redirect to login page if (!isset($_SESSION['authenticated'])) { header('Location: ../sessions/login.php'); exit; } // define a constant for the maximum upload size define ('MAX_FILE_SIZE', 71680); if (array_key_exists('upload', $_POST)) { // define constant for upload folder define('UPLOAD_DIR', '../images/gallery/'); // replace any spaces in original filename with underscores // at the same time, assign to a simpler variable $file = str_replace(' ','_',$_FILES['image']['name']); // convert the maximum size to KB $max = number_format(MAX_FILE_SIZE/1024, 1).'KB'; // create an array of permitted MIME types $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png'); // begin by assuming the file is unacceptable $sizeOK = false; $typeOK = false; // check that file is within the permitted size if ($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) { $sizeOK = true; } // check that file is of a permitted MIME type foreach ($permitted as $type) { if ($type == $_FILES['image']['type']) { $typeOK = true; break; } } if ($sizeOK && $typeOK) { switch($_FILES['image']['error']) { case 0: ##################################################### # include the script that assigns new names to both # ##################################################### include('../includes/create_both_new.inc.php'); ##################################################### break; case 3: $result = "Error uploading $file. Please try again."; default: $result = "System error uploading $file. Contact webmaster."; } } elseif ($_FILES['image']['error'] == 4) { $result = 'No file selected'; } else { $result = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png."; } } ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>File upload</title> </head> <body> <?php // if the form has been submitted, display result if (isset($result)) { echo "<p><strong>$result</strong></p>"; } ?> <form action="" method="post" enctype="multipart/form-data" name="uploadImage" id="uploadImage"> <p> <label for="image">Upload image:</label> <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" /> <input type="file" name="image" id="image" /> </p> <p> <input type="submit" name="upload" id="upload" value="Upload" /> </p> </form> <p><a href="../sessions/menu.php">Back to restricted menu</a> </p> <?php include('../includes/logout.inc.php'); ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
scoobyr1 Posted November 11, 2007 Author Share Posted November 11, 2007 *bump* Quote Link to comment Share on other sites More sharing options...
scoobyr1 Posted November 11, 2007 Author Share Posted November 11, 2007 *bump* Quote Link to comment Share on other sites More sharing options...
jordanwb Posted November 12, 2007 Share Posted November 12, 2007 First off welcome to the forum. When posting code use the code BB button in the menu (the number sign #). Where is the chmod function? I couldn't find it in the code. the chmod must be called in the following manner: chmod ($file_source, 0744); 7 meaning all permissions for server, and 4 meaning read permissions for user and group (whatever group is for). A note though that the file source has to be relative the script for eaxmple: /includes/upload.php <- has upload function /images/foo.jpg /index.php (includes: includes/upload.php and calls the uplaod function) the parameter would be images/foo.jpg Hope this helps. Quote Link to comment Share on other sites More sharing options...
scoobyr1 Posted November 12, 2007 Author Share Posted November 12, 2007 Thank you for the greeting Jordan and for your asistance. i will remember to post the code using the BB Button next time! I removed the chkmod function as I couldn't get it to work, so you won't find it in the code! The problem is two fold, firstly I don't know the final image name as it is stored in a variable after a numerical suffix is added. I am not sure which variable to use in the chkmon function as it changes. Secondly the main image is not resizing which I believe is down the the permissions, therefore I beleieve I need to use the chkmod function before the image resize. The thumbnail is resizing and has the right permissions which I think has something to do with it being a copy! I hope someone canhelp me, this is very frustrating! Quote Link to comment 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.