outl1ne / nova-media-hub

This Laravel Nova package allows you to manage media and media fields.
MIT License
45 stars 21 forks source link

The output conversion file name does not reflect the conversion format #72

Open silviaho opened 7 months ago

silviaho commented 7 months ago

Hi, I'm having trouble converting the image format.

I have a conversion that converts images to WebP format. Here is my config file:

    'image_conversions' => [
        '*' => [
            'thumbnail' => [
                'format' => Manipulations::FORMAT_WEBP,
                'width' => 150,
                'height' => 150,
                'fit' => Manipulations::FIT_CROP,
            ],
        ],
    ],

However, when I input an image named sample.png, the resulting conversion file is named sample.thumbnail.png instead of sample.thumbnail.webp in the conversions directory. Despite the file extension being PNG, I have confirmed that the binary content of the converted image is indeed in WebP format.

Then I looked into the source code and found that the extension for the image conversion is always taken from the original media's extension, resulting in the incorrect file extension. image

Is this the expected behavior? If not, I think the correct way to get conversion file names should be something like the following:

    public function getConversionFileName(Media $media, $conversionName)
    {
        [$fileName, $extension] = FileHelpers::splitNameAndExtension($media->file_name);

        $conversion = (MediaHub::getConversionForMedia($media))[$conversionName];
        $extension = isset($conversion['format']) ? $conversion['format'] : $extension;

        $fileNamer = MediaHub::getFileNamer();
        return $fileNamer->formatConversionFileName($fileName, $extension, $conversionName);
    }

Let me know what you think and looking forward to hearing from you!

Versions: PHP 8.3 Nova 4.32.9 Nova Media Hub: 1.7.1

paulbeames commented 5 months ago

Same problem here. Would like to use the conversion to include webp but the file does not get renamed.