一些浏览器智能翻转图片,php判断并复制(覆盖)带有Orientation图片翻转信息的JPEG图片

/**
* 处理带有Orientation图片翻转信息的JPEG图片
* param $imagePath 图片资源路径
* param $dscPath 目标路径
* 照片中EXIF Orientation 参数让你随便照像但都可以看到正确方向的照片而无需手动旋转(前提要图片浏览器支持,Windows 自带的不支持)
*
* */
public static function delImgOrientation($imagePath, $dscPath = null)
{
/* exif_imagetype($imagePath)返回2为JPGE,为可能数码相机拍摄的照片
,可能包含Orientation信息, 先判断图片资源存在且为JPEG*/
if (!file_exists($imagePath) || exif_imagetype($imagePath) != 2) {
return false;
}
//$exifInfo['Orientation']值1为不旋转 3为旋转180度 6为顺时针90度 8为逆时针90度
$exifInfo = @read_exif_data($imagePath, 'EXIF', 0);//获取图片的exif信息
if ($exifInfo && in_array($exifInfo['Orientation'], array(3, 6, 8))) { //如果图片Orientation翻转,拷贝图像
$size = getimagesize($imagePath);
$weight = $size[0];
$height = $size[1];
$dstImg = @imagecreatetruecolor($weight, $height);//创建目标图像
$srcImg = @imagecreatefromjpeg($imagePath);//读取源图像
imagecopy($dstImg, $srcImg, 0, 0, 0, 0, $weight, $height);//复制图像
$dscPath = isset($dscPath) ? $dscPath : $imagePath;//如未设置目标图片路径覆盖原图片
imagejpeg($dstImg, $dscPath);//输出图片(覆盖原图片)
imagedestroy($dstImg);//释放图像内存
imagedestroy($srcImg);
}
}

 

//转载请注明出处

posted @ 2017-01-20 11:28  太菜  阅读(1101)  评论(0编辑  收藏  举报