2011-02-25 206 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