Jump to content

Image upload and resize issues


andrew_biggart

Recommended Posts

Apologies for another thread about this, but i have completely re-written my previous code so it's no longer relevant to my previous post, hence why i am creating a new one.

 

I am trying to create an image upload script which resizes the uploaded image to a set parameter and then saves it to an upload folder as well as the database.

 

At the minute the image is being saved but not resized can anyone see what I am doing wrong?.

 

                <?php

				$upload_dir = 'uploads/';
				$thumbs_dir = 'thumbs/';
				$max_width  = 570;

				// Check if the upload and thumbs directories exist.
				// If they don't then we create them.

				if(!file_exists($upload_dir)) {

					if(mkdir($upload_dir)) {

						if(!file_exists($upload_dir . $thumbs_dir)) {

							if(mkdir($upload_dir . $thumbs_dir)) {

							}
							else {

								exit_status($upload_dir . 'directory could not be created! Please check your folder permissions.');

							}

						}

					}
					else {

						exit_status($upload_dir . 'directory could not be created! Please check your folder permissions.');

					}

				}

				$allowed_ext = array('jpg','jpeg', 'png','gif');
				$unique_id   = date("YmdHis");
				$unique_id   = $unique_id . rand(0,999999999);

				if(isset($_POST['upload_file'])){

					$file_tmp   = $_FILES['pic']['tmp_name'];
					$file_name  = $_FILES['pic']['name'];
					$file_size  = $_FILES['pic']['size'];
					$file_ext   = get_extension($_FILES['pic']['name']);
					$file_new   = $unique_id . "." . $file_ext;
					$createdby  = "Andrew";

					if(in_array(get_extension($file_name),$allowed_ext)){

						if($file_size < 2000000) {

							// Check the width of the uploaded image
							list($file_width,$file_height) = getimagesize($file_tmp);

							if($file_width > $max_width) {




								if($file_ext == "jpg" || $file_ext == "jpeg" ){
									$src = imagecreatefromjpeg($file_tmp);
								}
								else if($file_ext == "png"){
									$src = imagecreatefrompng($file_tmp);
								}
								else if($file_ext == "gif"){
									$src = imagecreatefromgif($file_tmp);
								}


								$file_height_new = ($height/$width) * $max_width;
								$tmp = imagecreatetruecolor($max_width,$file_height_new);


								imagecopyresampled($tmp,$src,0,0,0,0,$max_width,$file_height_new,$width,$height);

								imagejpeg($tmp,$upload_dir . $file_new,100);


								if(move_uploaded_file($file_tmp, $upload_dir . $file_new)){

									connect();

									$link   = get_option('admin_url');
									$sql    = " INSERT into mediaT ( name, url, link, createdon, createdby ) VALUES ( '$file_name', '$upload_dir$file_new', '$link$upload_dir$file_new', NOW(), '$createdby' ) ";
									$result = mysql_query($sql);

									if($result) {
										echo "Success! <i>$file_name has been added to your media library!</i>";
									}
									else {
										echo "Error! <i>File couldn't be saved to the database.</i>";
									}
								}
								else {
									echo "Error! <i>File couldn't be saved to $upload_dir.</i>";
								}

								imagedestroy($src);
								imagedestroy($tmp);	




							}
							else {

								if(move_uploaded_file($file_tmp, $upload_dir . $file_new)){

									connect();

									$link   = get_option('admin_url');
									$sql    = " INSERT into mediaT ( name, url, link, createdon, createdby ) VALUES ( '$file_name', '$upload_dir$file_new', '$link$upload_dir$file_new', NOW(), '$createdby' ) ";
									$result = mysql_query($sql);

									if($result) {
										echo "Success! <i>$file_name has been added to your media library!</i>";
									}
									else {
										echo "Error! <i>File couldn't be saved to the database.</i>";
									}
								}
								else {
									echo "Error! <i>File couldn't be saved to $upload_dir.</i>";
								}

							}

						}
						else {
							echo "Error! <i>Filesize is to large. Please upload a file under 2MB.</i>";
						}

					}
					else {
						echo "Error! <i>Only the following file types are allowed ".implode(',',$allowed_ext).".</i>";
					}

				}
				else {
					echo "Select a file to upload.<br /> <i>(With a maximum filesize of 2MB)</i>";
				}

				function get_extension($file_name){
					$ext = explode('.', $file_name);
					$ext = array_pop($ext);
					return strtolower($ext);
				}
			?>

Link to comment
Share on other sites

A quick look seems to indicate the problem can be found in these four lines...

$tmp = imagecreatetruecolor($max_width,$file_height_new);
imagecopyresampled($tmp,$src,0,0,0,0,$max_width,$file_height_new,$width,$height);
imagejpeg($tmp,$upload_dir . $file_new,100);
if(move_uploaded_file($file_tmp, $upload_dir . $file_new)){

 

it appears that you are saving the resized image,  then overwriting it with the original.

Link to comment
Share on other sites

Apologies, I had tried to modify the code as from my orginal which is posted below. I am still getting the same issue with that as well tho. The image is being saved in it's orginal state, and not being resized. I'd imagine it should be something quite simple, but I cannot for the life of me spot what the issue is.

 

                <?php
				ini_set(?display_errors?,1);
				error_reporting(E_ALL);

				$upload_dir = 'uploads/';
				$max_width  = 570;

				// Check if the upload and thumbs directories exist.
				// If they don't then we create them.

				if(!file_exists($upload_dir)) {

					if(mkdir($upload_dir)) {


					}
					else {

						exit_status($upload_dir . 'directory could not be created! Please check your folder permissions.');

					}

				}

				$allowed_ext = array('jpg','jpeg', 'png','gif');
				$unique_id   = date("YmdHis");
				$unique_id   = $unique_id . rand(0,999999999);

				if(isset($_POST['upload_file'])){

					$file_tmp   = $_FILES['pic']['tmp_name'];
					$file_name  = $_FILES['pic']['name'];
					$file_size  = $_FILES['pic']['size'];
					$file_ext   = get_extension($_FILES['pic']['name']);
					$file_new   = $unique_id . "." . $file_ext;
					$createdby  = $_SESSION["ufullname"];

					if(in_array(get_extension($file_name),$allowed_ext)){

						if($file_size < 2000000) {

							// Check the width of the uploaded image
							list($file_width,$file_height) = getimagesize($file_tmp);

							if($file_width > $max_width) {

								if(move_uploaded_file($file_tmp, $upload_dir . $file_new)){

									connect();

									$link   = get_option('admin_url');
									$sql    = " INSERT into mediaT ( name, url, link, createdon, createdby ) VALUES ( '$file_name', '$upload_dir$file_new', '$link$upload_dir$file_new', NOW(), '$createdby' ) ";
									$result = mysql_query($sql);

									if($result) {

										if($file_ext == "jpg" || $file_ext == "jpeg" ){
											$src = imagecreatefromjpeg($file_tmp);
										}
										else if($file_ext == "png"){
											$src = imagecreatefrompng($file_tmp);
										}
										else if($file_ext == "gif"){
											$src = imagecreatefromgif($file_tmp);
										}


										$file_height_new = ($height/$width) * $max_width;
										$tmp = imagecreatetruecolor($max_width,$file_height_new);


										imagecopyresampled($tmp,$src,0,0,0,0,$max_width,$file_height_new,$width,$height);

										imagejpeg($tmp,$upload_dir . $file_new,100);

										echo "Success! <i>$file_name has been added to your media library!</i>";

										imagedestroy($src);
										imagedestroy($tmp);	

									}
									else {
										echo "Error! <i>File couldn't be saved to the database.</i>";
									}
								}
								else {
									echo "Error! <i>File couldn't be saved to $upload_dir.</i>";
								}

							}
							else {

								if(move_uploaded_file($file_tmp, $upload_dir . $file_new)){

									connect();

									$link   = get_option('admin_url');
									$sql    = " INSERT into mediaT ( name, url, link, createdon, createdby ) VALUES ( '$file_name', '$upload_dir$file_new', '$link$upload_dir$file_new', NOW(), '$createdby' ) ";
									$result = mysql_query($sql);

									if($result) {
										echo "Success! <i>$file_name has been added to your media library!</i>";
									}
									else {
										echo "Error! <i>File couldn't be saved to the database.</i>";
									}
								}
								else {
									echo "Error! <i>File couldn't be saved to $upload_dir.</i>";
								}

							}

						}
						else {
							echo "Error! <i>Filesize is to large. Please upload a file under 2MB.</i>";
						}

					}
					else {
						echo "Error! <i>Only the following file types are allowed ".implode(',',$allowed_ext).".</i>";
					}

				}
				else {
					echo "Select a file to upload.<br /> <i>(With a maximum filesize of 2MB)</i>";
				}

				function get_extension($file_name){
					$ext = explode('.', $file_name);
					$ext = array_pop($ext);
					return strtolower($ext);
				}
			?>

Link to comment
Share on other sites

I have also tried creating a separate directory called thumbs to store the resized images by changing the following:

 

imagejpeg($tmp,$upload_dir . $file_new,100);

 

Which I changed to

 

imagejpeg($tmp,"uploads/thumbs/" . $file_new,100);

 

The resized images are not getting saved into the directory so therefor I'm assuming the problem is to do with the resizing part of the code but I can figure out what. From what I can see I am currently resizing the image using the following lines of code.

 

imagecopyresampled($tmp,$src,0,0,0,0,$max_width,$file_height_new,$width,$height);

 

I understand that I will need to create if statements based on the extension to recreate either jpgs, gifs and pngs using imagejpeg, imagegif and imagepng. But I have left them out for now until I get the resizing part working. While I am testing this current script i am only using jpgs.

 

Can anyone spot something I ma missing?

 

Link to comment
Share on other sites

I was looking at this at first what your saying is  your max width is 570

but lets say the image is 600 x 200

so 600/200 = 3    x  570  =  1710  = $file_height_new

Would this be a problem.

 

$file_height_new = ($height/$width) * $max_width;

Link to comment
Share on other sites

That is a very valid point lordshoa, thank  you for pointing that out. What method would you suggest that I use then?

 

I also made sure that this wasn't causing the original issue of the image not being resized by changing the $file_height_new variable to a whole number.

 

I am not trying to make images bigger, I only want to resize the image if it's width is larger than 570px. The reason for this is so that all uploaded images are in keeping with the websites design.

Link to comment
Share on other sites

Ok so I have found what the problem was that was causing the image not to resize properly.

 

I had been defining the width and height like this.

 

list($file_width,$file_height) = getimagesize($file_tmp);

 

But in my following lines of code I was using the variables $width and $height instead of $file_width and $file_height. I changed the following three lines of code as follows:

 

$file_height_new = ($height/$width) * $max_width;
$tmp = imagecreatetruecolor($max_width,$file_height_new);
imagecopyresampled($tmp,$src,0,0,0,0,$max_width,$file_height_new,$width,$height);

 

to this

 

$file_height_new = ($file_height/$file_width) * $max_width;
$tmp = imagecreatetruecolor($max_width,$file_height_new);
imagecopyresampled($tmp,$src,0,0,0,0,$max_width,$file_height_new,$file_width,$file_height);

 

This has now resolved the resizing issue, however I have now ran into the same problem I had with my orginal script. The problem is that the thumbnail image that is being created is completely black. I don't know why but the original image isn't be copied properly. Does anyone have any ideas?

 

 

Link to comment
Share on other sites

Has your code been producing any php error messages?

 

I ask for two reasons, it should have been, over the $height/$width variables that don't exist and your ini_set(?display_errors?,1); code is displaying with ? marks instead of straight single or double quotes and is probably not actually having an effect. Try retyping that ini_set statement using straight quotes and see if you get any php error messages.

Link to comment
Share on other sites

Firstly PFMaBiSmAd, YOU ARE MY HERO! I can't believe that I hadn't noticed that before now. My error reporting had those weird american quotation marks.

 

Once I had fixed this I was able to figure out that because I had moved the temporary file to the uploads directory I was longer able to use it in the imagecreatefromjpeg part of my script. The error I was getting was a failure to open stream because it was having trouble finding the image because it no longer existed.

 

I changed the following lines of code:

 

if($file_ext == "jpg" || $file_ext == "jpeg" ){
   $src = imagecreatefromjpeg($file_tmp);
}
else if($file_ext == "png"){
   $src = imagecreatefrompng($file_tmp);
}
else if($file_ext == "gif"){
   $src = imagecreatefromgif($file_tmp);
}

 

to the following.

 

if($file_ext == "jpg" || $file_ext == "jpeg" ){
   $src = imagecreatefromjpeg($upload_dir . $file_new);
}
else if($file_ext == "png"){
   $src = imagecreatefrompng($upload_dir . $file_new);
}
else if($file_ext == "gif"){
   $src = imagecreatefromgif($upload_dir . $file_new);
}

 

The resized image is now being created properly. Thanks for everyones help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.