sonata-project / SonataMediaBundle

Symfony SonataMediaBundle
https://docs.sonata-project.org/projects/SonataMediaBundle
MIT License
451 stars 495 forks source link

Width parameter is missing in context "default" #1571

Closed franckentien closed 4 years ago

franckentien commented 5 years ago

Environment

Sonata packages

sonata-project/admin-bundle              3.45.2 = 3.45.2 The missing Symfony Admin Generator
sonata-project/block-bundle              3.14.0 = 3.14.0 Symfony SonataBlockBundle
sonata-project/cache                     2.0.1  = 2.0.1  Cache library
sonata-project/classification-bundle     3.8.0  = 3.8.0  Symfony SonataClassificationBundle
sonata-project/core-bundle               3.16.1 = 3.16.1 Symfony SonataCoreBundle
sonata-project/datagrid-bundle           2.4.0  = 2.4.0  Symfony SonataDatagridBundle
sonata-project/doctrine-extensions       1.1.5  = 1.1.5  Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.8.2  = 3.8.2  Symfony Sonata / Integrate Doctrine ORM into the SonataAdm...
sonata-project/easy-extends-bundle       2.5.0  = 2.5.0  Symfony SonataEasyExtendsBundle
sonata-project/exporter                  1.11.0 = 1.11.0 Lightweight Exporter library
sonata-project/formatter-bundle          3.5.0  ~ 4.1.2  Symfony SonataFormatterBundle
sonata-project/intl-bundle               2.5.0  = 2.5.0  Symfony SonataIntlBundle
sonata-project/media-bundle              3.19.1 = 3.19.1 Symfony SonataMediaBundle
sonata-project/news-bundle               3.8.0  = 3.8.0  Symfony SonataNewsBundle
sonata-project/user-bundle               4.3.0  = 4.3.0  Symfony SonataUserBundle

Symfony packages

symfony/monolog-bundle     v3.3.1  = v3.3.1  Symfony MonologBundle
symfony/phpunit-bridge     v3.4.22 ~ v4.2.3  Symfony PHPUnit Bridge
symfony/polyfill-apcu      v1.10.0 = v1.10.0 Symfony polyfill backporting apcu_* functions to lower PHP versions
symfony/polyfill-ctype     v1.10.0 = v1.10.0 Symfony polyfill for ctype functions
symfony/polyfill-intl-icu  v1.10.0 = v1.10.0 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-mbstring  v1.10.0 = v1.10.0 Symfony polyfill for the Mbstring extension
symfony/polyfill-php56     v1.10.0 = v1.10.0 Symfony polyfill backporting some PHP 5.6+ features to lower PHP versions
symfony/polyfill-php70     v1.10.0 = v1.10.0 Symfony polyfill backporting some PHP 7.0+ features to lower PHP versions
symfony/polyfill-util      v1.10.0 = v1.10.0 Symfony utilities for portability of PHP codes
symfony/security-acl       v3.0.1  = v3.0.1  Symfony Security Component - ACL (Access Control List)
symfony/swiftmailer-bundle v2.6.7  ~ v3.2.5  Symfony SwiftmailerBundle
symfony/symfony            v3.4.22 ~ v4.2.3  The Symfony PHP framework

PHP version

PHP 7.1.9 (cli) (built: Aug 30 2017 18:34:46) ( ZTS MSVC14 (Visual C++ 2015) x64 )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies

Subject

I try to add new media picture from media. I try to downgrade sonata-project/media-bundle from 3.19.1 to 3.18.1 and it's work perfectly.

Expected results

Add a new media

Actual results

I got an error and i can import news media:

Uncaught PHP Exception RuntimeException: "Width parameter is missing in context "default" for provider "sonata.media.provider.image"" at E:\wamp64\www\convention-geek.fr\vendor\sonata-project\media-bundle\src\Resizer\SimpleResizer.php line 59
franckentien commented 5 years ago

This is a part of my config.yml:

    default_context: default # you need to set a context
    contexts:
        default:  # the default context is mandatory
            providers:
                #- sonata.media.provider.dailymotion
                - sonata.media.provider.youtube
                - sonata.media.provider.image
                #- sonata.media.provider.file
                #- sonata.media.provider.vimeo

            formats:
                small: { width: 100 , quality: 70}
                cg-small: { height: 200 , quality: 70}
                big:   { width: 500 , quality: 70}
                cg-big: { height: 500 , quality: 70}
danquah commented 5 years ago

I ran in to (possibly?) the same issue, It tracked it down to a series of things colliding, but the necessary fix still escapes me.

Long story short: I can "fix" my local setup by adding a width: false to those formats I don't want to specify a width for (portrait formats). The reason it works is that https://github.com/sonata-project/SonataMediaBundle/blob/a19f79e76f4ec3d156217512d5c9f90c8507ca89/src/Resizer/SimpleResizer.php#L56 checks whether width is set, not whether it is empty, which allows falseto pass.

Now, why did this suddenly break? My best guess is that the cause of the breakages is not going from 3.18.1 to 3.19.1, but an update to symfony. In my case I could track it down to Symfonys autogenerated dependency injection cache. I managed to get the cache from an old version of my site, it had something like this in it (php 5.6, symfony 2.8.42, media-bundle 2.3.4)

       $instance->addFormat('default_landscape', array('width' => 960, 'height' => false, 'quality' => 80, 'format' => 'jpg', 'constraint' => true));
        $instance->addFormat('default_portrait', array('height' => 540, 'width' => false, 'quality' => 80, 'format' => 'jpg', 'constraint' => true));

While my latest version of the site (php 7.3, symfony 3.4.30, media-bundle 3.20.1) Reads like this

        $instance->addFormat('default_landscape', ['width' => 960, 'height' => NULL, 'quality' => 80, 'format' => 'jpg', 'constraint' => true, 'resizer' => false, 'resizer_options' => []]);
        $instance->addFormat('default_portrait', ['height' => 540, 'width' => NULL, 'quality' => 80, 'format' => 'jpg', 'constraint' => true, 'resizer' => false, 'resizer_options' => []]);

Notice the switch from false to NULL

I have absolutely no idea whether the main problem here is

or something else.

danquah commented 5 years ago

One more update - setting the width to false ended up not working in all situations, so a proper fix is probably necessary get this to work.

DorianSternVukotic commented 5 years ago

Ran into the same issue. Cause in my case was that some formats in sonata_media.yml have defined height but not width. It seems that width is mandatory.

In your case: Try changing

 formats:
                 small: { width: 100 , quality: 70}
                 cg-small: { height: 200 , quality: 70}
                 big:   { width: 500 , quality: 70}
                 cg-big: { height: 500 , quality: 70}

To

formats:
                small: { width: 100 , quality: 70}
                cg-small: { width: 200 , quality: 70}
                big:   { width: 500 , quality: 70}
                cg-big: { width: 500 , quality: 70}
garak commented 4 years ago

I've got the same problem.

Suggested solution is not working: the problem is that the check is wrongly done. Code is testing for if (!isset($settings['width'])) that is false also when $settings['width'] = null. It should be if (!array_key_exists('width', $settings))