2017-04-20 217 views
0

如何使用PHP降低圖像的質量?在php中降低圖像質量

upload_mode = @$this->setting->upload_mode?:'file'; 
      $upload_path = @$this->setting->upload_path?:'uploads/'; 

      $file    = Request::file($name); 
      $fm     = array(); 
      $fm['name']   = $_FILES[$name]['name'];     
      $fm['ext']   = $file->getClientOriginalExtension(); 
      $fm['size']   = $_FILES[$name]['size']; 
      $fm['content_type'] = $_FILES[$name]['type']; 

      if($upload_mode=='database') { 
       $fm['filedata']  = file_get_contents($_FILES[$name]['tmp_name']); 
       DB::table('cms_filemanager')->insert($fm); 
       $id_fm    = DB::getPdo()->lastInsertId(); 
       DB::table('cms_filemanager')->where('id',$id_fm)->update(['id_md5' =>md5($id_fm)]); 
       $filename   = 'upload_virtual/files/'.md5($id_fm).'.'.$fm['ext']; 
      }else{ 
       if(!file_exists($upload_path.date('Y-m'))) { 
        if(!mkdir($upload_path.date('Y-m'),0777)) { 
         die('Gagal buat folder '.$upload_path.date('Y-m')); 
        } 
       } 
       $filename = md5(str_random(12)).'.'.$fm['ext']; 
       $file->move($upload_path.date('Y-m'),$filename);       
       $filename = $upload_path.date('Y-m').'/'.$filename; 
      } 

      $url    = asset($filename); 

已經有人幫我嗎?我需要添加什麼才能使它像我需要的那樣工作?

+0

你想調整它,或者只是降低質量?否則'imagejpeg()'將是一個很好的解決方案:http://php.net/manual/en/function.imagejpeg.php – Thoby

回答

1

GD是一個開源代碼庫,用於程序員動態創建圖像 。 GD是用C編寫的,「包裝器」可用於Perl,PHP和其他語言的 。 GD創建PNG,JPEG和GIF圖像, 等格式。 GD通常用於生成圖表,圖形, 縮略圖,以及大多數其他內容。

代碼以減小文件大小的圖像:

<?php 
    function compress($source, $destination, $quality) { 

     $info = getimagesize($source); 

     if ($info['mime'] == 'image/jpeg') 
      $image = imagecreatefromjpeg($source); 

     elseif ($info['mime'] == 'image/gif') 
      $image = imagecreatefromgif($source); 

     elseif ($info['mime'] == 'image/png') 
      $image = imagecreatefrompng($source); 

     imagejpeg($image, $destination, $quality); 

     return $destination; 
    } 

    $source_img = 'source.jpg'; 
    $destination_img = 'destination .jpg'; 

    $d = compress($source_img, $destination_img, 90); 
?> 
$d = compress($source_img, $destination_img, 90); 

Reference

相關問題