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     }

 

转载于:https://www.cnblogs.com/aeiou/p/5580967.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值