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     }

 

posted @ 2016-06-13 16:00  后土·K  阅读(653)  评论(0编辑  收藏  举报