2015-04-04 62 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