2011-02-25 171 views
1

我已经完成了图片的代码(单个按钮点击超过1个)上传,而上传图片缩略图想要生成并保存到分开的文件夹...在我的代码图片上传工作完美还第一缩略图也生成缩略图文件夹,缩略图的其余部分不产生..抛出一个错误 这是我的图片上传并创建缩略图代码图片缩略图问题

$uploadDir = $_SERVER[DOCUMENT_ROOT].'/aqua/v_images/'; 

if(!empty($_FILES['img1']['name'])) { 

    $fileName1 = $_FILES['img1']['name']; 
    $tmpName1 = $_FILES['img1']['tmp_name']; 
    $fileSize1 = $_FILES['img1']['size']; 
    $fileType1 = $_FILES['img1']['type']; 
    $ext1 = substr(strrchr($fileName1, "."), 1); 
    $randName1 = md5(rand() * time()); 

    $encFileName1 = $randName1.'.'.$ext1; 
    $filePath1 = $uploadDir . $encFileName1; 

    $result1 = move_uploaded_file($tmpName1, $filePath1); 
    if (!$result1) { 
     echo "Please Uploade a Image to Image 1 area"; 
     exit; 
    } 
    if(!get_magic_quotes_gpc()) { 
     $fileName1 = addslashes($fileName1); 
     $filePath1 = addslashes($filePath1); 
    } 
    $thumb_name = $_SERVER[DOCUMENT_ROOT].'/aqua/v_thumb/'.$encFileName1; 
    $thumb=make_thumb($filePath1,$thumb_name,100,100); 
} 
////////////////////////////////////image2//////////////// 

if(!empty($_FILES['img2']['name'])) { 

    $fileName2 = $_FILES['img2']['name']; 
    $tmpName2 = $_FILES['img2']['tmp_name']; 
    $fileSize2 = $_FILES['img2']['size']; 
    $fileType2 = $_FILES['img2']['type']; 
    $ext2 = substr(strrchr($fileName2, "."), 1); 
    $randName2 = md5(rand() * time()); 

    $encFileName2 = $randName2.'.'.$ext2; 
    $filePath2 = $uploadDir . $encFileName2; 

    $result2 = move_uploaded_file($tmpName2, $filePath2); 

    if(!get_magic_quotes_gpc()) { 
     $fileName2 = addslashes($fileName2); 
     $filePath2 = addslashes($filePath2); 
    } 
    $thumb_name = $_SERVER[DOCUMENT_ROOT].'/aqua/v_thumb/'.$encFileName2; 
    $thumb=make_thumb($filePath2,$thumb_name,100,100); 
} 

这是make_thumb功能

<?php 
function make_thumb($img_name,$filename,$new_w,$new_h) 
{ 
    //get image extension. 
    $ext=getExtension($img_name); 
    //creates the new image using the appropriate function from gd library 
    if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext)) 
     $src_img=imagecreatefromjpeg($img_name); 

    if(!strcmp("png",$ext)) 
     $src_img=imagecreatefrompng($img_name); 

     //gets the dimmensions of the image 
    $old_x=imageSX($src_img); 
    $old_y=imageSY($src_img); 

    // next we will calculate the new dimmensions for the thumbnail image 
    // the next steps will be taken: 
    // 1. calculate the ratio by dividing the old dimmensions with the new ones 
    // 2. if the ratio for the width is higher, the width will remain the one define in WIDTH variable 
    //  and the height will be calculated so the image ratio will not change 
    // 3. otherwise we will use the height ratio for the image 
    // as a result, only one of the dimmensions will be from the fixed ones 
    $ratio1=$old_x/$new_w; 
    $ratio2=$old_y/$new_h; 
    if($ratio1>$ratio2) { 
     $thumb_w=$new_w; 
     $thumb_h=$old_y/$ratio1; 
    } 
    else { 
     $thumb_h=$new_h; 
     $thumb_w=$old_x/$ratio2; 
    } 

    // we create a new image with the new dimmensions 
    $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); 

    // resize the big image to the new created one 
    imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 

    // output the created image to the file. Now we will have the thumbnail into the file named by $filename 
    if(!strcmp("png",$ext)) 
     imagepng($dst_img,$filename); 
    else 
     imagejpeg($dst_img,$filename); 

    //destroys source and destination images. 
    imagedestroy($dst_img); 
    imagedestroy($src_img); 

} 

// This function reads the extension of the file. 
// It is used to determine if the file is an image by checking the extension. 
function getExtension($str) { 
     $i = strrpos($str,"."); 
     if (!$i) { return ""; } 
     $l = strlen($str) - $i; 
     $ext = substr($str,$i+1,$l); 
     return $ext; 
}?> 

表现Ë在功能make_thumb()

Warning: imagesx(): supplied argument is not a valid Image resource in C:\AppServ\www\Aqua\thumb.php on line 14 

Warning: imagesy(): supplied argument is not a valid Image resource in C:\AppServ\www\Aqua\thumb.php on line 15 

Warning: Division by zero in C:\AppServ\www\Aqua\thumb.php on line 32 

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in C:\AppServ\www\Aqua\thumb.php on line 36 

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in C:\AppServ\www\Aqua\thumb.php on line 39 

Warning: imagejpeg(): supplied argument is not a valid Image resource in C:\AppServ\www\Aqua\thumb.php on line 45 

Warning: imagedestroy(): supplied argument is not a valid Image resource in C:\AppServ\www\Aqua\thumb.php on line 48 

Warning: imagedestroy(): supplied argument is not a valid Image resource in C:\AppServ\www\Aqua\thumb.php on line 49 
+0

你解决了这个问题了吗? –

+0

还没有?请如果你能帮助MEEEE – Wazan

+0

@ user618124:看看我的更新回答 –

回答

1

改变这一行
$ext2 = substr(strrchr($fileName, "."), 1);

对此
$ext2 = substr(strrchr($fileName2, "."), 1);

+0

但第一个图像上传并完全生成缩略图!多数民众赞成什么我不能undestand ..哪里错了! – Wazan

+1

我已经更新了我的答案 –

+0

我纠正了这个错误!但仍然有同样的问题! – Wazan