spatie / laravel-medialibrary

Associate files with Eloquent models
https://spatie.be/docs/laravel-medialibrary
MIT License
5.78k stars 1.08k forks source link

[11.x] Watermark functionality regression with upgrade of `spatie/image` to v3 #3459

Closed kduma closed 11 months ago

kduma commented 11 months ago

Hello,

Due to removal of watermarkOpacity, watermarkPadding, watermarkHeight and watermarkWidth methods in spatie/image:v3.0.0, there is an inability to easily upgrade conversion such as following one:

$this->addMediaConversion()
    ->watermark(public_path('logo.png'))
    ->watermarkPosition(Manipulations::POSITION_BOTTOM_RIGHT)
    ->watermarkPadding(10)
    ->watermarkHeight(110)

While watermarkPosition(Manipulations::POSITION_BOTTOM_RIGHT) method translates to position: AlignPosition::BottomRight, watermarkPadding(10) translates to x: 10, y: 10, there is no easy way to translate watermarkHeight(110) into something else. According to spatie/image docs on watermark, the correct way to apply additional modifications (like resizing) to watermark, is to load and modify the watermark file using Image class, like that:

$this->addMediaConversion()
    ->insert( 
        otherImage: Image::load(public_path('logo.png'))->height(110),
        position: AlignPosition::BottomRight,
       x: 10,
       y: 10,
    )

Besides that it is not working (as it throws an error Typed property Spatie\Image\Drivers\Imagick\ImagickDriver::$image must not be accessed before initialization), it is bad from the optimisation standpoint as the registerMediaConversions method is called every time a conversion is loaded.

The best solution I could get is to prepare a logo file for each watermark size, and use prepared files. Is there another way to do that?

ariaieboy commented 11 months ago

Another problem is that with watermarkWidth or watermarkHeight we could pass a percentage for Example: 20% which translates to having a watermark with a width equal to 20% of the base image.

That is super useful but I didn't find any solution like it in the V11

freekmurze commented 11 months ago

We'll handle this further in #218