Simplify images, tag overview
This commit is contained in:
52
CHDataManagement/Generator/ImageSet.swift
Normal file
52
CHDataManagement/Generator/ImageSet.swift
Normal file
@ -0,0 +1,52 @@
|
||||
import Foundation
|
||||
|
||||
struct ImageSet {
|
||||
|
||||
let image: FileResource
|
||||
|
||||
let maxWidth: Int
|
||||
|
||||
let maxHeight: Int
|
||||
|
||||
let quality: CGFloat
|
||||
|
||||
let description: String
|
||||
|
||||
init(image: FileResource, maxWidth: Int, maxHeight: Int, description: String, quality: CGFloat = 0.7) {
|
||||
self.image = image
|
||||
self.maxWidth = maxWidth
|
||||
self.maxHeight = maxHeight
|
||||
self.description = description
|
||||
self.quality = quality
|
||||
}
|
||||
|
||||
var jobs: [ImageVersion] {
|
||||
let type = image.type
|
||||
|
||||
let width2x = maxWidth * 2
|
||||
let height2x = maxHeight * 2
|
||||
|
||||
return [
|
||||
.init(image: image, type: .avif, maximumWidth: maxWidth, maximumHeight: maxHeight, quality: quality),
|
||||
.init(image: image, type: .avif, maximumWidth: width2x, maximumHeight: height2x, quality: quality),
|
||||
.init(image: image, type: .webp, maximumWidth: maxWidth, maximumHeight: maxHeight, quality: quality),
|
||||
.init(image: image, type: .webp, maximumWidth: width2x, maximumHeight: height2x, quality: quality),
|
||||
.init(image: image, type: type, maximumWidth: maxWidth, maximumHeight: maxHeight, quality: quality),
|
||||
.init(image: image, type: type, maximumWidth: width2x, maximumHeight: height2x, quality: quality)
|
||||
]
|
||||
}
|
||||
|
||||
var content: String {
|
||||
let fileExtension = image.type.fileExtension.map { "." + $0 } ?? ""
|
||||
|
||||
let prefix1x = "/\(image.outputImageFolder)/\(maxWidth)x\(maxHeight)"
|
||||
let prefix2x = "/\(image.outputImageFolder)/\(maxWidth*2)x\(maxHeight*2)"
|
||||
|
||||
var result = "<picture>"
|
||||
result += "<source type='image/avif' srcset='\(prefix1x).avif 1x, \(prefix2x).avif 2x'/>"
|
||||
result += "<source type='image/webp' srcset='\(prefix1x).webm 1x, \(prefix1x).webm 2x'/>"
|
||||
result += "<img srcset='\(prefix2x)\(fileExtension) 2x' src='\(prefix1x)\(fileExtension)' loading='lazy' alt='\(description.htmlEscaped())'/>"
|
||||
result += "</picture>"
|
||||
return result
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user