上传PHP后修复iOS图片方向

有没有一种方式来旋转图片只有当从iOS上传后没有正确定位的PHP?

有些照片旋转了90度,而其他照片则正确上传。

图像旋转是因为您将图像保存为JPEG,如果将图像保存为PNG,则方向不会改变。 这里是解决方向问题的代码。

- (UIImage *)fixrotation:(UIImage *)image{ if (image.imageOrientation == UIImageOrientationUp) return image; CGAffineTransform transform = CGAffineTransformIdentity; switch (image.imageOrientation) { case UIImageOrientationDown: case UIImageOrientationDownMirrored: transform = CGAffineTransformTranslate(transform, image.size.width, image.size.height); transform = CGAffineTransformRotate(transform, M_PI); break; case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: transform = CGAffineTransformTranslate(transform, image.size.width, 0); transform = CGAffineTransformRotate(transform, M_PI_2); break; case UIImageOrientationRight: case UIImageOrientationRightMirrored: transform = CGAffineTransformTranslate(transform, 0, image.size.height); transform = CGAffineTransformRotate(transform, -M_PI_2); break; case UIImageOrientationUp: case UIImageOrientationUpMirrored: break; } switch (image.imageOrientation) { case UIImageOrientationUpMirrored: case UIImageOrientationDownMirrored: transform = CGAffineTransformTranslate(transform, image.size.width, 0); transform = CGAffineTransformScale(transform, -1, 1); break; case UIImageOrientationLeftMirrored: case UIImageOrientationRightMirrored: transform = CGAffineTransformTranslate(transform, image.size.height, 0); transform = CGAffineTransformScale(transform, -1, 1); break; case UIImageOrientationUp: case UIImageOrientationDown: case UIImageOrientationLeft: case UIImageOrientationRight: break; } // Now we draw the underlying CGImage into a new context, applying the transform // calculated above. CGContextRef ctx = CGBitmapContextCreate(NULL, image.size.width, image.size.height, CGImageGetBitsPerComponent(image.CGImage), 0, CGImageGetColorSpace(image.CGImage), CGImageGetBitmapInfo(image.CGImage)); CGContextConcatCTM(ctx, transform); switch (image.imageOrientation) { case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: case UIImageOrientationRight: case UIImageOrientationRightMirrored: // Grr... CGContextDrawImage(ctx, CGRectMake(0,0,image.size.height,image.size.width), image.CGImage); break; default: CGContextDrawImage(ctx, CGRectMake(0,0,image.size.width,image.size.height), image.CGImage); break; } // And now we just create a new UIImage from the drawing context CGImageRef cgimg = CGBitmapContextCreateImage(ctx); UIImage *img = [UIImage imageWithCGImage:cgimg]; CGContextRelease(ctx); CGImageRelease(cgimg); return img; } 

使用exif_read_data来找出图像的方向:

 $exif = exif_read_data('image.jpg'); if (isset($exif['Orientation'])) { switch ($exif['Orientation']) { case 3: // Need to rotate 180 deg break; case 6: // Need to rotate 90 deg clockwise break; case 8: // Need to rotate 90 deg counter clockwise break; } } 

您可以在这里find对方向代码的解释: http : //www.impulseadventure.com/photo/exif-orientation.html

它不完全回答你的问题,但我总是使用这个脚本来修复方向和缩放到一个合理的大小预上传。 这样你节省应用程序用户一些带宽,没有任何东西做服务器端。

 - (UIImage *)scaleAndRotateImage:(UIImage *)image { int kMaxResolution = 1024; // Or whatever CGImageRef imgRef = image.CGImage; CGFloat width = CGImageGetWidth(imgRef); CGFloat height = CGImageGetHeight(imgRef); CGAffineTransform transform = CGAffineTransformIdentity; CGRect bounds = CGRectMake(0, 0, width, height); if (width > kMaxResolution || height > kMaxResolution) { CGFloat ratio = width/height; if (ratio > 1) { bounds.size.width = kMaxResolution; bounds.size.height = roundf(bounds.size.width / ratio); } else { bounds.size.height = kMaxResolution; bounds.size.width = roundf(bounds.size.height * ratio); } } CGFloat scaleRatio = bounds.size.width / width; CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef)); CGFloat boundHeight; UIImageOrientation orient = image.imageOrientation; switch(orient) { case UIImageOrientationUp: //EXIF = 1 transform = CGAffineTransformIdentity; break; case UIImageOrientationUpMirrored: //EXIF = 2 transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0); transform = CGAffineTransformScale(transform, -1.0, 1.0); break; case UIImageOrientationDown: //EXIF = 3 transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height); transform = CGAffineTransformRotate(transform, M_PI); break; case UIImageOrientationDownMirrored: //EXIF = 4 transform = CGAffineTransformMakeTranslation(0.0, imageSize.height); transform = CGAffineTransformScale(transform, 1.0, -1.0); break; case UIImageOrientationLeftMirrored: //EXIF = 5 boundHeight = bounds.size.height; bounds.size.height = bounds.size.width; bounds.size.width = boundHeight; transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width); transform = CGAffineTransformScale(transform, -1.0, 1.0); transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0); break; case UIImageOrientationLeft: //EXIF = 6 boundHeight = bounds.size.height; bounds.size.height = bounds.size.width; bounds.size.width = boundHeight; transform = CGAffineTransformMakeTranslation(0.0, imageSize.width); transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0); break; case UIImageOrientationRightMirrored: //EXIF = 7 boundHeight = bounds.size.height; bounds.size.height = bounds.size.width; bounds.size.width = boundHeight; transform = CGAffineTransformMakeScale(-1.0, 1.0); transform = CGAffineTransformRotate(transform, M_PI / 2.0); break; case UIImageOrientationRight: //EXIF = 8 boundHeight = bounds.size.height; bounds.size.height = bounds.size.width; bounds.size.width = boundHeight; transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0); transform = CGAffineTransformRotate(transform, M_PI / 2.0); break; default: [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"]; } UIGraphicsBeginImageContext(bounds.size); CGContextRef context = UIGraphicsGetCurrentContext(); if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) { CGContextScaleCTM(context, -scaleRatio, scaleRatio); CGContextTranslateCTM(context, -height, 0); } else { CGContextScaleCTM(context, scaleRatio, -scaleRatio); CGContextTranslateCTM(context, 0, -height); } CGContextConcatCTM(context, transform); CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef); UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return imageCopy; } 

我已经创build了一个函数来纠正存档的图像,你可以使用这个小修改空气更正。

主要function链接

 <?php /* Correct image orientation v1.0 Author: Mathuvathanan Mounasamy Licensed under the MIT license This function correct all the images' orientation in a given path or directory. Run: php -r "require 'correctImageOrientation.php'; correctImageOrientation('test/');" or php -r "require 'correctImageOrientation.php'; correctImageOrientation('test/test1');" or php -r "require 'correctImageOrientation.php'; correctImageOrientation('test');" */ function correctImageOrientation($directory) { $scanned_directory = array_diff(scandir($directory), array('..', '.')); echo "<pre>"; print_r("scanned directory: \r\n"); print_r($scanned_directory); echo "</pre>\r\n"; foreach ($scanned_directory as &$file) { if (is_dir($directory."/".$file)) { correctImageOrientation($directory."/".$file); } else { $filen = explode(".", $file); $ext = end($filen); try { $exif = @exif_read_data($directory."/".$file); $orientation = $exif['Orientation']; if (isset($orientation) && $orientation != 1){ switch ($orientation) { case 3: $deg = 180; break; case 6: $deg = 270; break; case 8: $deg = 90; break; } if ($deg) { // If png if ($ext == "png") { $img_new = imagecreatefrompng($directory."/".$file); $img_new = imagerotate($img_new, $deg, 0); // Save rotated image imagepng($img_new,$directory.$file); }else { $img_new = imagecreatefromjpeg($directory."/".$file); $img_new = imagerotate($img_new, $deg, 0); // Save rotated image imagejpeg($img_new,$directory."/".$file,80); } } echo "<pre>"; print_r("image changed: \r\n"); print_r($directory."/".$file); echo "</pre>\r\n"; } } catch (Exception $e) { echo "error:"; echo $e; echo "error"; } } } unset($file); } ?> 

在文件夹中上传图片后,可以添加下面的脚本

  $filename = "/files/1.jpg"; /*ADD YOUR FILENAME WITH PATH*/ $exif = exif_read_data($filename); $ort = $exif['Orientation']; /*STORES ORIENTATION FROM IMAGE */ $ort1 = $ort; $exif = exif_read_data($filename, 0, true); if (!empty($ort1)) { $image = imagecreatefromjpeg($filename); $ort = $ort1; switch ($ort) { case 3: $image = imagerotate($image, 180, 0); break; case 6: $image = imagerotate($image, -90, 0); break; case 8: $image = imagerotate($image, 90, 0); break; } } imagejpeg($image,$filename, 90); /*IF FOUND ORIENTATION THEN ROTATE IMAGE IN PERFECT DIMENSION*/ 

这个脚本在图像上传到文件夹后工作,所以你可以在文件夹中上传文件后添加这个脚本

使用GD库,我们可以在PHP中处理图像。 该脚本使用imagerotate()函数旋转照片。 在我们的例子中,我们将它旋转90度,但是这可以在脚本中进行更改。

  <?php // The file you are rotating $image = 'myfile.jpg'; //How many degrees you wish to rotate $degrees = 90; // This sets the image type to .jpg but can be changed to png or gif header('Content-type: image/jpeg') ; // Create the canvas $source = imagecreatefromjpeg($image) ; // Rotates the image $rotate = imagerotate($source, $degrees, 0) ; // Outputs a jpg image, you could change this to gif or png if needed imagejpeg($rotate) ; ?> 

这个脚本的最重要的部分,旋转,是用imagerotate()函数完成的。 这个function的参数是:

 imagerotate (The_image, degrees_to_rotate, background_color, Optional_ignore_transparency) 

如果可选忽略透明度为空或0,则保留透明度。

 array getimagesize ( string $filename [, array &$imageinfo ] ) 

getimagesize()函数将确定任何给定图像文件的大小,并返回尺寸以及要在常规HTML IMG标记和相应的HTTP内容types中使用的文件types和高度/宽度文本string。

你应该使用这个来检查手机图像的方向,以便知道何时调用imagerotate()。

您也可以用imagejpeg保存图像。 http://www.php.net/manual/en/function.imagejpeg.php

 // Save the image as 'path/to/myfile.jpg' imagejpeg($image, 'path/to/myfile.jpg'); // Free up memory imagedestroy($image); 
 // File and rotation $filename = 'test.jpg'; $degrees = 180; // Content type header('Content-type: image/jpeg'); // Load $source = imagecreatefromjpeg($filename); // Rotate $rotate = imagerotate($source, $degrees, 0); // Output imagejpeg($rotate); check this link : http://www.php.net/manual/en/function.imagerotate.php 

如果您使用的ImageMagick有一个非常简单的修复它,只需将-auto-orient添加到您的命令。

 convert -auto-orient -quality 90 -resize 1200x800 $f-new