Help with re-sizing image on upload - $$$

Discussion in 'PHP' started by Jonhc, Sep 14, 2008.

  1. #1
    Ok so the CMS I use was coded by a friend of mine, but haven't spoken in years and lost contact. What I want to do is re-size the pictures uploaded to a certain size. I'm not a hugely experienced PHP coder so need someone to help, or do it for me I'm willing to pay, but not a huge ammount.

    The PHP is below, basically I need any file uploaded to be re-sized to 629px x 481px any help is very much appreciated.

    <?
    session_start('login'); // Resume Session.
    include('../config.php');
    
    if(session_is_registered(valid_user)) {
    	echo "<!-- Logged In --> \n";
    } else {
    	// Include Login Page
    	include('../index.php');
    	exit;
    }
    
    ?>
    <?php
    
    include('../config.php');
    include('../../functions.php');
    
    $pagenum = $_GET["id"];
    
    if($pagenum == "") {
    	$pagenum = $_POST['id'];
    }
    
    if ($_GET['method'] == "delete") {
    
    		$photo = $_GET['photo'];
    
    		$link = mysql_connect($datab, $user, $pass) or die("Could not connect: " . mysql_error());
    		mysql_select_db($data_base) or die("Could not select database");
    		$query = "SELECT * FROM gallery WHERE id=\"$photo\"";
    		$result = mysql_query($query) or die("Query failed : " . mysql_error());
    		while ($row = mysql_fetch_assoc($result)) {
    
    
    
    		if (file_exists("../../images/gallery/".$row['file'])) {
    					// Delete Image
    					unlink("../../images/gallery/".$row['file']);
    	  			} 
    	
    		}
    
    
    		$link = mysql_connect($datab, $user, $pass) or die("Could not connect: " . mysql_error());
    		mysql_select_db($data_base) or die("Could not select database");
    		$query = "DELETE FROM `gallery` WHERE `id` = $photo LIMIT 1";
    		$result = mysql_query($query) or die("Query failed : " . mysql_error());
    
    }
    
    
    if ($_POST['addgallery'] == "true") {
    
    		$link = mysql_connect($datab, $user, $pass) or die("Could not connect: " . mysql_error());
    		mysql_select_db($data_base) or die("Could not select database");
    		$query = "SELECT max(id) as nextid FROM gallery";
    		$result = mysql_query($query) or die("Query failed : " . mysql_error());
    		while ($row = mysql_fetch_assoc($result)) {
    			
    			// $imagename2 = $row['nextid'] + 1;
    		//	$imagename = $imagename2.".jpg";
    	
    		}
    
    
    	
    		
    
    		$insertcaption = $_POST['caption'];
    		$insertpage = $_POST['id'];
    		$insertdescription = $_POST['description'];
    
    
    		$link = mysql_connect($datab, $user, $pass) or die("Could not connect: " . mysql_error());
    		mysql_select_db($data_base) or die("Could not select database");
    		$query = "INSERT INTO  `gallery` (  `id` ,  `page` ,  `caption` ,  `file`,  `description`) VALUES ('',  '$insertpage',  '$insertcaption',  '$imagename', '$insertdescription')";
    		mysql_query($query) or die("Query failed : " . mysql_error());
    		
    		$insertedinto = mysql_insert_id();	
    		$imagename = mysql_insert_id().".jpg";
    		
    		$thumbnail = '../../images/gallery/' . mysql_insert_id()."_thumb.jpg";
    		$normalimage = '../../images/gallery/' . mysql_insert_id()."_normal.jpg";		
    		
    		
    		$link = mysql_connect($datab, $user, $pass) or die("Could not connect: " . mysql_error());
    		mysql_select_db($data_base) or die("Could not select database");
    		$query = "UPDATE `gallery` SET file='$imagename' WHERE id='$insertedinto'";
    		mysql_query($query) or die("Query failed : " . mysql_error());
    		
    			# Uploading of File
    
    			$uploaddir = $_SERVER['DOCUMENT_ROOT'].'/images/gallery';
    			$uploadfile = $uploaddir."/".$imagename; //. basename($_FILES['userfile']['name']);
    
    			// echo $uploadfile;
    			// 
    			// 			exit;
    
    
    			if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    
    				// echo "File is valid, and was successfully uploaded.\n";
    
    				list($width, $height, $type, $attr) = getimagesize($uploadfile);
    				// echo "Supplied Image Size";
    				// echo "Width: " . $width . "px Height: " . $height	. "px <br />";
    
    					chmod($uploadfile, 0777);
    
    					// Create thumbnail image
    					# Generate a Thumbnail from the large image
    
    					// Resize image
    					$percent = 0.5;
    					// Content type
    					// Resize image
    					list($width, $height) = getimagesize($uploadfile);
    					$new_width = 70;
    					$new_height = 50;
    
    					// Resample the image
    					$image_p = imagecreatetruecolor($new_width, $new_height);
    					$image = imagecreatefromjpeg($uploadfile);
    					imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    
    					// Output the actual image
    					imagejpeg($image_p, $thumbnail, 80);
    
    					###############################################################
    
    
    					$percent = 0.5;
    					// Content type
    					// Resize image
    					list($width, $height) = getimagesize($uploadfile);
    					$new_width = 350;
    					$new_height = 248;
    
    // MAKE AN IMAGE INTO THE RIGHT SIZE FOR PAGE DISPLAY
    function create_right_size_image($image, $width=720) {
     
    	$source = imagecreatefromjpeg("$image");
     
    	$imageX = imagesx($source);
    	$imageY = imagesy($source);
     
    	if ($imageX <= $width) {
    return FALSE;
    }
    	if ($imageX > $width) {
    		$tnailX = $width;
    		$tnailY = (int) (($tnailX * $imageY) / $imageX );
    	} else {
    		$tnailX	= $imageX;
    		$tnailY = (int) (($tnailX * $imageY) / $imageX );
    	}
     
    	$target = imagecreatetruecolor($tnailX, $tnailY);
    //	$target = imagecreate($tnailX, $tnailY);
    	imagecopyresampled ($target, $source, 0, 0, 0, 0, $tnailX, $tnailY, $imageX, $imageY);
    //	imagecopyresized   ($target, $source, 0, 0, 0, 0, $tnailX, $tnailY, $imageX, $imageY);
     
    return $target ;
    }
    
    					// Output the actual image
    					imagejpeg($image_p, $normalimage, 80);
    
    				} else {
    					
    					echo '<div style="border: 3px solid red; margin: 0 auto; width: 700px; text-align: center; color: red; font-family: verdana; background-color: #ffd1d6;">';
    					echo "<h4>Wrong Image Size - image must be 629px x 481px</h4>";
    					echo "Your image was " .  $width . "px x " . $height . "px <br /><br />";
    					echo "</div>";
    					
    					$link = mysql_connect($datab, $user, $pass) or die("Could not connect: " . mysql_error());
    					mysql_select_db($data_base) or die("Could not select database");
    					$query = "DELETE FROM `gallery` WHERE `id` = $insertedinto LIMIT 1";
    					$result = mysql_query($query) or die("Query failed : " . mysql_error());
    					
    					if (file_exists($uploadfile)) {
    								// Delete Image
    								unlink($uploadfile);
    				  	} 
    					
    					
    					
    					}
    					
    					
    				} else {
    			   		echo "Possible file upload attack!\n";
    				}
    		
    		
    		
    
    		
    
    }
    
    
    if ($_POST['updateentry'] == "true") {
    
    	$newcaption = $_POST['caption'];
    	$toupdate = $_POST['phototoupdate'];
    	$newdescription = $_POST['description'];
    //	echo $newcaption;
    //	echo $toupdate;
    	
    	$link = mysql_connect($datab, $user, $pass) or die("Could not connect: " . mysql_error());
    	mysql_select_db($data_base) or die("Could not select database");
    	$query = "UPDATE gallery SET caption=\"".$newcaption."\",description=\"".$newdescription."\" WHERE id=\"".$toupdate."\"";
    	$result = mysql_query($query) or die("Query failed : " . mysql_error());
    	
    	
    }
    
    
    if ($_POST['updatezoom'] == "true") {
    
    	$zoom = $_POST['zoomfeature'];
    
    	$link = mysql_connect($datab, $user, $pass) or die("Could not connect: " . mysql_error());
    	mysql_select_db($data_base) or die("Could not select database");
    	$query = "UPDATE prefs SET value=\"".$zoom."\" WHERE id='zoom_feature'";
    	$result = mysql_query($query) or die("Query failed : " . mysql_error());
    	
    	
    }
    
    
    if ($_POST['updateextended'] == "true") {
    
    	$extended = $_POST['extendedfeature'];
    
    	$link = mysql_connect($datab, $user, $pass) or die("Could not connect: " . mysql_error());
    	mysql_select_db($data_base) or die("Could not select database");
    	$query = "UPDATE prefs SET value=\"".$extended."\" WHERE id='show_gallery_description'";
    	$result = mysql_query($query) or die("Query failed : " . mysql_error());
    	
    	
    }
    
    
    
    
    	
    
    
    $link = mysql_connect($datab, $user, $pass) or die("Could not connect: " . mysql_error());
    mysql_select_db($data_base) or die("Could not select database");
    $query = "SELECT * FROM pages WHERE id=\"$pagenum\"";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    $num_rows = mysql_num_rows($result);
    while ($row = mysql_fetch_assoc($result)) {
    	$content = $row["textcontent"];
    	$p_title = $row["pagetitle"];
    	$b_title = $row["menutext"];
    	$image_1 = $row["image1"];
    	$image1alt = $row["images1alt"];
    	$image_2 = $row["image2"];
    	$image2alt = $row["image2alt"];
    	$pagetype = $row["pagetype"];
    }
    	
    
    ?>
    
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Discovery Design Content Management System - Version 2.0</title>
    <script src="../../scripts/master.js" language="javascript" type="text/javascript"></script>
    <link href="../admin.css" type="text/css" media="all" rel="stylesheet" />
    
    <script language="javascript" type="text/javascript" src="../../jscripts/tiny_mce/tiny_mce.js"></script>
    <script language="javascript" type="text/javascript">
    tinyMCE.init({
    	mode : "textareas",
    	relative_urls : false,
    	theme : "simple"
    });
    
    function toggleEditor(id) {
    	var elm = document.getElementById(id);
    
    	if (tinyMCE.getInstanceById(id) == null)
    		tinyMCE.execCommand('mceAddControl', false, id);
    	else
    		tinyMCE.execCommand('mceRemoveControl', false, id);
    }
    
    </script>
    
    </head>
    
    
    <body>
    	  <table width="710" align="center">
    			<tr bgcolor="#13355E">
    				<td colspan="2" style="padding: 10px; font-size: 14px; color: white;">
    					Updating Page: <b><?php echo $b_title; ?> (This is a Photo Gallery Page)</b>
    				</td>
    			</tr>
    </table>	
    	    		
       
    	<table width="710" align="center" style="background:#F2F2F2; ">
    	<?php
    
    						$link = mysql_connect($datab, $user, $pass) or die("Could not connect: " . mysql_error());
    						mysql_select_db($data_base) or die("Could not select database");
    						$query = "SELECT * FROM gallery WHERE page=\"$pagenum\" order by id";
    						$result = mysql_query($query) or die("Query failed : " . mysql_error());
    						$num_links = mysql_num_rows($result);
    
    	?>
    
    		<tr>
    			<td valign="top" bgcolor="#585858" style="color: white; font-weight: bold; font-size: 13px; padding: 5px;">Current Images (<?php echo $num_links; ?>)</td>
    		</tr>
    
    		<tr>
    			<td>
    
    				<form action="InlineGallery.php" method="post">
    				<table width="100%" cellpadding="5" cellspacing="1" bgcolor="#F5F5F5">
    					<tr>
    						<td colspan="3" bgcolor="#0869AF" style="color: white; font-weight: bold; font-size: 13px;">
    						
    							Add a New Gallery Image
    							
    							<?php
    
    
    									$link2 = mysql_connect($datab, $user, $pass) or die("Could not connect: " . mysql_error());
    									mysql_select_db($data_base) or die("Could not select database");
    									$query2 = "SELECT id,value FROM prefs where id='zoom_feature'";
    									$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
    									while ($row2 = mysql_fetch_assoc($result2)) {
    
    										$zoom = $row2['value'];
    									
    
    								  }
    
    
    										$link2 = mysql_connect($datab, $user, $pass) or die("Could not connect: " . mysql_error());
    										mysql_select_db($data_base) or die("Could not select database");
    										$query2 = "SELECT id,value FROM prefs where id='show_gallery_description'";
    										$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
    										while ($row2 = mysql_fetch_assoc($result2)) {
    
    											$extended = $row2['value'];
    
    
    									  }
    								
    
    							?>
    							<hr />Include Zoom Feature: 
    							<input type="hidden" name="id" value="<?php echo $pagenum; ?>" />
    							<input type="hidden" name="updatezoom" value="true" />
    							<select name="zoomfeature">
    								<option value="yes" <?php if ($zoom == "yes") { echo "selected"; } ?>>Yes</option>
    								<option value="no" <?php if ($zoom == "no") { echo "selected"; } ?>>No</option>
    							</select>
    							
    							<input type="submit" value="Update" />
    							</form>
    							<form action="InlineGallery.php" method="post">
    							<hr />Display Extended Description?: 
    							<input type="hidden" name="id" value="<?php echo $pagenum; ?>" />
    							<input type="hidden" name="updateextended" value="true" />
    							<select name="extendedfeature">
    								<option value="yes" <?php if ($extended == "yes") { echo "selected"; } ?>>Yes</option>
    								<option value="no" <?php if ($extended == "no") { echo "selected"; } ?>>No</option>
    							</select>
    							
    							<input type="submit" value="Update" />
    							</form>
    							
    							
    						</td>
    					</tr>
    					<form action="InlineGallery.php" method="post" enctype="multipart/form-data" />
    					<tr bgcolor="#17539D">
    						<td style="color: #FFFFFF;" width="10"><b>Photo</b></td>
    						<td colspan="2" style="color: #FFFFFF;"><b>Caption / Description</b></td>
    					</tr>
    					<input type="hidden" name="id" value="<?php echo $pagenum; ?>" />
    					<input type="hidden" name="addgallery" value="true" />
    					<tr>
    						<td valign="top">
    							<input type="file" name="userfile" />
    							<input type="hidden" name="MAX_FILE_SIZE" value="30000000" />
    						</td>
    						<td>
    							<b>Caption:</b><br />
    							<textarea  name="caption" style="width: 100%; font-family: verdana; height: 20px;" /></textarea>
    							<b>Description:</b><br />
    							<textarea  name="description" style="width: 100%; font-family: verdana; height: 130px;" /></textarea>
    							</td>
    						<td align="center" width="50" valign="bottom"><input type="submit" value="Upload New Gallery Entry" /></td>
    						
    					</tr>
    					
    				</table>
    				</form>
    			</td>
    		</tr>
    		
    		<?php
    
    			if ($_GET['method'] == "edit") {
    				
    				$phototoupdate = $_GET['photo'];
    				
    				
    				$link = mysql_connect($datab, $user, $pass) or die("Could not connect: " . mysql_error());
    				mysql_select_db($data_base) or die("Could not select database");
    				$query = "SELECT caption,description FROM gallery WHERE id=".$phototoupdate."";
    				$result = mysql_query($query) or die("Query failed : " . mysql_error());
    				while ($row = mysql_fetch_assoc($result)) {
    
    				$caption = $row['caption'];
    				$description = $row['description'];
    
    				}	
    			
    			echo	"<tr>";
    			echo		"<td>";
    			echo		'<form action="InlineGallery.php" method="post" />';
    			echo			'<table width="100%" cellpadding="5" cellspacing="1" bgcolor="#F5F5F5">';
    			echo				'<tr>';
    			echo					'<td colspan="3" bgcolor="red" style="color: white; font-weight: bold; font-size: 13px;">Edit Gallery Entry</td>';
    			echo				'</tr>';
    
    			echo				'<tr bgcolor="#E0E0E0">';
    			
    			echo					'<td style="color: #585858;" width="100"><b>Photo</b></td>';
    			echo					'<td style="color: #585858;"><b>Caption</b></td>';
    			echo					'<td>&nbsp;</td>';
    			echo				'</tr>';
    			echo				'<input type="hidden" name="id" value="'. $pagenum .'" />';
    			echo				'<input type="hidden" name="phototoupdate" value="'. $_GET['photo'] .'" />';
    			echo				'<input type="hidden" name="updateentry" value="true" />';
    			echo				'<tr>';
    			echo					'<td>';
    			echo 					"<img src=\"../../images/gallery/".$phototoupdate.".jpg\" width=\"175\" height=\"124\" style=\"border: 1px #000 solid;\" />";
    			echo					'</td><td>';
    			echo					'<b>Caption:</b><br />';
    			echo 					'<textarea  name="caption" style="width: 100%; font-family: verdana; height: 20px;" />'.$caption.'</textarea>';
    			echo 					'<b>Description:</b><br />';
    			echo 					'<textarea  name="description" style="width: 100%; font-family: verdana; height: 130px;" />'.$description.'</textarea></td>';
    			echo					'<td align="center" width="100"><input type="submit" value="Update Caption" /></td>';
    
    			echo				'</tr></table></form></td></tr>';
    
    				
    				
    			}
    			
    			
    
    		?>
    		
    		<tr>
    						<td colspan="4">
    						<table width="100%" cellpadding="5" cellspacing="1">
    						<tr bgcolor="#17539d">
    						<td style="color: #FFFFFF;"><b>ID</b></td>
    						<td style="color: #FFFFFF;"><b>Image</b></td>
    						<td style="color: #FFFFFF;"><b>Caption</b></td>
    						<td style="color: #FFFFFF;" align="center"><b>Edit</b></td>
    						<td style="color: #FFFFFF;" align="center"><b>Delete</b></td>
    						</tr>	
    
    						<?php
    						while ($row = mysql_fetch_assoc($result)) {
    							echo "<tr>";
    							echo "<td style=\"border-right: 1px #D4D4D4 solid; border-bottom: 1px #D4D4D4 solid;\" width=\"15\" align=\"center\">".$row['id']."</td>";
    							echo "<td style=\"border-right: 1px #D4D4D4 solid; border-bottom: 1px #D4D4D4 solid;\" width=\"150\" align=\"center\">";
    
    							if (file_exists("../../images/gallery/".$row['file'])) {
       							echo "<img src=\"showimage.php?thumb=".$row['file']."\" style=\"border: 1px #000 solid;\" /><br />";
       							echo $row['file'];
    	   					} else {
       							echo "<b style=\"color: red;\">The file $filename does not exist<br /></b>Has it been uploaded correctly?<br />";
    	   					}
    							
    							echo "</td>";
    							echo "<td style=\"border-right: 1px #D4D4D4 solid; border-bottom: 1px #D4D4D4 solid;\" valign=\"top\">";
    							echo "<b>Caption:</b> <br />".$row['caption'];
    							echo "<br /><br />";
    							echo "<b>Description</b>: <br />".$row['description'];
    							echo "</td>";
    							echo "<td align=\"center\" style=\"background-color: #0BA80B; color: white;\" width=\"12%\">";
    							echo "<b><a href=\"InlineGallery.php?method=edit&id=".$pagenum."&photo=".$row['id']."\" style=\"color: white;\">Edit</a>";
    							echo "</td>";
    							echo "<td align=\"center\" style=\"background-color: #E85454; color: white;\" width=\"12%\">";
    							echo "<b><a href=\"InlineGallery.php?method=delete&id=".$pagenum."&photo=".$row['id']."\" style=\"color: white;\">Delete</a></b>";
    							echo "</td>";
    						}
    
    					
    					?>
    						</table>
    						<br />
    						</td>
    </tr>
    
    		
    	</table>
    
    </body>
    </html>
    PHP:
     
    Jonhc, Sep 14, 2008 IP
  2. nabz245

    nabz245 Well-Known Member

    Messages:
    677
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    110
    #2
    nabz245, Sep 14, 2008 IP
  3. Jonhc

    Jonhc Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks dude but to be honest, I wouldn't know where to start, could you do this?
     
    Jonhc, Sep 14, 2008 IP