业务经理

微信扫描以上二维码

028-61286886

技术咨询

怎样防止系统自动把图片旋转90度

迅睿框架 更新 2023-04-14 11:37:00 qq119604

开启图片压缩时怎样防止系统自动把图片旋转90度?

方案

  • 迅睿框架联合创始人 #1 · 2023-03-31 11:15:21
    cms并不会进行旋转图片
  • qq119604 #2 · 2023-03-31 16:56:51
    迅睿框架联合创始人 只有移动端上传手机照片的时候会自动旋转,如果开启压缩的话,缩略图和原图都会旋转,不开启压缩只有缩略图旋转。
  • qq119604 #3 · 2023-03-31 16:59:19
    感觉是手机拍摄角度的exif不一样。
  • qq119604 #4 · 2023-04-14 11:37:00
    问题找到了,就是手机拍摄角度影响了exif信息。修改了一下Image类,用exif扩展把图片旋转回来即可。

    // 图片压缩处理
    public function reduce($imgsrc, $cw) { list($width, $height, $type) = getimagesize($imgsrc); $angle = $this->detect_orientation($imgsrc); if ($angle===90 || $angle===270){ $width = $height; $height = $width; } if ($width > $cw) { $per = $cw / $width;//计算比例 $new_width = floor($width * $per); //压缩后的图片宽 $new_height = floor($height * $per); //压缩后的图片高 switch ($type) { case 1: // gif break; case 2: //header('Content-Type:image/jpeg'); $image_wp = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromjpeg($imgsrc); if($angle) { $image = imagerotate($image, $angle, 0); } imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); //90代表的是质量、压缩图片容量大小 imagejpeg($image_wp, $imgsrc, 100); imagedestroy($image_wp); imagedestroy($image); break; case 3: header('Content-Type:image/png'); $image_wp = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefrompng($imgsrc); if($angle) { $image = imagerotate($image, $angle, 0); } //2.上色 $color=imagecolorallocate($image_wp,255,255,255); //3.设置透明 imagecolortransparent($image_wp,$color); imagefill($image_wp,0,0,$color); imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); //90代表的是质量、压缩图片容量大小 imagejpeg($image_wp, $imgsrc, 100); imagedestroy($image_wp); imagedestroy($image); break; case 18: header('Content-Type:image/webp'); $image_wp = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromwebp($imgsrc); if($angle) { $image = imagerotate($image, $angle, 0); } imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); //90代表的是质量、压缩图片容量大小 imagewebp($image_wp, $imgsrc, 100); imagedestroy($image_wp); imagedestroy($image); break; } } else { CI_DEBUG(@反馈BUG) && log_message('debug', '系统要求宽度>'.$cw.'px才进行压缩处理,当前图片宽度='.$width.',不满足压缩条件:'.$imgsrc); } return; } //检测图片的拍摄角度, 返回需要旋转回正常的角度,需要开启php的exif扩展 public function detect_orientation($imgsrc) { if (function_exists('exif_read_data')) { $ori_type = exif_read_data($imgsrc)['Orientation']; switch ($ori_type) { case 6: return 270; case 3: return 180; case 8: return 90; default: return 0; } } else { return 0; } }

迅睿系统漏洞

AI文章生成器
AI文章生成器(2026-10-27)
修正蒸馏词生成时BUG
百度AI批量检测敏感词
百度AI批量检测敏感词(2026-10-26)
优化长任务超时问题
泛解析站群系统
泛解析站群系统(2026-10-26)
添加子站时设置模板
GEO系统
GEO系统(2026-10-26)
修复抖音图文会话不持久:扩大 Cookie 域、会话 Cookie ...
APP消息推送
APP消息推送(2026-10-26)
增加自动推送任务的删除功能 增加推送内容显示函数(避免内容过长的显...
GeoSeo收录宝
GeoSeo收录宝(2026-10-25)
优化插件日志等小细节 1 优化 爬虫监控月统计超200万数据容易...