2012-07-12 52 views
-1

我正在调整用户上传表单的图像大小,并且已将$_FILES数组的内容传递给了我的调整大小函数。该函数使用GD调整图像大小并将其保存在所需的目录中。

我需要制作两个图像副本:一个大副本和一个缩略图的小副本。当我尝试再次调用该函数(但具有不同的尺寸)来制作缩略图图像时,问题就出现了。我收到$_FILES阵列的unidentified index阵列消息。

$_FILES数组在传递给函数后是否自动删除,尽管我没有使用函数来清除它?

FUNCION CALL如下

if ($_FILES['image']['error'] != 4){ 
foto($_FILES['image'], $THUMBS_DIR, 400, 400, 1); 
foto($_FILES['image'], $THUMBS_DIR, 70, 70, 1); 
} 

功能

/*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';}} 

大家好我已经发布的代码,我本来didnt张贴监守我没有预料到人们希望看到它,我真的感谢它,感谢大家。

+3

您可以发布您的代码的相关部分? – 2012-07-12 19:57:04

+0

请务必检查文件大小是否超过'upload_max_filesize'或'post_max_size' ini指令 – 2012-07-12 19:58:31

+0

您能否包含相关函数? – 2012-07-12 19:59:34

回答

0

我很肯定这是因为$ _FILES是一个超全局的,因此当你在函数foto中使用它作为参数名时,它会被破坏,这样$ _FILES就成为你作为第一个参数传递的任何东西。

重写照片功能与除$ _FILES变量,例如:

function foto($image, $THUMBS_DIR, $MAX_WIDTH, $MAX_HEIGHT){ 
    // rest of code here with $_FILES changed to $image 
} 

此外,作为$ _FILES是一个超级全局的,你可以跳过使用它作为一个参数可言,改变电话是:

foto($THUMBS_DIR, 400, 400, 1) 

而且函数的形式为:

function foto($THUMBS_DIR, $MAX_WIDTH, $MAX_HEIGHT){ 
    //code placeholder 
    if (is_uploaded_file($_FILES['image']['tmp_name'])) {  
     $original = $_FILES['image']['tmp_name']; 
    //rest of code 
} 
+0

作品一种享受!非常感谢你。我接受了你的第二个建议,并且直接开始工作,没有和脑筋急转弯。 Robert – 2012-07-13 07:25:59

相关问题