Hi, I am trying to build a function and from a script but it does not seem to be working. Can anyone see anything wrong with this? Also i need it to convert the large image to a specific size also, like the thumbnail so i need to put something in there so i can enter a width of the large image and this will be resized also. Here is the function: function uploadimage($value){ $path_thumbs = "uploads/thumbs"; $path_big = "uploads/big"; //the new width of the resized image. $img_thumb_width = 150; // in pixcel $extlimit = "yes"; //Do you want to limit the extensions of files uploaded (yes/no) //allowed Extensions $limitedext = array(".gif",".jpg",".png",".jpeg",".bmp"); $file_type = $_FILES['image']['type']; $file_name = $_FILES['image']['name']; $file_size = $_FILES['image']['size']; $file_tmp = $_FILES['image']['tmp_name']; //check file extension $ext = strrchr($file_name,'.'); $ext = strtolower($ext); if (($extlimit == "yes") && (!in_array($ext,$limitedext))) { echo "Wrong file extension. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>"; exit(); } //get the file extension. $getExt = explode ('.', $file_name); $file_ext = $getExt[count($getExt)-1]; //create a random file name $rand_name = md5(time()); $rand_name= rand(0,999999999); //get the new width variable. $ThumbWidth = $img_thumb_width; //keep image type if($file_size){ if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){ $new_img = imagecreatefromjpeg($file_tmp); }elseif($file_type == "image/x-png" || $file_type == "image/png"){ $new_img = imagecreatefrompng($file_tmp); }elseif($file_type == "image/gif"){ $new_img = imagecreatefromgif($file_tmp); } //list width and height and keep height ratio. list($width, $height) = getimagesize($file_tmp); $imgratio=$width/$height; if ($imgratio>1){ $newwidth = $ThumbWidth; $newheight = $ThumbWidth/$imgratio; }else{ $newheight = $ThumbWidth; $newwidth = $ThumbWidth*$imgratio; } imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); //save image ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext"); ImageDestroy ($resized_img); ImageDestroy ($new_img); //upload the big image move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext"); } $value = "$rand_name.$file_ext"; return $value; } PHP: Thanks in advanced. Adam
Right i have built this to test out the function. It is uploading the normal image now but is not uploading the thumbs. <?php function uploadimage($value){ $file_type = $value['type']; $file_name = $value['name']; $file_size = $value['size']; $file_tmp = $value['tmp_name']; //check file extension $ext = strrchr($file_name,'.'); $ext = strtolower($ext); //get the file extension. $getExt = explode ('.', $file_name); $file_ext = $getExt[count($getExt)-1]; //create a random file name $rand_name = md5(time()); $rand_name= rand(0,999999999); //get the new width variable. $ThumbWidth = 150; //keep image type if($file_size){ if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){ $new_img = imagecreatefromjpeg($file_tmp); }elseif($file_type == "image/x-png" || $file_type == "image/png"){ $new_img = imagecreatefrompng($file_tmp); }elseif($file_type == "image/gif"){ $new_img = imagecreatefromgif($file_tmp); } //list width and height and keep height ratio. list($width, $height) = getimagesize($file_tmp); $imgratio=$width/$height; if ($imgratio>1){ $newwidth = $ThumbWidth; $newheight = $ThumbWidth/$imgratio; }else{ $newheight = $ThumbWidth; $newwidth = $ThumbWidth*$imgratio; } imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); //save image ImageJpeg ($resized_img,"uploads/thumbs/$rand_name.$file_ext"); ImageDestroy ($resized_img); ImageDestroy ($new_img); //upload the big image move_uploaded_file ($file_tmp, "uploads/big/$rand_name.$file_ext"); } $value = "$rand_name.$file_ext"; return $value; } if ($_POST){ $image = uploadimage($_FILES['image']); $image2 = uploadimage($_FILES['image2']); $image3 = uploadimage($_FILES['image3']); print "Image 1: <a href='uploads/big/$image'>$image</a>"; print "<br>"; print "Image 2: <a href='uploads/big/$image3'>$image2</a>"; print "<br>"; print "Image 3: <a href='uploads/big/$image2'>$image3</a>"; } ?> <form action="" method="post" enctype="multipart/form-data"> <table width="100%" border="0" cellspacing="0" cellpadding="3"> <tr> <td><input name="image" type="file" id="image" /></td> </tr> <tr> <td><input name="image2" type="file" id="image2" /></td> </tr> <tr> <td><input name="image3" type="file" id="image3" /></td> </tr> <tr> <td><input type="submit" name="Submit" value="Submit" /></td> </tr> </table> </form> PHP:
Well here's one problem. ImageJpeg and ImageDestroy shouldn't be capitalized like that. They should be all lower case - imagejpeg() and imagedestroy(). That should cause a fatal error, though - "Call to undefined function." - Walkere
I have tried adding: move_uploaded_file ($resized_img, "uploads/thumbs/$rand_name.$file_ext"); After ImageDestroy ($new_img); But that does not work.
Where would you suggest it going? I have put it here and this still doesn't work. //save image move_uploaded_file ($resized_img, "uploads/thumbs/$rand_name.$file_ext"); imagejpeg ($resized_img,"uploads/thumbs/$rand_name.$file_ext"); imagedestroy ($resized_img); imagedestroy ($new_img); PHP: