迅睿CMS框架是一款PHP8高性能·簡單易用的CMS開源開發(fā)框架,基于MIT開源許可協(xié)議發(fā)布,免費且不限制商業(yè)使用,是免費開源的產(chǎn)品,以萬端互聯(lián)為設計理念,支持的微信公眾號、小程序、APP客戶端、移動端網(wǎng)站、PC網(wǎng)站等多終端式管理系統(tǒng)。
聯(lián)系官方銷售客服
1835022288
028-61286886
開啟圖片壓縮時怎樣防止系統(tǒng)自動把圖片旋轉90度?
回復@迅??蚣苈?lián)合創(chuàng)始人 只有移動端上傳手機照片的時候會自動旋轉,如果開啟壓縮的話,縮略圖和原圖都會旋轉,不開啟壓縮只有縮略圖旋轉。
感覺是手機拍攝角度的exif不一樣。
問題找到了,就是手機拍攝角度影響了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);
case 3:
header('Content-Type:image/png');
$image = imagecreatefrompng($imgsrc);
//2.上色
$color=imagecolorallocate($image_wp,255,255,255);
//3.設置透明
imagecolortransparent($image_wp,$color);
imagefill($image_wp,0,0,$color);
case 18:
header('Content-Type:image/webp');
$image = imagecreatefromwebp($imgsrc);
imagewebp($image_wp, $imgsrc, 100);
} else {
CI_DEBUG(@反饋BUG) && log_message('debug', '系統(tǒng)要求寬度>'.$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;
return 180;
case 8:
return 90;
default:
return 0;
回復@迅??蚣苈?lián)合創(chuàng)始人 只有移動端上傳手機照片的時候會自動旋轉,如果開啟壓縮的話,縮略圖和原圖都會旋轉,不開啟壓縮只有縮略圖旋轉。
感覺是手機拍攝角度的exif不一樣。
問題找到了,就是手機拍攝角度影響了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', '系統(tǒng)要求寬度>'.$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;
}
}