Hi all!

Im making an upload script that uploads an image file, saves it and saves a thumbnail copy in a different directory.

I have tried a couple of scripts and have found one which I think will work nicely but i cannot get it working.

The script is below:

//get values for other required vars
$filename = $_POST['uploadfile'];  //origional filename
$filetype = $_FILES['uploadfile']['type']; //uploaded filetype
$filesize = $_FILES['uploadfile']['size']; //uploaded file size
$filenametmp = $_FILES['uploadfile']['tmp_name']; //uploaded file's server side temporary name

$uploaddir = '../images/'; //upload directory
$uploadfile = $uploaddir.$iid.basename($_FILES['uploadfile']['name']); //upload directory + rand + origional name

$size = getimagesize($filenametmp);
$width = $size[0];
$height = $size[1];

//get file extension for validation
$ext = strrchr($file_name,'.');
$ext = strtolower($ext);
$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");	
$getExt = explode ('.', $filename);
$file_ext = $getExt[count($getExt)-1];

//set width variables
$ThumbWidth = 80;

		//keep image type
		if($file_type == "image/pjpeg" || $filenametmp == "image/jpeg" || $filenametmp == "image/jpg")
		{$new_img = imagecreatefromjpeg($filenametmp);}
		elseif($file_type == "image/x-png" || $filenametmp == "image/png")
		{$new_img = imagecreatefrompng($filenametmp);}
		elseif($file_type == "image/gif")
		{$new_img = imagecreatefromgif($filenametmp);}		

		//make width and height array from getimagesize returned array
		list($width, $height) = getimagesize($filenametmp);
		//calculate the image ratio
		if ($imgratio>1){
		$newwidth = $ThumbWidth;
		$newheight = $ThumbWidth/$imgratio;}
		$newheight = $ThumbWidth;
		$newwidth = $ThumbWidth*$imgratio;}
		//resize image.
		$resized_img = imagecreatetruecolor($newwidth,$newheight);
		//the resizing is going on here!
		//error 1 results from the line below:
		imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
		//finally, save the image
		ImageJpeg ($resized_img,"../thumbs/".$iid.$file_ext);
		ImageDestroy ($resized_img);
		//error 2 results form the line below:
		ImageDestroy ($new_img);
		move_uploaded_file ($filenametmp, "../images/".$iid.$file_ext);

The errors are:

error #1:
Warning: imagecopyresized(): supplied argument is not a valid Image resource in /home/sites/mister-style.com/public_html/photos/add/process.php on line 120

Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/sites/mister-style.com/public_html/photos/add/process.php on line 125

I hope somebody can help, iv spent aaages on trying to get something working xD

Thanks in advance,

Max :)


You seem to mix up your variables a lot. You define them using one name and then try to use them using another.
And you are validating the image based on the extension, which is pretty much useless. Validating the mime type returned by the getimagesize function is a lot more reliable.

This is a modified version of code I have used in the past.
It should do pretty much the same thing yours is meant to do:

header("Content-Type: text/plain");

// Validate upload
	die("File was not uploaded");
if($_FILES['uploadfile']['error'] != 0){
	die("File upload failed. (Code #". $_FILES['uploadfile']['error'] .")");

// Set and validate the file directory info
$uploadDir = "/path/to/upload/dir/";
$originalDir = $uploadDir . "originals/";
$thumbDir = $uploadDir . "thumbs/";
if(!is_writable($originalDir) || !is_writable($thumbDir)) {
	die("PHP does not have permission to write to the upload directories.");

// Get and validate the uploaded image information
$image = $_FILES['uploadfile'];
$imageInfo = getimagesize($image['tmp_name']);
$allowedMime = array("image/jpeg", "image/png", "image/gif");
if(!in_array($imageInfo['mime'], $allowedMime)) {
	die("Image Mime type is not allowed (". $imageInfo['mime'] .")");

// Move the original to it's new location
$originalPath = $originalDir . $image['name'];
if(!move_uploaded_file($image['tmp_name'], $originalPath)) {
	die("Failed to move the original image");

// Load the original into a GD object
switch ($imageInfo['mime']) {
	case "image/jpeg":
		$originalImage = imagecreatefromjpeg($originalPath);
	case "image/png":
		$originalImage = imagecreatefrompng($originalPath);
	case "image/gif":
		$originalImage = imagecreatefromgif($originalPath);
	// No default because the above mime check makes sure it isn't needed

// Calculate the thumb size
$thumbMaxSize = 80;
$originalRatio = $imageInfo[1] / $imageInfo[0];
if($originalRatio < 0) {
	$thumbWidth = $thumbMaxSize;
	$thumbHeight = $thumbMaxSize * $originalRatio;
else {
	$thumbHeight = $thumbMaxSize;
	$thumbWidth = $thumbMaxSize / $originalRatio;

// Create the thumb
$thumbImage = imagecreatetruecolor($thumbWidth, $thumbHeight);
if(!imagecopyresampled($thumbImage, $originalImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imageInfo[0], $imageInfo[1])){
	die("Failed to create the thumb");

// Save the thumb
$thumbPath = $thumbDir . "thumb_" . $image['name'];
switch ($imageInfo['mime']) {
	case "image/jpeg":
		imagejpeg($thumbImage, $thumbPath);
	case "image/png":
		imagepng($thumbImage, $thumbPath);
	case "image/gif":
		imagegif($thumbImage, $thumbPath);

// Delete resources

// Show success message
echo "Upload complete";

ahh fantastic, thanks for the script. I think I just dived in head first without trying to understand whats happening where first which is why my variables were so scrambled.

just incase anybody else wants to use the script I changed the permissions on my files to 755 and it worked great :)

Thanks again, much appreciated.


This question has already been answered. Start a new discussion instead.