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:
<?php
//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;
if($filesize){
//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);}
}//if
//make width and height array from getimagesize returned array
list($width, $height) = getimagesize($filenametmp);
//calculate the image ratio
$imgratio=$width/$height;
if ($imgratio>1){
$newwidth = $ThumbWidth;
$newheight = $ThumbWidth/$imgratio;}
else{
$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
error#2:
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 :)