Skip to content

Commit

Permalink
Optimize target size for panoramas
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Sep 21, 2023
1 parent 70e1e85 commit 8e553ec
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
22 changes: 13 additions & 9 deletions WordPress/Classes/Services/MediaImageService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,21 @@ extension MediaImageService {
guard mediaSize.width > 0 && mediaSize.height > 0 else {
return originalTargetSize
}
let scaleHorizontal = originalTargetSize.width / mediaSize.width
let scaleVertical = originalTargetSize.height / mediaSize.height
// Scale image to fill the target size but avoid upscaling.
let aspectFillScale = min(1, max(scaleHorizontal, scaleVertical))
let targetSize = mediaSize.scaled(by: aspectFillScale).rounded()
// Scale image to fill the target size but avoid upscaling
let scale = min(1, max(
originalTargetSize.width / mediaSize.width,
originalTargetSize.height / mediaSize.height
))
let targetSize = mediaSize.scaled(by: scale).rounded()

// Sanitize the size to make sure ultra-wide panoramas are still resized
// to fit the target size, but increase it a bit for an acceptable size.
if targetSize.width > originalTargetSize.width * 4 ||
targetSize.height > originalTargetSize.height * 4 {
let aspectFitScale = min(scaleHorizontal, scaleVertical)
return mediaSize.scaled(by: aspectFitScale * 4).rounded()
let threshold: CGFloat = 4
if targetSize.width > originalTargetSize.width * threshold || targetSize.height > originalTargetSize.height * threshold {
return CGSize(
width: min(targetSize.width, originalTargetSize.width * threshold),
height: min(targetSize.height, originalTargetSize.height * threshold)
)
}
return targetSize
}
Expand Down
2 changes: 1 addition & 1 deletion WordPress/WordPressTest/MediaImageServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class MediaImageServiceTests: CoreDataTestCase {
forMediaSize: CGSize(width: 4000, height: 400),
targetSize: CGSize(width: 200, height: 200)
),
CGSize(width: 800, height: 80)
CGSize(width: 800, height: 200)
)
}

Expand Down

0 comments on commit 8e553ec

Please sign in to comment.