Jump to content

Recommended Posts

This code below was originally ONLY for JPEGs and it worked fine.  Today, I added all the code relating to GIFs and PNGs.  Now when I test it, the GIFs get created properly, but the PNGs do not.  So overall, the JPGs and GIFs are handled as intended, but PNGs are not. 

 

Does anyone see anything wrong with this code that would prevent the PNGs from getting created?

 

FYI, I just have a simple "file upload" form where the user can pick an image from their computer and then this code below goes to work...

 

NOTE:  This original code is several years old, so if there are better ways to handle image creation/resizing than what this code uses, please direct me toward those new ways.  I want to be current :)...

 

//upload and resize and add new image

		if ($_FILES['userfile']['type'] == "image/pjpeg" || $_FILES['userfile']['type'] == "image/jpeg"  || $_FILES['userfile']['type'] == "image/gif" ) {

			if ($_FILES['userfile']['type'] == "image/pjpeg" || $_FILES['userfile']['type'] == "image/jpeg") {
				$imgtype="jpg";
			} else if ($_FILES['userfile']['type'] == "image/gif") {
				$imgtype="gif";
			} else {
				$imgtype="png";
			}

								/// find existing file (if there is one) and delete it
								if (file_exists('images/userimages/user' . $_SESSION['userid'] . '.jpg')) {
								//remove existing image
								unlink('images/userimages/user' . $_SESSION['userid'] . '.jpg');
								}
								if (file_exists('images/userimages/user' . $_SESSION['userid'] . '.png')) {
									//remove existing image
								unlink('images/userimages/user' . $_SESSION['userid'] . '.png');
								} 
								if (file_exists('images/userimages/user' . $_SESSION['userid'] . '.gif')) {
									//remove existing image
								unlink('images/userimages/user' . $_SESSION['userid'] . '.gif');
								} 


							// This is the temporary file created by PHP 
							$uploadedfile = $_FILES['userfile']['tmp_name'];

							// Create an Image from it so we can do the resize
							if ($imgtype == "jpg") {
							$src = imagecreatefromjpeg($uploadedfile);
							} elseif ($imgtype == "png") {
							$src = imagecreatefrompng($uploadedfile);
							} else {
							$src = imagecreatefromgif($uploadedfile);
							}	


							// Capture the original size of the uploaded image
							list($width,$height) = getimagesize($uploadedfile);


							$newheight=50;
							$newwidth= 50;

							/*
								if ($newwidth1 > 100) {
									$toowide1 = "yes";
									$newwidth1=100;

									} else {																	
									}

								$_SESSION['width1'] = $newwidth1;
								*/
							$tmp=imagecreatetruecolor($newwidth,$newheight);

							// this line actually does the image resizing, copying from the original
							// image into the $tmp image


							imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 

							// now write the resized image to disk. I have assumed that you want the
							// resized, uploaded image file to reside in the ./images subdirectory.



							if ($imgtype=="jpg") {
								$imagefolder = 'images/userimages/user' . $_SESSION['userid'] . '.jpg';
							} else if ($imgtype=="png") {
								$imagefolder = 'images/userimages/user' . $_SESSION['userid'] . '.png';
							} else {
								$imagefolder = 'images/userimages/user' . $_SESSION['userid'] . '.gif';
							}
							$filename = $imagefolder;
							$_SESSION['filename'] = $filename;
							if (imagejpeg($tmp,$imagefolder,100)) {
								$imagemessage = "File is valid, and was successfully uploaded into FOLDER.\n";
								$success="yes";
								} else {
								$imagemessage = "Possible file upload attack!\n";
								$success="no";
								}
							imagedestroy($src);
							imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request
							// has completed.

			}
redirect_to('profile.php');

}
?>

Link to comment
https://forums.phpfreaks.com/topic/240273-working-with-images-in-php/
Share on other sites

When you have a list of possible values, it is usually easier (and less error prone) to use an array (or a database) to hold the list -

<?php
$types = array();
$types["image/pjpeg"] = "jpg";
$types["image/jpeg"] = "jpg";
$types["image/gif"] = "gif";
$types["image/png"] = "png";
// add other types here...

$imgtype = false;
if(isset($types[$_FILES['userfile']['type']])){
$imgtype = $types[$_FILES['userfile']['type']];

// the rest of your code to process the image goes here...

} else {
// when validating user supplied data, supply as much information as possible about why the data is not valid
echo "The image type: {$_FILES['userfile']['type']}, is not supported!<br />";
echo "Only the following types are allowed: " . implode(', ',array_keys($types)) . "!";
}
?>

Thanks all, especially PFMaBiSmAd for the efficiency advice!  One question if anyone has any ideas....  when this code runs, it deletes the old image, creates the new image and then redirects to a separate page which display the existing image.  So it's weird that SOMETIMES the image does not refresh to the new one, but sometimes it does.

 

For example, if there is a picture of an apple and I choose to change the image to a banana, it will process the code (which deletes the apple image and then creates the banana image) and redirects me back to the main "profile" page which displays the "current" image. 

 

This should show the new banana image, but sometimes it still shows the "old" apple image.  If I manually hit refresh, the new banana image will show up.

 

So, is there a way to force everything to refresh automatically and completely?  I would expect redirecting to a brand new page would be enough to make sure the new image shows, but for some reason it doesn't always work.

 

 

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.