images.rs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. use const_serialize::SerializeConst;
  2. use crate::AssetOptions;
  3. /// The type of an image. You can read more about the tradeoffs between image formats [here](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types)
  4. #[derive(
  5. Debug,
  6. PartialEq,
  7. PartialOrd,
  8. Clone,
  9. Copy,
  10. Hash,
  11. SerializeConst,
  12. serde::Serialize,
  13. serde::Deserialize,
  14. )]
  15. #[repr(u8)]
  16. pub enum ImageFormat {
  17. /// A png image. Png images cannot contain transparency and tend to compress worse than other formats
  18. Png,
  19. /// A jpg image. Jpg images can contain transparency and tend to compress better than png images
  20. Jpg,
  21. /// A webp image. Webp images can contain transparency and tend to compress better than jpg images
  22. Webp,
  23. /// An avif image. Avif images can compress slightly better than webp images but are not supported by all browsers
  24. Avif,
  25. /// An unknown image type
  26. Unknown,
  27. }
  28. /// The size of an image asset
  29. #[derive(
  30. Debug,
  31. PartialEq,
  32. PartialOrd,
  33. Clone,
  34. Copy,
  35. Hash,
  36. SerializeConst,
  37. serde::Serialize,
  38. serde::Deserialize,
  39. )]
  40. #[repr(C, u8)]
  41. pub enum ImageSize {
  42. /// A manual size in pixels
  43. Manual {
  44. /// The width of the image in pixels
  45. width: u32,
  46. /// The height of the image in pixels
  47. height: u32,
  48. },
  49. /// The size will be automatically determined from the image source
  50. Automatic,
  51. }
  52. /// Options for an image asset
  53. #[derive(
  54. Debug,
  55. PartialEq,
  56. PartialOrd,
  57. Clone,
  58. Copy,
  59. Hash,
  60. SerializeConst,
  61. serde::Serialize,
  62. serde::Deserialize,
  63. )]
  64. pub struct ImageAssetOptions {
  65. ty: ImageFormat,
  66. low_quality_preview: bool,
  67. size: ImageSize,
  68. preload: bool,
  69. }
  70. impl Default for ImageAssetOptions {
  71. fn default() -> Self {
  72. Self::new()
  73. }
  74. }
  75. impl ImageAssetOptions {
  76. /// Create a new image asset options
  77. pub const fn new() -> Self {
  78. Self {
  79. ty: ImageFormat::Unknown,
  80. low_quality_preview: false,
  81. size: ImageSize::Automatic,
  82. preload: false,
  83. }
  84. }
  85. /// Make the asset preloaded
  86. ///
  87. /// Preloading an image will make the image start to load as soon as possible. This is useful for images that will be displayed soon after the page loads or images that may not be visible immediately, but should start loading sooner
  88. ///
  89. /// ```rust
  90. /// # use manganis::{asset, Asset, ImageAssetOptions};
  91. /// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_preload(true));
  92. /// ```
  93. pub const fn with_preload(self, preload: bool) -> Self {
  94. Self { preload, ..self }
  95. }
  96. /// Check if the asset is preloaded
  97. pub const fn preloaded(&self) -> bool {
  98. self.preload
  99. }
  100. /// Sets the format of the image
  101. ///
  102. /// Choosing the right format can make your site load much faster. Webp and avif images tend to be a good default for most images
  103. ///
  104. /// ```rust
  105. /// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat};
  106. /// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_format(ImageFormat::Webp));
  107. /// ```
  108. pub const fn with_format(self, format: ImageFormat) -> Self {
  109. Self { ty: format, ..self }
  110. }
  111. /// Sets the format of the image to [`ImageFormat::Avif`]
  112. ///
  113. /// Avif images tend to be a good default for most images rendered in browser because
  114. /// they compress images well
  115. ///
  116. /// ```rust
  117. /// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat};
  118. /// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_avif());
  119. /// ```
  120. pub const fn with_avif(self) -> Self {
  121. self.with_format(ImageFormat::Avif)
  122. }
  123. /// Sets the format of the image to [`ImageFormat::Webp`]
  124. ///
  125. /// Webp images tend to be a good default for most images rendered in browser because
  126. /// they compress images well
  127. ///
  128. /// ```rust
  129. /// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat};
  130. /// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_webp());
  131. /// ```
  132. pub const fn with_webp(self) -> Self {
  133. self.with_format(ImageFormat::Webp)
  134. }
  135. /// Sets the format of the image to [`ImageFormat::Jpg`]
  136. ///
  137. /// Jpeg images compress much better than [`ImageFormat::Png`], but worse than [`ImageFormat::Webp`] or [`ImageFormat::Avif`]
  138. ///
  139. /// ```rust
  140. /// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat};
  141. /// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_jpg());
  142. /// ```
  143. pub const fn with_jpg(self) -> Self {
  144. self.with_format(ImageFormat::Jpg)
  145. }
  146. /// Sets the format of the image to [`ImageFormat::Png`]
  147. ///
  148. /// Png images don't compress very well, so they are not recommended for large images
  149. ///
  150. /// ```rust
  151. /// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat};
  152. /// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_png());
  153. /// ```
  154. pub const fn with_png(self) -> Self {
  155. self.with_format(ImageFormat::Png)
  156. }
  157. /// Get the format of the image
  158. pub const fn format(&self) -> ImageFormat {
  159. self.ty
  160. }
  161. /// Sets the size of the image
  162. ///
  163. /// If you only use the image in one place, you can set the size of the image to the size it will be displayed at. This will make the image load faster
  164. ///
  165. /// ```rust
  166. /// # use manganis::{asset, Asset, ImageAssetOptions, ImageSize};
  167. /// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_size(ImageSize::Manual { width: 512, height: 512 }));
  168. /// ```
  169. pub const fn with_size(self, size: ImageSize) -> Self {
  170. Self { size, ..self }
  171. }
  172. /// Get the size of the image
  173. pub const fn size(&self) -> ImageSize {
  174. self.size
  175. }
  176. // LQIP is currently disabled until we have the CLI set up to inject the low quality image preview after the crate is built through the linker
  177. // /// Make the image use a low quality preview
  178. // ///
  179. // /// A low quality preview is a small version of the image that will load faster. This is useful for large images on mobile devices that may take longer to load
  180. // ///
  181. // /// ```rust
  182. // /// # use manganis::{asset, Asset, ImageAssetOptions};
  183. // /// const _: Asset = manganis::asset!("/assets/image.png", ImageAssetOptions::new().with_low_quality_image_preview());
  184. // /// ```
  185. //
  186. // pub const fn with_low_quality_image_preview(self, low_quality_preview: bool) -> Self {
  187. // Self {
  188. // low_quality_preview,
  189. // ..self
  190. // }
  191. // }
  192. /// Convert the options into options for a generic asset
  193. pub const fn into_asset_options(self) -> AssetOptions {
  194. AssetOptions::Image(self)
  195. }
  196. pub(crate) const fn extension(&self) -> Option<&'static str> {
  197. match self.ty {
  198. ImageFormat::Png => Some("png"),
  199. ImageFormat::Jpg => Some("jpg"),
  200. ImageFormat::Webp => Some("webp"),
  201. ImageFormat::Avif => Some("avif"),
  202. ImageFormat::Unknown => None,
  203. }
  204. }
  205. }