Php 图像在移动设备上正确旋转,而不是在桌面上
我上传的图片有一个奇怪的问题。当我在iPhone和iPad上查看它们时,它们的旋转方向是正确的,但每当我试图在桌面上查看它们时,它们的显示方向是错误的。 我找不到错误,在花了几个小时处理EXIF数据之后,我几乎要放弃了。 在确定了方向之后,我还调整了图像的大小,但这不会干扰其他代码。如果有,我会把它包括在内 我没有足够的声誉上传图片,但这里有一个链接:Php 图像在移动设备上正确旋转,而不是在桌面上,php,iphone,image,image-processing,Php,Iphone,Image,Image Processing,我上传的图片有一个奇怪的问题。当我在iPhone和iPad上查看它们时,它们的旋转方向是正确的,但每当我试图在桌面上查看它们时,它们的显示方向是错误的。 我找不到错误,在花了几个小时处理EXIF数据之后,我几乎要放弃了。 在确定了方向之后,我还调整了图像的大小,但这不会干扰其他代码。如果有,我会把它包括在内 我没有足够的声誉上传图片,但这里有一个链接: 以下是我用来上传的代码: $path_parts = pathinfo($_FILES["file"]["name"]); $filepat
以下是我用来上传的代码:
$path_parts = pathinfo($_FILES["file"]["name"]);
$filepath = $_FILES['file']['tmp_name'];
$image = imagecreatefromstring(file_get_contents($filepath));
// Rotate image correctly!
$exif = exif_read_data($image);
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']){
case 1: // nothing
break;
case 2: // horizontal flip
$image = imageflip($image, IMG_FLIP_HORIZONTAL);
break;
case 3: // 180 rotate left
$image = imagerotate($image,180,0);
break;
case 4: // vertical flip
$image = imageflip($image, IMG_FLIP_VERTICAL);
break;
case 5: // vertical flip + 90 rotate right
$image = imageflip($image, IMG_FLIP_VERTICAL);
$image = imagerotate($image,-90,0);
break;
case 6: // 90 rotate right
$image = imagerotate($image,-90,0);
break;
case 7: // horizontal flip + 90 rotate right
$image = imageflip($image, IMG_FLIP_HORIZONTAL);
$image = imagerotate($image,-90,0);
break;
case 8: // 90 rotate left
$image = imagerotate($image,90,0);
break;
}
}
switch ($path_parts['extension']) {
case 'gif' :
$im = imagecreatefromgif($image);
break;
case 'jpg' :
$im = imagecreatefromjpeg($image);
break;
case 'png' :
$im = imagecreatefrompng($image);
break;
case 'bmp' :
$im = imagecreatefrombmp($image);
break;
}
if($im){
imagejpeg($im, $_FILES['file']['tmp_name'], 40);
}
$image_path = 'd_'.time() . "." . $path_parts['extension'];
$move_result = move_uploaded_file($_FILES['file']['tmp_name'], '../img/results/' . $image_path);
如果你知道为什么它只能在某些平台上正确旋转,我将非常感激
编辑:可能需要澄清的是,图像通常是从智能手机或平板电脑上传的。有一些错误导致代码无法正常工作。尝试打开以帮助您调试此类问题
作用于文件,而不是GD资源,因此传递exif\u read\u data()
而不是$filepath
$image
直接操作资源并返回imageflip()
,因此将返回值赋给bool
会破坏资源$image
- 第二个
语句根本不需要。switch()
imagecreatefrom_u_;()
$path_parts = pathinfo($_FILES["file"]["name"]);
$filepath = $_FILES['file']['tmp_name'];
$image = imagecreatefromstring(file_get_contents($filepath));
// Rotate image correctly!
$exif = exif_read_data($filepath);
if (!empty($exif['Orientation'])) {
switch ($exif['Orientation']) {
case 1: // nothing
break;
case 2: // horizontal flip
imageflip($image, IMG_FLIP_HORIZONTAL);
break;
case 3: // 180 rotate left
$image = imagerotate($image, 180, 0);
break;
case 4: // vertical flip
imageflip($image, IMG_FLIP_VERTICAL);
break;
case 5: // vertical flip + 90 rotate right
imageflip($image, IMG_FLIP_VERTICAL);
$image = imagerotate($image, -90, 0);
break;
case 6: // 90 rotate right
$image = imagerotate($image, -90, 0);
break;
case 7: // horizontal flip + 90 rotate right
imageflip($image, IMG_FLIP_HORIZONTAL);
$image = imagerotate($image, -90, 0);
break;
case 8: // 90 rotate left
$image = imagerotate($image, 90, 0);
break;
}
}
imagejpeg($image, $_FILES['file']['tmp_name'], 40);
$image_path = 'd_'.time() . "." . $path_parts['extension'];
$move_result = move_uploaded_file($_FILES['file']['tmp_name'], '../img/results/' . $image_path);
可能与您所述的问题没有直接关系,但
imageflip()
返回bool
,而不是资源,因此您不应该将其分配给$image
,然后尝试imagerotate()
。哦。正确的。谢谢你,蒂姆!我应该使用哪种方法翻转图像?只需使用imageflip()
,而不指定返回值。它直接操纵资源。