上传PHP后修复iOS图片方向

前端之家收集整理的这篇文章主要介绍了上传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,0);
            transform = CGAffineTransformRotate(transform,M_PI_2);
            break;

        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform,-M_PI_2);
            break;
        case UIImageOrientationUp:
        case UIImageOrientationUpMirrored:
            break;
    }

    switch (image.imageOrientation) {
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform,0);
            transform = CGAffineTransformScale(transform,-1,1);
            break;

        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform,image.size.height,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,CGImageGetBitsPerComponent(image.CGImage),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,image.size.width),image.CGImage);
            break;

        default:
            CGContextDrawImage(ctx,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;
}
原文链接:https://www.f2er.com/php/131220.html

猜你在找的PHP相关文章