RyanMinor Posted August 1, 2010 Share Posted August 1, 2010 I have this script that I am using to upload new video galleries to a website. The script allows the user to upload an image that will display when they click on the gallery. As of now the image gets uploaded and a thumbnail is created automatically and stored in the appropriate places. The original image however remains its original dimensions. How would I tweak this script to resize the original image to MAX_WIDHT = 400px and MAX_HEIGHT = 750px along with keeping the create thumbnail feature? <?php require_once('Connections/DBConnect.php'); ?> <?php session_start(); if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "uploadvideo")) { // make the gallery name available for the result message $video = $_POST['videocaption']; // define a constant for the maximum upload size define ('MAX_FILE_SIZE', 256000); if (array_key_exists('upload', $_POST)) { // define constant for upload folder define('UPLOAD_DIR', 'C:/wamp/www/test/videos/video_photos/'); // replace any spaces in original filename with underscores // at the same time, assign to a simpler variable $file = str_replace(' ', '_', $_FILES['videophotoname']['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['videophotoname']['size'] > 0 && $_FILES['videophotoname']['size'] <= MAX_FILE_SIZE) { $sizeOK = true; } // check that file is of an permitted MIME type foreach ($permitted as $type) { if ($type == $_FILES['videophotoname']['type']) { $typeOK = true; break; } } if ($sizeOK && $typeOK) { switch($_FILES['videophotoname']['error']) { case 0: // define constants define('THUMBS_DIR', 'C:/wamp/www/test/videos/video_photos/thumbs/'); define('MAX_WIDTH', 150); define('MAX_HEIGHT',225); // process the uploaded image if (is_uploaded_file($_FILES['videophotoname']['tmp_name'])) { $original = $_FILES['videophotoname']['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['videophotoname']['name'])); // move the temporary file to the upload folder $moved = move_uploaded_file($original, UPLOAD_DIR.$_FILES['videophotoname']['name']); if ($moved) { $result = $_FILES['videophotoname']['name'].' successfully uploaded; '; $original = UPLOAD_DIR.$_FILES['videophotoname']['name']; } else { $result = 'Problem uploading '.$_FILES['videophotoname']['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: if (function_exists('imagegif')) { $success = imagegif($thumb, THUMBS_DIR.$name.'_thb.gif'); $thumb_name = $name.'_thb.gif'; } else { $success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 50); $thumb_name = $name.'_thb.jpg'; } break; case 2: $success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 100); $thumb_name = $name.'_thb.jpg'; break; case 3: $success = imagepng($thumb, THUMBS_DIR.$name.'_thb.png'); $thumb_name = $name.'_thb.png'; } if ($success) { $insertSQL = sprintf("INSERT INTO tblmembervideo (videophotoname, videothumbname, videodescription, videocaption, modelid, `date`) VALUES (%s, %s, %s, %s, %s, %s)", GetSQLValueString($_FILES['videophotoname']['name'], "text"), GetSQLValueString($thumb_name, "text"), GetSQLValueString($_POST['videodescription'], "text"), GetSQLValueString($_POST['videocaption'], "text"), GetSQLValueString($_POST['modelid'], "int"), GetSQLValueString($_POST['date'], "date")); mysql_select_db($database_DBConnect, $DBConnect); $Result1 = mysql_query($insertSQL, $DBConnect) or die(mysql_error()); $videoid = mysql_insert_id(); $_SESSION['videoid'] = $videoid; $result .= "$thumb_name created and $video uploaded. Click <a href='addclip.php'>here</a> to add clips."; } else { $result .= 'Problem creating thumbnail'; } // remove the image resources from memory imagedestroy($source); imagedestroy($thumb); } } break; case 3: $result = "Error uploading $file. Please try again."; default: $result = "System error uploading $file. Contact webmaster."; } } elseif ($_FILES['videophotoname']['error'] == 4) { $result = 'No file selected'; } else { $result = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png."; } } } mysql_select_db($database_DBConnect, $DBConnect); $query_rsgetmodel = "SELECT modelid, modelname FROM tblmembermodel"; $rsgetmodel = mysql_query($query_rsgetmodel, $DBConnect) or die(mysql_error()); $row_rsgetmodel = mysql_fetch_assoc($rsgetmodel); $totalRows_rsgetmodel = mysql_num_rows($rsgetmodel); ?> <!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=utf-8" /> <title>Untitled Document</title> </head> <body> <div align="center"> <?php // if the form has been submitted, display result if (isset($result)) { echo "<p>$result</p>"; } ?> </div> <form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="uploadvideo" id="uploadvideo"> Select a Photo (thumbnail will automatically be created) for Video:<br /> <input name="MAX_FILE_SIZE" type="hidden" id="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" /> <input type="file" name="videophotoname" id="videophotoname" /> <br /> <br /> Video Caption:<br /> <input type="text" name="videocaption" id="videocaption" /> <br /> <br /> Video Description:<br /> <textarea name="videodescription" id="videodescription" cols="35" rows="3"></textarea> <br /> <br /> Model:<br /> <select name="modelid" id="modelid"> <?php do { ?> <option value="<?php echo $row_rsgetmodel['modelid']?>"><?php echo $row_rsgetmodel['modelname']?></option> <?php } while ($row_rsgetmodel = mysql_fetch_assoc($rsgetmodel)); $rows = mysql_num_rows($rsgetmodel); if($rows > 0) { mysql_data_seek($rsgetmodel, 0); $row_rsgetmodel = mysql_fetch_assoc($rsgetmodel); } ?> </select> <br /> <br /> <input type="submit" name="upload" id="upload" value="Add Video" /> <input name="date" type="hidden" id="date" value="<?php echo date ("Y-m-d H:m:s"); ?>" /> <input type="hidden" name="MM_insert" value="uploadvideo" /> <br /> </form> </body> </html> <?php mysql_free_result($rsgetmodel); ?> [code/] Quote Link to comment Share on other sites More sharing options...
TheChaosFactor Posted August 1, 2010 Share Posted August 1, 2010 http://highslide.com/tutorial If that's not quite what you're looking for there is also lightbox 2, and some other ones if you give a search on google. Quote Link to comment Share on other sites More sharing options...
RyanMinor Posted August 2, 2010 Author Share Posted August 2, 2010 Thanks for the link, but it isn't what I am looking for. I was just wondering if someone could take a look at my current code and help me adapt it to re-size the original image being uploaded as well as to create the thumbnail. I've tried but couldn't figure it out. Quote Link to comment Share on other sites More sharing options...
lemmin Posted August 2, 2010 Share Posted August 2, 2010 It would be easier if the resizing method was written in its own function. You basically need to run all of this code a second time on the original image: 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: if (function_exists('imagegif')) { $success = imagegif($thumb, THUMBS_DIR.$name.'_thb.gif'); $thumb_name = $name.'_thb.gif'; } else { $success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 50); $thumb_name = $name.'_thb.jpg'; } break; case 2: $success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 100); $thumb_name = $name.'_thb.jpg'; break; case 3: $success = imagepng($thumb, THUMBS_DIR.$name.'_thb.png'); $thumb_name = $name.'_thb.png'; } Of course, you will want to change the output file to overwrite the original and you will be using different $thumb_wdith and $thumb_heights. The ratio would be the same though Quote Link to comment Share on other sites More sharing options...
RyanMinor Posted August 2, 2010 Author Share Posted August 2, 2010 So, you are sayng to put the "create thumbnail" code into a seperate page and include it. Then use the same code in the create thumbnail to resize the original image? If so, where would I insert the include for the create thumbnail code? I'm going to mess around with it throughout the night. It would be easier if the resizing method was written in its own function. You basically need to run all of this code a second time on the original image: 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: if (function_exists('imagegif')) { $success = imagegif($thumb, THUMBS_DIR.$name.'_thb.gif'); $thumb_name = $name.'_thb.gif'; } else { $success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 50); $thumb_name = $name.'_thb.jpg'; } break; case 2: $success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 100); $thumb_name = $name.'_thb.jpg'; break; case 3: $success = imagepng($thumb, THUMBS_DIR.$name.'_thb.png'); $thumb_name = $name.'_thb.png'; } Of course, you will want to change the output file to overwrite the original and you will be using different $thumb_wdith and $thumb_heights. The ratio would be the same though Quote Link to comment Share on other sites More sharing options...
RyanMinor Posted August 8, 2010 Author Share Posted August 8, 2010 I have played with this script all day and cannot get it to work properly. When I run it, it uploads the original image in its original dimensions (I do not want it to do this) and the newly resized image (from the resizeimage.php include file), but it won't creat the thumbnail or run the insert query. How can I combine these two scripts more efficiently? I'm still a beginner in PHP. Any help is appreciated. Below is my code: addphoto.php (This page receives the galleryid from addgallery.php and allows the user to upload a photo. My goals is to get it to resize the original photo to a new size of max width 400px, create a thumbnail with max width of 135px, and insert both filnames into my database.) <?php if (!isset($_SESSION)) { session_start(); } $MM_authorizedUsers = ""; $MM_donotCheckaccess = "true"; // *** Restrict Access To Page: Grant or deny access to this page function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { // For security, start by assuming the visitor is NOT authorized. $isValid = False; // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. // Therefore, we know that a user is NOT logged in if that Session variable is blank. if (!empty($UserName)) { // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. // Parse the strings into arrays. $arrUsers = Explode(",", $strUsers); $arrGroups = Explode(",", $strGroups); if (in_array($UserName, $arrUsers)) { $isValid = true; } // Or, you may restrict access to only certain users based on their username. if (in_array($UserGroup, $arrGroups)) { $isValid = true; } if (($strUsers == "") && true) { $isValid = true; } } return $isValid; } $MM_restrictGoTo = "index.php"; if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) { $MM_qsChar = "?"; $MM_referrer = $_SERVER['PHP_SELF']; if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&"; if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) $MM_referrer .= "?" . $QUERY_STRING; $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer); header("Location: ". $MM_restrictGoTo); exit; } require_once('../Connections/conndb.php'); if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "uploadphoto")) { if (array_key_exists('upload', $_POST)) { // create an array of permitted MIME types $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png'); // begin by assuming the file is unacceptable $typeOK = false; // check that file is of an permitted MIME type foreach ($permitted as $type) { if ($type == $_FILES['image']['type']) { $typeOK = true; break; } } if ($typeOK) { switch($_FILES['image']['error']) { case 0: // put photo resize script here include ('resizephoto.php'); // the following code is for creating the thumbnail // define constants define('THUMBS_DIR', '../members/photos/thumbs/'); define('MAX_WIDTH', 134); define('MAX_HEIGHT',200); // 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'])); // move the temporary file to the upload folder $moved = move_uploaded_file($original, THUMBS_DIR.$_FILES['image']['name']); if ($moved) { $result = $_FILES['image']['name'].' successfully uploaded; '; $original = THUMBS_DIR.$_FILES['image']['name']; } 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: if (function_exists('imagegif')) { $success = imagegif($thumb, THUMBS_DIR.$name.'_thb.gif'); $thumb_name = $name.'_thb.gif'; } else { $success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 50); $thumb_name = $name.'_thb.jpg'; } break; case 2: $success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 100); $thumb_name = $name.'_thb.jpg'; break; case 3: $success = imagepng($thumb, THUMBS_DIR.$name.'_thb.png'); $thumb_name = $name.'_thb.png'; } if ($success && $psuccess) { $insertSQL = sprintf("INSERT INTO tblmembergalleryphoto (galleryid, photoname, thumbname) VALUES (%s, %s, %s)", GetSQLValueString($_SESSION['galleryid'], "int"), GetSQLValueString($pthumb_name, "text"), GetSQLValueString($thumb_name, "text")); mysql_select_db($database_conndb, $conndb); $Result1 = mysql_query($insertSQL, $conndb) or die(mysql_error()); $result .= "$thumb_name and $pthumb_name created. Add another photo or Click <a href='home.php'>here</a> to go back to the administrator home page."; } else { $result .= 'Problem creating thumbnail'; } // remove the image resources from memory imagedestroy($source); imagedestroy($thumb); } } 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>Photo Upload</title> </head> <body> <div align="center"> <?php // if the form has been submitted, display result if (isset($result)) { echo "<p>$result</p>"; } ?> </div> <div align="center"> <form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="uploadphoto" id="uploadphoto"> <p> <label for="image">Select an Image to Upload to the Gallery:</label> <br /> <input type="file" name="image" id="image" /> </p> <p> <input type="submit" name="upload" id="upload" value="Upload" /> <input type="hidden" name="MM_insert" value="uploadphoto" /> </p> </form> </div> </body> </html> resizephoto.php (This page is what I was trying to get to resize the original image to max width 400px) <?php // define constants define('PHOTO_DIR', '../members/photos/'); define('MAX_PWIDTH', 400); define('MAX_PHEIGHT',750); // process the uploaded image if (is_uploaded_file($_FILES['image']['tmp_name'])) { $poriginal = $_FILES['image']['tmp_name']; // begin by getting the details of the original list($pwidth, $pheight, $ptype) = getimagesize($poriginal); // calculate the scaling ratio if ($pwidth <= MAX_PWIDTH && $pheight <= MAX_PHEIGHT) { $pratio = 1; } elseif ($pwidth > $pheight) { $pratio = MAX_PWIDTH/$pwidth; } else { $pratio = MAX_PHEIGHT/$pheight; } // strip the extension off the image filename $pimagetypes = array('/\.gif$/', '/\.jpg$/', '/\.jpeg$/', '/\.png$/'); $pname = preg_replace($pimagetypes, '', basename($_FILES['image']['name'])); // move the temporary file to the upload folder $pmoved = move_uploaded_file($poriginal, PHOTO_DIR.$_FILES['image']['name']); if ($pmoved) { $presult = $_FILES['image']['name'].' successfully uploaded; '; $poriginal = PHOTO_DIR.$_FILES['image']['name']; } else { $presult = 'Problem uploading photo, '.$_FILES['image']['name'].'; '; } // create an image resource for the original switch($ptype) { case 1: $psource = @ imagecreatefromgif($poriginal); if (!$psource) { $presult = 'Cannot process GIF files. Please use JPEG or PNG.'; } break; case 2: $psource = imagecreatefromjpeg($poriginal); break; case 3: $psource = imagecreatefrompng($poriginal); break; default: $psource = NULL; $presult = 'Cannot identify file type.'; } // make sure the image resource is OK if (!$psource) { $presult = 'Problem copying original'; } else { // calculate the dimensions of the thumbnail $pthumb_width = round($pwidth * $pratio); $pthumb_height = round($pheight * $pratio); // create an image resource for the thumbnail $pthumb = imagecreatetruecolor($pthumb_width, $pthumb_height); // create the resized copy imagecopyresampled($pthumb, $psource, 0, 0, 0, 0, $pthumb_width, $pthumb_height, $pwidth, $pheight); // save the resized copy switch($ptype) { case 1: if (function_exists('imagegif')) { $psuccess = imagegif($pthumb, PHOTO_DIR.$pname.'_org.gif'); $pthumb_name = $pname.'_org.gif'; } else { $psuccess = imagejpeg($pthumb, PHOTO_DIR.$pname.'_org.jpg', 50); $pthumb_name = $pname.'_org.jpg'; } break; case 2: $psuccess = imagejpeg($pthumb, PHOTO_DIR.$pname.'_org.jpg', 100); $pthumb_name = $pname.'_org.jpg'; break; case 3: $psuccess = imagepng($pthumb, PHOTO_DIR.$pname.'_org.png'); $pthumb_name = $pname.'_org.png'; } } } ?> Quote Link to comment Share on other sites More sharing options...
RyanMinor Posted August 8, 2010 Author Share Posted August 8, 2010 Here is what I have come up with in combining these two functions. I have literally spent all day in front of my laptop trying to figure this out. Can anybody help me out here? The errors I get are: Notice: Undefined variable: photo_original in C:\wamp\www\test\addphoto.php on line 187 Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: Filename cannot be empty in C:\wamp\www\test\addphoto.php on line 187 Notice: Undefined variable: thumb_original in C:\wamp\www\test\addphoto.php on line 188 Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: Filename cannot be empty in C:\wamp\www\test\addphoto.php on line 188 Problem copying original photo. Here is the code: <?php if (!isset($_SESSION)) { session_start(); } $MM_authorizedUsers = ""; $MM_donotCheckaccess = "true"; // *** Restrict Access To Page: Grant or deny access to this page function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { // For security, start by assuming the visitor is NOT authorized. $isValid = False; // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. // Therefore, we know that a user is NOT logged in if that Session variable is blank. if (!empty($UserName)) { // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. // Parse the strings into arrays. $arrUsers = Explode(",", $strUsers); $arrGroups = Explode(",", $strGroups); if (in_array($UserName, $arrUsers)) { $isValid = true; } // Or, you may restrict access to only certain users based on their username. if (in_array($UserGroup, $arrGroups)) { $isValid = true; } if (($strUsers == "") && true) { $isValid = true; } } return $isValid; } $MM_restrictGoTo = "index.php"; if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) { $MM_qsChar = "?"; $MM_referrer = $_SERVER['PHP_SELF']; if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&"; if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) $MM_referrer .= "?" . $QUERY_STRING; $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer); header("Location: ". $MM_restrictGoTo); exit; } require_once('Connections/DBConnect.php'); if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "uploadphoto")) { if (array_key_exists('upload', $_POST)) { // create an array of permitted MIME types $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png'); // begin by assuming the file is unacceptable $typeOK = false; // check that file is of an permitted MIME type foreach ($permitted as $type) { if ($type == $_FILES['image']['type']) { $typeOK = true; break; } } if ($typeOK) { switch($_FILES['image']['error']) { case 0: // define constants define('PHOTO_DIR', 'C:/wamp/www/test/photos/'); define('MAX_PHOTO_WIDTH', 400); define('MAX_PHOTO_HEIGHT', 750); define('THUMB_DIR', 'C:/wamp/www/test/photos/thumbs/'); define('MAX_THUMB_WIDTH', 135); define('MAX_THUMB_HEIGHT',200); // 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 photo list($width, $height, $type) = getimagesize($original); // calculate the scaling ratio for the new large photo if ($width <= MAX_PHOTO_WIDTH && $height <= MAX_PHOTO_HEIGHT) { $photo_ratio = 1; } elseif ($width > $height) { $photo_ratio = MAX_PHOTO_WIDTH/$width; } else { $photo_ratio = MAX_PHOTO_HEIGHT/$height; } // calculate the scaling ratio for the thumbnail if ($width <= MAX_THUMB_WIDTH && $height <= MAX_THUMB_HEIGHT) { $thumb_ratio = 1; } elseif ($width > $height) { $thumb_ratio = MAX_THUMB_WIDTH/$width; } else { $thumb_ratio = MAX_THUMB_HEIGHT/$height; } // strip the extension off the image filename $imagetypes = array('/\.gif$/', '/\.jpg$/', '/\.jpeg$/', '/\.png$/'); $name = preg_replace($imagetypes, '', basename($_FILES['image']['name'])); // move the temporary file to the upload folder $photo_moved = move_uploaded_file($original, PHOTO_DIR.$_FILES['image']['name']); $thumb_moved = move_uploaded_file($original, THUMB_DIR.$_FILES['image']['name']); if ($photo_moved && $thumb_moved) { $result = $_FILES['image']['name'].' successfully uploaded; '; $photo_original = PHOTO_DIR.$_FILES['image']['name']; $thumb_original = THUMB_DIR.$_FILES['image']['name']; } else { $result = 'Problem uploading '.$_FILES['image']['name'].'; '; } // create an image resource for the original switch($type) { case 1: $photo_source = @ imagecreatefromgif($photo_original); $thumb_source = @ imagecreatefromgif(thumb_original); if ((!$photo_source) || (!$thumb_source)) { $result = 'Cannot process GIF files. Please use JPEG or PNG.'; } break; case 2: $photo_source = imagecreatefromjpeg($photo_original); $thumb_source = imagecreatefromjpeg($thumb_original); break; case 3: $photo_source = imagecreatefrompng($photo_original); $thumb_source = imagecreatefrompng($thumb_original); break; default: $photo_source && $thumb_source = NULL; $result = 'Cannot identify file type.'; } // make sure the image resource is OK if ((!$photo_source) || (!$thumb_source)) { $result = 'Problem copying original photo.'; } else { // calculate the dimensions of the new large photo $photo_width = round($width * $ratio); $photo_height = round($height * $ratio); // calculate the dimensions of the thumbnail $thumb_width = round($width * $ratio); $thumb_height = round($height * $ratio); // create an image resource for the new large photo $photo = imagecreatetruecolor($photo_width, $photo_height); // create an image resource for the thumbnail $thumb = imagecreatetruecolor($thumb_width, $thumb_height); //create the resized new large photo imagecopyresampled($photo, $source, 0, 0, 0, 0, $photo_width, $photo_height, $width, $height); // create the resized thumbnail imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height); // save the resized copy switch($type) { case 1: if (function_exists('imagegif')) { $photo_success = imagegif($photo, PHOTO_DIR.$name.'.gif'); $thumb_success = imagegif($thumb, THUMB_DIR.$name.'_thumb.gif'); $photo_name = $name.'.gif'; $thumb_name = $name.'_thumb.gif'; } else { $photo_success = imagejpeg($photo, PHOTO_DIR.$name.'.jpg', 50); $thumb_success = imagejpeg($thumb, THUMB_DIR.$name.'_thumb.gif', 50); $photo_name = $name.'.jpg'; $thumb_name = $name.'_thb.jpg'; } break; case 2: $photo_success = imagejpeg($photo, PHOTO_DIR.$name.'.jpg', 100); $thumb_success = imagejpeg($thumb, THUMB_DIR.$name.'_thumb.jpg', 100); $photo_name = $name.'.jpg'; $thumb_name = $name.'_thb.jpg'; break; case 3: $photo_success = imagepng($photo, PHOTO_DIR.$name.'.png'); $thumb_success = imagepng($thumb, THUMB_DIR.$name.'_thumb.png'); $photo_name = $name.'.png'; $thumb_name = $name.'_thumb.png'; } if ($photo_success && $thumb_success) { $insertSQL = sprintf("INSERT INTO tblmembergalleryphoto (galleryid, photoname, thumbname) VALUES (%s, %s, %s)", GetSQLValueString($_SESSION['galleryid'], "int"), GetSQLValueString($photo_name, "text"), GetSQLValueString($thumb_name, "text")); mysql_select_db($database_conndb, $conndb); $Result1 = mysql_query($insertSQL, $conndb) or die(mysql_error()); $result .= "$photo_name and $thumb_name created and uploaded successfully. Add another photo or Click <a href='home.php'>here</a> to go back to the administrator home page."; } else { $result .= 'Problem creating thumbnail'; } // remove the image resources from memory imagedestroy($photo_source); imagedestroy($thumb_source); imagedestroy($photo); imagedestroy($thumb); } } 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. 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>Photo Upload</title> </head> <body> <div align="center"> <?php // if the form has been submitted, display result if (isset($result)) { echo "<p>$result</p>"; } ?> </div> <div align="center"> <form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="uploadphoto" id="uploadphoto"> <p> <label for="image">Select an Image to Upload to the Gallery:</label> <br /> <input type="file" name="image" id="image" /> </p> <p> <input type="submit" name="upload" id="upload" value="Upload" /> <input type="hidden" name="MM_insert" value="uploadphoto" /> </p> </form> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
RyanMinor Posted August 8, 2010 Author Share Posted August 8, 2010 OK I have been debugging this thing all day and here is what I have found: 1) The $thumb_moved variable is empty. I ran a var_dump on both variables and got back true for $photo_moved and false for $thumb_moved. Now does anybody have any ideas? 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.