Skip to content

Commit

Permalink
Fix an issue calculating the width/height of an image.
Browse files Browse the repository at this point in the history
  • Loading branch information
pixlwave committed Oct 21, 2024
1 parent bd4ecdd commit f66ade1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
16 changes: 14 additions & 2 deletions ElementX/Sources/Services/Media/MediaUploadingPreprocessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,22 @@ struct MediaUploadingPreprocessor {
private extension CGImageSource {
var size: CGSize? {
guard let properties = CGImageSourceCopyPropertiesAtIndex(self, 0, nil) as? [NSString: Any],
let width = properties[kCGImagePropertyPixelWidth] as? Int,
let height = properties[kCGImagePropertyPixelHeight] as? Int else {
var width = properties[kCGImagePropertyPixelWidth] as? Int,
var height = properties[kCGImagePropertyPixelHeight] as? Int else {
return nil
}

// Make sure the width and height are the correct way around if an orientation is set.
if let orientationValue = properties[kCGImagePropertyOrientation] as? UInt32,
let orientation = CGImagePropertyOrientation(rawValue: orientationValue) {
switch orientation {
case .up, .down, .upMirrored, .downMirrored:
break
case .left, .right, .leftMirrored, .rightMirrored:
swap(&width, &height)
}
}

return CGSize(width: width, height: height)
}
}
Expand Down
3 changes: 3 additions & 0 deletions UnitTests/Resources/Media/test_rotated_image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions UnitTests/Sources/MediaUploadingPreprocessorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,46 @@ final class MediaUploadingPreprocessorTests: XCTestCase {
XCTAssertEqual(optimizedImageInfo.height, 498)
}

func testRotatedImageProcessing() async {
guard let url = Bundle(for: Self.self).url(forResource: "test_rotated_image.jpg", withExtension: nil) else {
XCTFail("Failed retrieving test asset")

Check failure on line 427 in UnitTests/Sources/MediaUploadingPreprocessorTests.swift

View workflow job for this annotation

GitHub Actions / Tests

testRotatedImageProcessing, failed - Failed retrieving test asset

Check failure on line 427 in UnitTests/Sources/MediaUploadingPreprocessorTests.swift

View workflow job for this annotation

GitHub Actions / Tests

testRotatedImageProcessing, failed - Failed retrieving test asset

Check failure on line 427 in UnitTests/Sources/MediaUploadingPreprocessorTests.swift

View workflow job for this annotation

GitHub Actions / Tests

testRotatedImageProcessing, failed - Failed retrieving test asset

Check failure on line 427 in UnitTests/Sources/MediaUploadingPreprocessorTests.swift

View workflow job for this annotation

GitHub Actions / Tests

testRotatedImageProcessing, failed - Failed retrieving test asset
return
}

guard case let .success(result) = await mediaUploadingPreprocessor.processMedia(at: url),
case let .image(convertedImageURL, thumbnailURL, imageInfo) = result else {
XCTFail("Failed processing asset")
return
}

compare(originalImageAt: url, toConvertedImageAt: convertedImageURL, withThumbnailAt: thumbnailURL)

// Check resulting image info
XCTAssertEqual(imageInfo.mimetype, "image/jpeg")
XCTAssertEqual(imageInfo.width, 2848)
XCTAssertEqual(imageInfo.height, 4272)

XCTAssertNotNil(imageInfo.thumbnailInfo)
XCTAssertEqual(imageInfo.thumbnailInfo?.width, 533)
XCTAssertEqual(imageInfo.thumbnailInfo?.height, 800)

// Repeat with optimised media setting
appSettings.optimizeMediaUploads = true

guard case let .success(optimizedResult) = await mediaUploadingPreprocessor.processMedia(at: url),
case let .image(optimizedImageURL, thumbnailURL, optimizedImageInfo) = optimizedResult else {
XCTFail("Failed processing asset")
return
}

compare(originalImageAt: url, toConvertedImageAt: optimizedImageURL, withThumbnailAt: thumbnailURL)

// Check optimised image info
XCTAssertEqual(optimizedImageInfo.mimetype, "image/jpeg")
XCTAssertEqual(optimizedImageInfo.width, 1365)
XCTAssertEqual(optimizedImageInfo.height, 2048)
}

// MARK: - Private

private func compare(originalImageAt originalImageURL: URL, toConvertedImageAt convertedImageURL: URL, withThumbnailAt thumbnailURL: URL) {
Expand Down

0 comments on commit f66ade1

Please sign in to comment.