laravel-image 0.2.0

Image handling and manipulation for Laravel Rust, in pure Rust: a port of intervention/image-laravel and Intervention Image.
Documentation
//! The package's settings: `config/intervention-image.php` and Intervention Image's `Config`.

use laravel::config::Section;

use crate::color::Color;
use crate::error::Result;

/// The settings of `config/intervention-image.php`, with its defaults:
///
/// ```ignore
/// use laravel_image::ImageConfig;
///
/// Application::configure()
///     .with_config(|config, _| {
///         let image = config.section_mut::<ImageConfig>();
///         image.background_color = "000000".to_owned();
///         image.strip = true;
///     })
/// ```
#[derive(Debug, Clone, Section)]
pub struct ImageConfig {
    /// `driver`, from `IMAGE_DRIVER`. PHP chooses between the GD, Imagick and Vips drivers; this
    /// port has a single pure-Rust driver, so every value selects it. The value is kept so
    /// applications can read it, and `gd` is the default as in PHP.
    #[env("IMAGE_DRIVER", default = "gd")]
    pub driver: String,

    /// `options.autoOrientation`: rotate decoded images according to their Exif orientation.
    #[section(default = true)]
    pub auto_orientation: bool,

    /// `options.decodeAnimation`: keep the frames of animated GIFs, or only the first one.
    #[section(default = true)]
    pub decode_animation: bool,

    /// `options.backgroundColor`: the default background and blending color.
    #[section(default = "ffffff".to_owned())]
    pub background_color: String,

    /// `options.strip`: remove metadata such as Exif when encoding.
    #[section(default = false)]
    pub strip: bool,
}

impl ImageConfig {
    /// The driver options of this configuration.
    pub fn options(&self) -> DriverConfig {
        DriverConfig {
            auto_orientation: self.auto_orientation,
            decode_animation: self.decode_animation,
            background_color: self.background_color.clone(),
            strip: self.strip,
        }
    }
}

/// The options of the driver: Intervention Image's `Config` class, with its defaults.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DriverConfig {
    /// `autoOrientation`, `true` by default.
    pub auto_orientation: bool,
    /// `decodeAnimation`, `true` by default.
    pub decode_animation: bool,
    /// `backgroundColor`, `ffffff` by default.
    pub background_color: String,
    /// `strip`, `false` by default.
    pub strip: bool,
}

impl Default for DriverConfig {
    fn default() -> Self {
        Self {
            auto_orientation: true,
            decode_animation: true,
            background_color: "ffffff".to_owned(),
            strip: false,
        }
    }
}

impl DriverConfig {
    pub(crate) fn background(&self) -> Result<Color> {
        Color::parse(&self.background_color)
    }
}

/// The section of the running application, or the defaults outside one.
pub(crate) fn current() -> ImageConfig {
    if laravel::context::Context::current().is_none() {
        return ImageConfig::default();
    }

    laravel::config().get::<ImageConfig>().cloned().unwrap_or_default()
}