PHP代码:
$file = "../root/$fname.png";
if (exif_imagetype($file) !== IMAGETYPE_JPEG)
return;
$image = imagecreatefromstring(file_get_contents($file));
$exif = exif_read_data($file); //注意先在php.ini中开启exif扩展
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
case 8:
case 7:
$image = imagerotate($image,90,0);
break;
case 3:
case 4:
$image = imagerotate($image,180,0);
break;
case 6:
case 5:
$image = imagerotate($image,-90,0);
break;
}
}
$isSuccess = imagejpeg($image,$file);
if (!$isSuccess) {
echo json_encode(["status"=>"fail","msg"=>"图片存储错误!"]);
return;
}
开启PHP exif扩展:
-
在php.ini文件中找到
;extension=php_exif.dll
,去掉前面的分号 -
在php.ini文件中找到
;extension=php_mbstring.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
评论