PHP MySQL image upload/resize

Okay. I have followed several tutorials now on this topic and it is not clicking as to how I would apply these tutorial scripts to my code. I have 2 separate issues to address but cannot seem to figure out either so any help or advice would be greatly appreciated. I am basically wanting a user to be able to upload images and have it resize them. I need to give them the option of uploading up to six images, with one image being 500 X 250 px and the rest being up to 500 X 500 px.

This is my current (one image) upload script:
[php]<?php
include ‘core/init.php’;
protect_page();
header(“Location: wod_admin.php”);

function GetFileName($f)
{
/*

  • this function will get the name of a file
  • for Instance if the file is image14.jpg
  • only image14 will be returned.

BY wilson382
*/
$h=substr($f,strrpos($f,"/"),strrpos($f,"."));
$FileName= substr($h,1,strrpos($h,".")-1);

return $FileName;
}

if(isset($_POST[‘submit’]))
{
if(is_uploaded_file($_FILES[‘myfile’][‘tmp_name’]))
{
$name = $_FILES[‘myfile’][‘name’];
$tmp_name = $_FILES[‘myfile’][‘tmp_name’];
$location = “images/wod/$name”;
move_uploaded_file($tmp_name,$location);
}

$_month = mysql_real_escape_string($_POST['month']);
$_day = mysql_real_escape_string($_POST['day']);
$_year = mysql_real_escape_string($_POST['year']);
$_name = mysql_real_escape_string($_POST['name']);
$_w1 = mysql_real_escape_string($_POST['w1']);
$_w2 = mysql_real_escape_string($_POST['w2']);
$_w3 = mysql_real_escape_string($_POST['w3']);
$_w4 = mysql_real_escape_string($_POST['w4']);
$_w5 = mysql_real_escape_string($_POST['w5']);
$_w6 = mysql_real_escape_string($_POST['w6']);
$_comment = mysql_real_escape_string($_POST['comment']);

// select all rows that are not the top 4

$sql = mysql_query(“SELECT * FROM wod WHERE id NOT IN (SELECT * FROM (SELECT id FROM wod ORDER BY id DESC LIMIT 4) AS ids_to_keep)”);
while($row = mysql_fetch_assoc($sql)) {
$del = mysql_query(sprintf(“DELETE FROM wod WHERE id = %d”, (int) $row[‘id’])); // delete current row
unlink($row[‘imagelocation’]); // remove image from current row
}

$sql="INSERT INTO `wod` SET `month`='$_month', 
					        `day`='$_day', 
					        `year`='$_year', 
					        `name`='$_name', 
					        `w1`='$_w1',
					        `w2`='$_w2',
					        `w3`='$_w3',
					        `w4`='$_w4',
					        `w5`='$_w5',
					        `w6`='$_w6',
					        `comment`='$_comment',
							`imagelocation`='$location'";
    
if (!mysql_query($sql))
{
	die('Error: ' . mysql_error());
}     
mysql_close($con);

}

?>[/php]

ad this is the tutorial script:
[php]<?php

// File Variables
$name=		$_FILES['image']['name'];
$temp=		$_FILES['image']['tmp_name'];
$type=		$_FILES['image']['type'];
$size=		$_FILES['image']['size'];
$size2= 	getimagesize( $temp );
$width=		$size2[0];
$height=	$size2[1];
$upload=	md5( rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) );

// Restrictions for uploading
$maxwidth=		4000;
$maxheight=		4000;
$allowed=		array( 'image/jpeg', 'image/png', 'image/gif' );

// Recognizing the extension
switch( $type ){
	
	// Image/Jpeg
	case 'image/jpeg':
		$ext= '.jpg';
	break;
	
	// Image/png
	case 'image/png':
		$ext= '.png';
	break;
	
	// Image/gif
	case 'image/gif':
		$ext= '.gif';
	break;
	
}

// Upload Variables
$path= 			'uploads/' . $upload . $ext;
$thumb_path= 	'uploads/thumbs/thumb_' . $upload . $ext;

// Check for the Image post.
if( $_POST ){

	// Got into the POST check.

	if( $_FILES ){
		
		// Checking if the extension is allowed.
		if( in_array( $type, $allowed ) ){
		
			// Checking if the resolution is FULLHD or under this resolution.
			if( $width <= $maxwidth && $height <= $maxheight ){
			
				// Checking if the image is 5MB or less.
				if( $size <= 5242880 ){
				
					// Check the shape of the image.
					if( $width == $height ){ $shape=1; }
					if( $width < $height ){ $shape=2; }
					if( $width > $height ){ $shape=3; }
					
					// Ajusting the resize script on shape.
					switch( $shape ){
						
						// Code to resize a square image.
						case 1:
						
							$newwidth=		100;
							$newheight=		100;
						
						break;
						
						// Code to resize a tall image.
						case 2:
							
							$newwidth=		100;
							$ratio=			$newwidth / $width;
							$newheight=		round( $height * $ratio );
							
						break;
						
						// Code to resize a wide image.
						case 3:
						
							$newheight=		100;
							$ratio=			$newheight / $height;
							$newwidth=		round( $width * $ratio );
						
						break;
						
					}
					
					// Resizing according to extension.
					switch( $type ){
	
						// Image/Jpeg
						case 'image/jpeg':
							
							$img=		imagecreatefromjpeg( $temp );
							$thumb=		imagecreatetruecolor( $newwidth, $newheight );
										imagecopyresized( $thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height );
										imagejpeg( $thumb, $thumb_path );
										
						break;
						
						// Image/png
						case 'image/png':
							
							$img=		imagecreatefrompng( $temp );
							$thumb=		imagecreatetruecolor( $newwidth, $newheight );
										imagecopyresized( $thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height );
										imagepng( $thumb, $thumb_path );
							
						break;
						
						// Image/gif
						case 'image/gif':
							
							$img=		imagecreatefromgif( $temp );
							$thumb=		imagecreatetruecolor( $newwidth, $newheight );
										imagecopyresized( $thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height );
										imagegif( $thumb, $thumb_path );
							
						break;
						
					}
					
					// Move the original file aswell.
					move_uploaded_file( $temp, $path );
					
					// Putting out the data.
					header('Location: index.php');
					
				}else{ die( "Your picture did not pass the size restrictions filter." ); }
			
			}else{ die( "Your picture did not pass the resolution restrictions filter." ); }
		
		}else{ die( "Your picture did not pass the extension restrictions filter." ); }
		
	}

}

?>[/php]

I am failing to see how to apply the tutorial script to my current script. The current script takes the html upload form (named ‘myfile’) and sends its file folder path to a row named ‘imagelocation’ in a database.

You have everything you need in that tutorial. Just try to read through it and see which parts you need to apply to your script.

These are doing the actual resizing, everything before that is just settings and validation

[php]
$img= imagecreatefromjpeg( $temp );
$thumb= imagecreatetruecolor( $newwidth, $newheight );
imagecopyresized( $thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height );
imagejpeg( $thumb, $thumb_path );
[/php]

Okay, this is what ive got so far, sir. (But now it will not even upload a file to the image folder or to the database)

[php]<?php
include ‘core/init.php’;
protect_page();
header(“Location: wod_admin.php”);
?>

<?php // File Variables $name= $_FILES['image']['name']; $temp= $_FILES['image']['tmp_name']; $type= $_FILES['image']['type']; $size= $_FILES['image']['size']; $size2= getimagesize( $temp ); $width= $size2[0]; $height= $size2[1]; $upload= md5( rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) ); // Restrictions for uploading $maxwidth= 4000; $maxheight= 4000; $allowed= array( 'image/jpeg', 'image/png', 'image/gif' ); // Recognizing the extension switch( $type ){ // Image/Jpeg case 'image/jpeg': $ext= '.jpg'; break; // Image/png case 'image/png': $ext= '.png'; break; // Image/gif case 'image/gif': $ext= '.gif'; break; } // Upload Variables images/wod/$name $path= 'images/wod/' . $upload . $ext; $thumb_path= 'images/wod/thumb_' . $upload . $ext; // Check for the Image post. if( $_POST ){ // Got into the POST check. if( $_FILES ){ // Checking if the extension is allowed. if( in_array( $type, $allowed ) ){ // Checking if the resolution is FULLHD or under this resolution. if( $width <= $maxwidth && $height <= $maxheight ){ // Checking if the image is 5MB or less. if( $size <= 5242880 ){ // Check the shape of the image. if( $width == $height ){ $shape=1; } if( $width < $height ){ $shape=2; } if( $width > $height ){ $shape=3; } // Ajusting the resize script on shape. switch( $shape ){ // Code to resize a square image. case 1: $newwidth= 100; $newheight= 100; break; // Code to resize a tall image. case 2: $newwidth= 100; $ratio= $newwidth / $width; $newheight= round( $height * $ratio ); break; // Code to resize a wide image. case 3: $newheight= 100; $ratio= $newheight / $height; $newwidth= round( $width * $ratio ); break; } // Resizing according to extension. switch( $type ){ // Image/Jpeg case 'image/jpeg': $img= imagecreatefromjpeg( $temp ); $thumb= imagecreatetruecolor( $newwidth, $newheight ); imagecopyresized( $thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height ); imagejpeg( $thumb, $thumb_path ); break; // Image/png case 'image/png': $img= imagecreatefrompng( $temp ); $thumb= imagecreatetruecolor( $newwidth, $newheight ); imagecopyresized( $thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height ); imagepng( $thumb, $thumb_path ); break; // Image/gif case 'image/gif': $img= imagecreatefromgif( $temp ); $thumb= imagecreatetruecolor( $newwidth, $newheight ); imagecopyresized( $thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height ); imagegif( $thumb, $thumb_path ); break; } // Move the original file aswell. move_uploaded_file( $temp, $path ); // Putting out the data. header('Location: index.php'); }else{ die( "Your picture did not pass the size restrictions filter." ); } }else{ die( "Your picture did not pass the resolution restrictions filter." ); } }else{ die( "Your picture did not pass the extension restrictions filter." ); } } } function GetFileName($f) { /* * this function will get the name of a file * for Instance if the file is image14.jpg * only image14 will be returned. BY wilson382 */ $h=substr($f,strrpos($f,"/"),strrpos($f,".")); $FileName= substr($h,1,strrpos($h,".")-1); return $FileName; } if(isset($_POST['submit'])) { if(is_uploaded_file($_FILES['p']['tmp_name'])) { $name = $_FILES['p']['name']; $tmp_name = $_FILES['p']['tmp_name']; $location = "images/wod/$upload"; move_uploaded_file($temp, $path); } $_month = mysql_real_escape_string($_POST['month']); $_day = mysql_real_escape_string($_POST['day']); $_year = mysql_real_escape_string($_POST['year']); $_name = mysql_real_escape_string($_POST['name']); $_w1 = mysql_real_escape_string($_POST['w1']); $_w2 = mysql_real_escape_string($_POST['w2']); $_w3 = mysql_real_escape_string($_POST['w3']); $_w4 = mysql_real_escape_string($_POST['w4']); $_w5 = mysql_real_escape_string($_POST['w5']); $_w6 = mysql_real_escape_string($_POST['w6']); $_comment = mysql_real_escape_string($_POST['comment']); // select all rows that are not the top 4 $sql = mysql_query("SELECT * FROM `wod` WHERE `id` NOT IN (SELECT * FROM (SELECT `id` FROM `wod` ORDER BY `id` DESC LIMIT 4) AS `ids_to_keep`)"); while($row = mysql_fetch_assoc($sql)) { $del = mysql_query(sprintf("DELETE FROM `wod` WHERE `id` = %d", (int) $row['id'])); // delete current row unlink($row['imagelocation']); // remove image from current row } $sql="INSERT INTO `wod` SET `month`='$_month', `day`='$_day', `year`='$_year', `name`='$_name', `w1`='$_w1', `w2`='$_w2', `w3`='$_w3', `w4`='$_w4', `w5`='$_w5', `w6`='$_w6', `comment`='$_comment', `imagelocation`='$path'"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } mysql_close($con); } ?>[/php]

Do you see anything outright wrong with the code, sir?

willscarlet, I have been really busy this week but I will try to help you with this tomorrow :slight_smile:

And you are the best, sir :slight_smile:

There’s not much I can do to help without actually doing the work for you. You must be able to read code and understand it if you ever want to be a programmer. All I did was copy & paste your tutorial into the proper location of your script. I also adjusted the ‘myfile’ key and paths.

Disclaimer: This may not actually work without further mods. I did it only as a demonstration to get you started :slight_smile:

[php]

<?php include 'core/init.php'; protect_page(); header("Location: wod_admin.php"); function GetFileName($f) { /* * this function will get the name of a file * for Instance if the file is image14.jpg * only image14 will be returned. BY wilson382 */ $h=substr($f,strrpos($f,"/"),strrpos($f,".")); $FileName= substr($h,1,strrpos($h,".")-1); return $FileName; } if(isset($_POST['submit'])) { /* * resizing tutorial */ // File Variables $name= $_FILES['myfile']['name']; $temp= $_FILES['myfile']['tmp_name']; $type= $_FILES['myfile']['type']; $size= $_FILES['myfile']['size']; $size2= getimagesize( $temp ); $width= $size2[0]; $height= $size2[1]; $upload= md5( rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) ); // Restrictions for uploading $maxwidth= 500; $maxheight= 500; $allowed= array( 'image/jpeg', 'image/png', 'image/gif' ); // Recognizing the extension switch( $type ){ // Image/Jpeg case 'image/jpeg': $ext= '.jpg'; break; // Image/png case 'image/png': $ext= '.png'; break; // Image/gif case 'image/gif': $ext= '.gif'; break; } // Upload Variables $path= 'images/wod/' . $upload . $ext; $thumb_path= 'images/wod/resized_' . $upload . $ext; // Check for the Image post. if( $_POST ){ // Got into the POST check. if( $_FILES ){ // Checking if the extension is allowed. if( in_array( $type, $allowed ) ){ // Checking if the resolution is FULLHD or under this resolution. if( $width <= $maxwidth && $height <= $maxheight ){ // Checking if the image is 5MB or less. if( $size <= 5242880 ){ // Check the shape of the image. if( $width == $height ){ $shape=1; } if( $width < $height ){ $shape=2; } if( $width > $height ){ $shape=3; } // Ajusting the resize script on shape. switch( $shape ){ // Code to resize a square image. case 1: $newwidth= 100; $newheight= 100; break; // Code to resize a tall image. case 2: $newwidth= 100; $ratio= $newwidth / $width; $newheight= round( $height * $ratio ); break; // Code to resize a wide image. case 3: $newheight= 100; $ratio= $newheight / $height; $newwidth= round( $width * $ratio ); break; } // Resizing according to extension. switch( $type ){ // Image/Jpeg case 'image/jpeg': $img= imagecreatefromjpeg( $temp ); $thumb= imagecreatetruecolor( $newwidth, $newheight ); imagecopyresized( $thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height ); imagejpeg( $thumb, $thumb_path ); break; // Image/png case 'image/png': $img= imagecreatefrompng( $temp ); $thumb= imagecreatetruecolor( $newwidth, $newheight ); imagecopyresized( $thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height ); imagepng( $thumb, $thumb_path ); break; // Image/gif case 'image/gif': $img= imagecreatefromgif( $temp ); $thumb= imagecreatetruecolor( $newwidth, $newheight ); imagecopyresized( $thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height ); imagegif( $thumb, $thumb_path ); break; } // Move the original file aswell. move_uploaded_file( $temp, $path ); // Putting out the data. header('Location: index.php'); }else{ die( "Your picture did not pass the size restrictions filter." ); } }else{ die( "Your picture did not pass the resolution restrictions filter." ); } }else{ die( "Your picture did not pass the extension restrictions filter." ); } } } /* * end resizing tutorial */ $_month = mysql_real_escape_string($_POST['month']); $_day = mysql_real_escape_string($_POST['day']); $_year = mysql_real_escape_string($_POST['year']); $_name = mysql_real_escape_string($_POST['name']); $_w1 = mysql_real_escape_string($_POST['w1']); $_w2 = mysql_real_escape_string($_POST['w2']); $_w3 = mysql_real_escape_string($_POST['w3']); $_w4 = mysql_real_escape_string($_POST['w4']); $_w5 = mysql_real_escape_string($_POST['w5']); $_w6 = mysql_real_escape_string($_POST['w6']); $_comment = mysql_real_escape_string($_POST['comment']); // select all rows that are not the top 4 $sql = mysql_query("SELECT * FROM `wod` WHERE `id` NOT IN (SELECT * FROM (SELECT `id` FROM `wod` ORDER BY `id` DESC LIMIT 4) AS `ids_to_keep`)"); while($row = mysql_fetch_assoc($sql)) { $del = mysql_query(sprintf("DELETE FROM `wod` WHERE `id` = %d", (int) $row['id'])); // delete current row unlink($row['imagelocation']); // remove image from current row } $sql="INSERT INTO `wod` SET `month`='$_month', `day`='$_day', `year`='$_year', `name`='$_name', `w1`='$_w1', `w2`='$_w2', `w3`='$_w3', `w4`='$_w4', `w5`='$_w5', `w6`='$_w6', `comment`='$_comment', `imagelocation`='$location'"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } mysql_close($con); } ?>

[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service