2015-04-04 68 views
1

我有一個功能,上傳文件高達8MB,但現在我也想壓縮或至少rescale更大的圖像,所以我的輸出圖像將不會超過100-200 KB和1000x1000px分辨率。如何在我的函數中實現壓縮和重新調整(比例)?壓縮和RESCALE上傳的圖像

function uploadFile($file, $file_restrictions = '', $user_id, $sub_folder = '') { 
    global $path_app; 
    $new_file_name = generateRandomString(20); 

    if($sub_folder != '') { 
     if(!file_exists('media/'.$user_id.'/'.$sub_folder.'/')) { 
      mkdir('media/'.$user_id.'/'.$sub_folder, 0777); 
     } 
     $sub_folder = $sub_folder.'/'; 
    } 
    else { 
     $sub_folder = ''; 
    } 

    $uploadDir = 'media/'.$user_id.'/'.$sub_folder; 
    $uploadDirO = 'media/'.$user_id.'/'.$sub_folder; 
    $finalDir = $path_app.'/media/'.$user_id.'/'.$sub_folder; 

    $fileExt = explode(".", basename($file['name'])); 
    $uploadExt = $fileExt[count($fileExt) - 1]; 
    $uploadName = $new_file_name.'_cache.'.$uploadExt; 
    $uploadDir = $uploadDir.$uploadName; 

    $restriction_ok = true; 
    if(!empty($file_restrictions)) { 
     if(strpos($file_restrictions, $uploadExt) === false) { 
      $restriction_ok = false; 
     } 
    } 
    if($restriction_ok == false) { 
     return ''; 
    } 
    else { 
     if(move_uploaded_file($file['tmp_name'], $uploadDir)) { 
      $image_info  = getimagesize($uploadDir); 
      $image_width = $image_info[0]; 
      $image_height = $image_info[1]; 

      if($file['size'] > 8000000) { 
       unlink($uploadDir); 
       return ''; 
      } 
      else { 
       $finalUploadName = $new_file_name.'.'.$uploadExt; 
       rename($uploadDirO.$uploadName, $uploadDirO.$finalUploadName); 

       return $finalDir.$finalUploadName; 
      } 
     } 
     else { 
      return ''; 
     } 
    } 
} 

回答

1

對於縮放我用這樣的功能:

function dimensions($width,$height,$maxWidth,$maxHeight) 
// given maximum dimensions this tries to fill that as best as possible 
{ 
    // get new sizes 
    if ($width > $maxWidth) { 
    $height = Round($maxWidth*$height/$width); 
    $width = $maxWidth; 
    } 
    if ($height > $maxHeight) { 
    $width = Round($maxHeight*$width/$height); 
    $height = $maxHeight; 
    } 
    // return array with new size 
    return array('width' => $width,'height' => $height); 
} 

壓縮是由PHP函數來完成:

// set limits 
    $maxWidth = 1000; 
    $maxHeight = 1000; 
    // read source 
    $source = imagecreatefromjpeg($originalImageFile); 
    // get the possible dimensions of destination and extract 
    $dims = dimensions(imagesx($source),imagesy($source),$maxWidth,$maxHeight); 
    // prepare destination 
    $dest = imagecreatetruecolor($dims['width'],$dims['height']); 
    // copy in high-quality 
    imagecopyresampled($dest,$source,0,0,0,0, 
        $width,$height,imagesx($source),imagesy($source)); 
    // save file 
    imagejpeg($dest,$destinationImageFile,85); 
    // clear both copies from memory 
    imagedestroy($source); 
    imagedestroy($dest); 

你將不得不提供$originalImageFile$destinationImageFile。這些東西來自我使用的類,所以我編輯了很多,但基本的功能在那裏。我遺漏了任何錯誤檢查,所以你仍然需要添加。請注意,imagejpeg()中的85表示壓縮量。

0

就以imagecopyresampled()一看,還有一個例子是如何實現它,對於壓縮採取imagejpeg()看看第三個參數有助於設置圖像的質量,100單元(質量最好,最大的文件),如果你跳過最後一個選項,那麼默認質量是75,這是好的,並壓縮它。

1

你可以使用一個簡單的在線解決方案通過imagemagic library命令會喜歡這個

$image="path to image"; 
$res="option to resize"; i.e 25% small , 50% small or anything else 
exec("convert ".$image." -resize ".$res." ".$image); 

有了這個,你可以旋轉調整和許多其他映像定製

+0

好的解決辦法,但只有我有Web服務器,沒有根或vroot - 但仍然謝謝! – Maxi 2015-04-04 09:33:57

+0

好的沒問題。你已經找到你的答案,那很重要。它可以幫助其他人 – MKD 2015-04-04 09:55:29