zoukankan      html  css  js  c++  java
  • php图片上传旋转压缩方法

    用到php的exif扩展,需要开启exif

    在php.ini文件中去掉exif组件的注释

    extension=php_mbstring.dll  //要放在php_exif.dll前面让它先加载
    extension=php_exif.dll
    
    [exif]
    exif.encode_unicode = ISO-8859-15
    exif.decode_unicode_motorola = UCS-2BE
    exif.decode_unicode_intel    = UCS-2LE
    exif.encode_jis =
    exif.decode_jis_motorola = JIS
    exif.decode_jis_intel    = JIS

     

      1   /**
      2      * Modified Version of cameraUsed, no longer returns date.
      3      */
      4     private function cameraUsed($imagePath)
      5     {
      6         // The default empty return
      7         $return = array(
      8             'make'      => "",
      9             'model'     => "",
     10             'exposure'  => "",
     11             'aperture'  => "",
     12             'iso'       => "",
     13             'Orientation' => 1,
     14         );
     15 
     16         // Check if the variable is set and if the file itself exists before continuing
     17         if ((isset($imagePath)) && (file_exists($imagePath)))
     18         {
     19             // There are 2 arrays which contains the information we are after, so it's easier to state them both
     20             $exif_ifd0 = @read_exif_data($imagePath ,'IFD0' ,0);
     21             $exif_exif = @read_exif_data($imagePath ,'EXIF' ,0);
     22 
     23             // Ensure that we actually got some information
     24             if (($exif_ifd0 !== false) && ($exif_exif !== false))
     25             {
     26                 // Make
     27                 if (@array_key_exists('Make', $exif_ifd0))
     28                 {
     29                     $return['make']     = $exif_ifd0['Make'];
     30                 }
     31 
     32                 // Model
     33                 if (@array_key_exists('Model', $exif_ifd0))
     34                 {
     35                     $return['model']    = $exif_ifd0['Model'];
     36                 }
     37 
     38                 // Exposure
     39                 if (@array_key_exists('ExposureTime', $exif_ifd0))
     40                 {
     41                     $return['exposure'] = $exif_ifd0['ExposureTime'];
     42                 }
     43 
     44                 // Aperture
     45                 if (@array_key_exists('ApertureFNumber', $exif_ifd0['COMPUTED']))
     46                 {
     47                     $return['aperture'] = $exif_ifd0['COMPUTED']['ApertureFNumber'];
     48                 }
     49 
     50                 // ISO
     51                 if (@array_key_exists('ISOSpeedRatings',$exif_exif))
     52                 {
     53                     $return['iso']     = $exif_exif['ISOSpeedRatings'];
     54                 }
     55                 if(@array_key_exists('Orientation',$exif_ifd0)){
     56                     $return['Orientation'] = $exif_ifd0['Orientation'];
     57                 }
     58             }
     59         }
     60 
     61         // Return either an empty array, or the details which we were able to extrapolate.
     62         return $return;
     63     }
     64 
     65     /**
     66      * resize image and don't rotates images have exif info
     67      * @param $pic  eg:$_FILES['file']['tmp_name']
     68      * @param $thumb  image save path
     69      * @param $thumbwidth  
     70      * @param int $quality
     71      */
     72     private function createThumbnail($pic,$thumb,$thumbwidth, $quality = 100)
     73     {
     74 
     75         $im1 = @imagecreatefromjpeg($pic);
     76 
     77         $exif = $this->cameraUsed($pic);
     78         if(($exif['Orientation'] > 1)) {
     79             switch($exif['Orientation']) {
     80                 case 8:
     81                     $im1 = @imagerotate($im1,90,0);
     82                     break;
     83                 case 3:
     84                     $im1 = @imagerotate($im1,180,0);
     85                     break;
     86                 case 6:
     87                     $im1 = @imagerotate($im1,-90,0);
     88                     break;
     89             }
     90         }
     91 
     92         $info = @getimagesize($pic);
     93 
     94         $width = $info[0];
     95 
     96         $w2 = @imagesx($im1);
     97         $h2 = @imagesy($im1);
     98         $w1 = ($thumbwidth <= $width) ? $thumbwidth : $width;
     99 
    100 
    101         $h1 = @floor($h2 * ($w1 / $w2));
    102 
    103         $im2 = @imagecreatetruecolor($w1,$h1);
    104 
    105         @imagecopyresampled ($im2,$im1,0,0,0,0,$w1,$h1,$w2,$h2);
    106         $path = addslashes($thumb);
    107         @imagejpeg($im2,$path,$quality);
    108         @imagedestroy($im1);
    109         @imagedestroy($im2);
    110     }

     

  • 相关阅读:
    HTTP Header 详解
    面试题----网页/应用访问慢突然变慢,如何定位问题
    PHP实现斐波那契数列
    常见的HTTP返回状态值
    通过实例理解单列索引、多列索引以及最左前缀原则
    Btree索引和Hash索引
    遍历和删除文件夹
    面试题之----禁掉cookie的session使用方案
    面试题之----写个函数来解决多线程同时读写一个文件的问题
    heredoc
  • 原文地址:https://www.cnblogs.com/aeiou/p/5580967.html
Copyright © 2011-2022 走看看