I am resizing an image from a user upload form, and I have passed the contents of the $_FILES
array to my resizing function. The function uses GD to resize the image and save it in the desired directory.
I need to make two copies of the image: a large copy and a small copy for a thumbnail. The problem arises when I try to call the function for a second time (but with different dimensions) to make the thumbnail image. I get an unidentified index
array message for the $_FILES
array.
Is the $_FILES
array being deleted automatically after passing it to the function, despite the fact that I have not used the functions to clear it?
FUNCION CALL is as follows
if ($_FILES['image']['error'] != 4){
foto($_FILES['image'], $THUMBS_DIR, 400, 400, 1);
foto($_FILES['image'], $THUMBS_DIR, 70, 70, 1);
}
FUNCTION
/*foto function, foto upload, where to save, fotowidth, fotosize,*/
function foto($_FILES, $THUMBS_DIR, $MAX_WIDTH, $MAX_HEIGHT){
/*generate random name*/
$fecha = time();$passpart1 = $fecha;$passpart2 = mt_rand(1, 1000);$ps = $passpart1.$passpart2;$ps2= $passpart1.$passpart2.'thumb';$thumbref='0';
if (is_uploaded_file($_FILES['tmp_name']))
{
/*resize ratio*/
$original = $_FILES['tmp_name'];
list($width, $height, $type) = getimagesize($original);
if ($width <= $MAX_WIDTH && $height <= $MAX_HEIGHT) {$ratio = 1;}
elseif ($width > $height) {$ratio = $MAX_WIDTH/$width;}
else {$ratio = $MAX_HEIGHT/$height;}
$imagetypes = array('/\.gif$/','/\.jpg$/','/\.jpeg$/','/\.png$/');
$name = preg_replace($imagetypes, '', basename($original));
$name=$ps;switch($type)
{case 1:$source = @ imagecreatefromgif($original);if (!$source) {$result = 'Cannot process GIF files. Please use JPEG or PNG.';}break;
case 2:$source = imagecreatefromjpeg($original);break;
case 3:$source = imagecreatefrompng($original);break;
default:$source = NULL;$result = 'Cannot identify file type.';}
if (!$source) {$result = 'Problem copying original';}
else {
$thumb_width = round($width * $ratio);
$thumb_height = round($height * $ratio);
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
switch($type)
{case 1:if (function_exists('imagegif')) {$success = imagegif($thumb, $THUMBS_DIR.$ps.'.gif');$thumb_name = $ps.'.gif';}
else {$success = imagejpeg($thumb, $THUMBS_DIR.$ps.'.jpg', 50);$thumb_name = $ps.'.jpg';}break;
case 2:$success = imagejpeg($thumb, $THUMBS_DIR.$ps.'.jpg', 100);$thumb_name = $ps.'.jpg';break;
case 3:$success = imagepng($thumb, $THUMBS_DIR.$ps.'.png');$thumb_name = $ps.'.png';}
/*destroy temp or not*/
if ($success) {$result = "$thumb_name created";}
}
$fotref = $thumb_name;
}
else{$errorreport='error with upload';}}
Hello again i have posted the code, I didnt originally post it becuase i didnt expect people to want to see it , I really appreciate it, Thanks to everybody.