2011-03-28 100 views
0

我使用這段代碼來製作上傳圖像的縮略圖。PHP GD圖像裁剪功能

$file = randString().'.jpg'; 
    $uniPath = 'i/u'.$login; 
    $completePath = $uniPath.'/a/'.$file; 
    $thumbPath = $uniPath.'/b/'.$file; 
    if(copy($_FILES['filename']['tmp_name'], $completePath)){ 

function convertPic($w_dst, $h_dst, $n_img){ 
    $wxh = $w_dst.'x'.$h_dst; 
    switch($wxh){ 
    case '150x150': $letter = 'b'; break; 
    case '50x50': $letter = 'c'; break; 
    default: $letter = 'z'; 
    } 
    $dbPath = '/i/u1/'.$letter.'/'.$n_img; 
    $new_img = $_SERVER['DOCUMENT_ROOT'].$dbPath; 
    $file_src = "img.jpg"; // temporary safe image storage 
    $img_src = $file_src; 
    unlink($file_src); 
    move_uploaded_file($_FILES['filename']['tmp_name'], $file_src); 

    list($w_src, $h_src, $type) = getimagesize($file_src);  // create new dimensions, keeping aspect ratio 

    $ratio = $w_src/$h_src; 

    $h_ratio = ($h_dst/$h_src); 
    $w_ratio = ($w_dst/$w_src); 

    if($w_src > $h_src){ //landscape 
    $w_crop = round($w_src * $h_ratio); 
    $h_crop = $h_dst; 
    $src_x = ceil(($w_src - $h_src)/2); 
    $src_y = 0; 
    } 
    elseif($w_src < $h_src){ // portrait 
    $h_crop = round($h_src * $w_ratio); 
    $w_crop = $w_dst; 
    $src_y = ceil(($h_src - $w_src)/2); 
    $src_x = 0; 
    } 
    else { //square 
    $w_crop = $w_dst; 
    $h_crop = $h_dst; 
    $src_x = 0; 
    $src_y = 0; 
    } 

    switch ($type) 
    {case 1: // gif -> jpg 
     $img_src = imagecreatefromgif($file_src); 
     break; 
     case 2: // jpeg -> jpg 
     $img_src = imagecreatefromjpeg($file_src); 
     break; 
     case 3: // png -> jpg 
     $img_src = imagecreatefrompng($file_src); 
     break; 
    } 
    $img_dst = imagecreatetruecolor($w_dst, $h_dst); // resample 
    imagecolorallocate($img_dst, 255, 255, 255) or die("fail imagecolorallocate"); 

    imagecopyresampled($img_dst, $img_src, 0, 0, $src_x, $src_y, $w_crop, $h_crop, $w_src, $h_src) or die("imagecopyresampled($img_dst, $img_src, 0, 0, $src_x, $src_y, $w_crop, $h_crop, $w_src, $h_src)"); 
    imagejpeg($img_dst, $new_img); // save new image 

    unlink($file_src); // clean up image storage 
    imagedestroy($img_src);  
    imagedestroy($img_dst); 
    return $db_path; 
    } 

convertPic(150, 150, $file); 
convertPic(250, 250, $file); 

但出於某種原因,如果調用兩次在上面的例子中,convertPic功能不起作用。如果它被稱爲一旦一切正常。我已經把一個警告,如果imagecopyresampled失敗,它輸出

imagecopyresampled(資源ID#17, img.jpg,0,0,0,0,250,250,,)

我認爲問題是臨時圖像存儲但不確定。請幫忙。

回答

0

我猜問題是取消鏈接($ file_src)...

首先,你叫convertPic()功能,因爲你的img.jpg還在這裏它工作正常,那麼你unlink()刪除您的圖片,並嘗試再次調用實際需要此文件才能正常工作的相同例程。

此外,您不能將同一個文件移動兩次,因此您必須將其移出該功能,根據需要多次執行任務,然後取消鏈接圖像。取消鏈接後,應該在convertPic()move_uploaded_file()雙重呼叫之後應該在功能convertPic()之前。

那麼,這是我一見鍾情。

1

這似乎是你正在處理上傳的圖像。作爲處理功能的一部分,您將上傳的文件從其臨時目錄中移出,然後對其進行一些處理。

當您第二次再次調用該函數時,上傳的文件不再存在於臨時目錄中,事情就會崩潰。

function convertPic(...) { 
    .... 
    move_uploaded_file($_FILES['filename']['tmp_name'], $file_src); 
    .... 
    unlink($file_src); 
    .... 
} 

基本上以convertPic進程,然後在第一次調用刪除「源」,這意味着它不再可用於第二呼叫之後立即。