php iphone/IOS6上传旋转问题:什么是保存旋转图像的最佳方法 [英] php iphone/IOS6 upload rotation issue: what is best way to save rotated image
问题描述
使用带有IOS6的safari移动浏览器,文件上传功能使用户可以选择拍摄照片.不幸的是,在捕捉照片后,当照片拇指在浏览器中正确显示时,当您上传到服务器时,文件被旋转了90度.这似乎是由于iPhone设置的exif数据造成的.我有一些代码,可以在投放时通过旋转图像来固定方向.但是,我怀疑最好保存旋转的,正确定向的图像,这样我就不必再担心定向了.我的许多其他照片甚至都没有exif数据,如果可以避免的话,我也不想弄乱它.
Using the safari mobile browser with IOS6, the file upload function gives users the option to snap a photo. Unfortunately, upon snapping the photo, while the photo thumb shows up properly in the browser, when you upload to a server, the file is rotated 90 degrees. This appears to be due to the exif data that the iphone sets. I have code that fixes the orientation by rotating the image when serving. However, I suspect it would be better to save the rotated, properly oriented, image so I no longer have to worry about orientation. Many of my other photos do not even have exif data and i don't want to mess with it if I can avoid it.
任何人都可以建议使用代码来保存图像以使其正确定向吗?
Can anyone suggest code to save the image so it is properly oriented?
这是旋转图像的代码.以下代码将显示正确定向的图像,但是,我要做的就是保存它,这样我就可以随时随地使用它,而不必担心定向.
Here is the code that rotates the image. The following code will display the properly oriented image, however, what I want to do is save it so I can then serve it whenever I want without worrying about orientation.
我还要替换下面的代码中的impagejpeg
调用,以便任何代码均可用于gif和jpg.
Also I would like to replace impagejpeg
call in code below so that any code works for gifs as well as jpgs.
感谢您的建议/代码!
PHP
//Here is sample image after uploaded to server and moved to a directory
$target = "pics/779_pic.jpg";
$source = imagecreatefromstring(file_get_contents($target));
$exif = exif_read_data($target);
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
case 8:
$image = imagerotate($source,90,0);
//echo 'It is 8';
break;
case 3:
$image = imagerotate($source,180,0);
//echo 'It is 3';
break;
case 6:
$image = imagerotate($source,-90,0);
//echo 'It is 6';
break;
}
}
// $image now contains a resource with the image oriented correctly
//This is where I want to save resource properly oriented instead of display.
header('Content-type: image/jpg');
imagejpeg($image);
?>
推荐答案
要保存图像,只需使用相同的函数imagejpeg
和下一个参数来保存图像,例如:
To save your image just use the same function imagejpeg
and the next parameter to save the image, something like:
imagejpeg($image, $target, 100);
在这种情况下,您不需要指定标题,因为您什么也不显示.
In this case you don't need the specify the header, because you are not showing nothing.
参考: http://sg3.php.net/manual/en/function.imagejpeg.php
这篇关于php iphone/IOS6上传旋转问题:什么是保存旋转图像的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!