簡體   English   中英

PHP GD圖像裁剪功能

[英]PHP GD Image Cropping Function

我使用這段代碼來制作上傳圖像的縮略圖。

   $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,,)

我認為問題在於臨時圖像存儲,但不確定。 請幫忙。

看來您正在處理上傳的圖片。 作為處理功能的一部分,您可以將上傳的文件從其臨時目錄中移出,然后對其進行一些處理。

當您第二次再次調用該函數時,上載的文件不再位於臨時目錄中,並且一切都變了。

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

基本上,第一次調用convertPic會先處理然后刪除“源”,這意味着此后第二次調用將不再可用。

我想問題在於unlink($ file_src)...

首先,您調用convertPic()函數,由於img.jpg仍在此處,因此它可以正常工作,然后使用unlink()刪除圖像,然后嘗試再次調用實際上需要此文件正常工作的例程。

同樣,您不能兩次移動同一文件,因此必須將其移動到函數外部,根據需要執行多次操作,然后取消鏈接圖像。 取消鏈接應在兩次調用convertPic() ,而move_uploaded_file()應在函數convertPic()之前。

好吧,這就是我的一見鍾情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM