Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spatie\MediaLibrary\Jobs\PerformConversions failed in making conversion #947

Closed
ronydebnath opened this issue Mar 7, 2018 · 17 comments
Closed

Comments

@ronydebnath
Copy link

localhost_horizon_failed_60

I'm using laradock in my production server and Gd and imagick is installed.
I have checked them both with php -i | grep -i gd which returns

gd
GD Support => enabled
GD Version => bundled (2.1.0 compatible)
gd.jpeg_ignore_warning => 1 => 1

and php -i | grep -i imagick returns

/usr/local/etc/php/conf.d/docker-php-ext-imagick.ini,
imagick
imagick module => enabled
imagick module version => 3.4.3
imagick classes => Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator, ImagickKernel
Imagick compiled with ImageMagick version => ImageMagick 6.8.9-9 Q16 x86_64 2017-11-16 http://www.imagemagick.org
Imagick using ImageMagick library version => ImageMagick 6.8.9-9 Q16 x86_64 2017-11-16 http://www.imagemagick.org
imagick.locale_fix => 0 => 0
imagick.progress_monitor => 0 => 0
imagick.skip_version_check => 0 => 0

changing in medialibrary.php

'image_driver' => 'gd',

returns the similar type of error

i tried to used nonQueued() function in model but this time mkdir(): Permission denied error occurred
mkdir permission denied

my model looks like this:

use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\MediaLibrary\Media;
//use Spatie\MediaLibrary\HasMedia\Interfaces\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMediaConversions;

class Tour extends Model implements HasMediaConversions
{
    use softDeletes,
        HasMediaTrait;

    protected $dates = ['deleted_at'];

    protected $fillable = ['location_id','category_id','operator_id','is_published','title','short_description','slug','base_currency','has_insurrance','is_child_allowed','is_food_allowed','is_featured','has_promotion','request_mode','minimum_seat_booking','average_rating','review_count','created_by','updated_by','deleted_by'];

    public function registerMediaConversions( Media $media = null )
    {
        $this->addMediaConversion('thumb')
            ->width(150)
            ->height(120)
            ->performOnCollections('image')
            ->nonQueued();

    }

@freekmurze
Copy link
Member

Could you add a dd statement to see the value of $directoryPath. Set the correct permissions for that directory.

@ronydebnath
Copy link
Author

ronydebnath commented Mar 9, 2018

Thank you for enlighten me. The temporary directory in storage folder has been creating with the root user group which was restricting the package from creating the conversions folder for a specific media.
dd'ing the $directoryPath returns
"/var/www/storage/medialibrary/temp/gqhU9vSpveO4CvvV0hlSgHNxMvHX9Zwn"

and i have solved the situation by adding chown function while making directory in path method of this fantastic package:

if anyone is facing the same situation like me, here is what is did:

public function path(string $pathOrFilename = ''): string
    {
        if (empty($pathOrFilename)) {
            return $this->getFullPath();
        }

        $path = $this->getFullPath().DIRECTORY_SEPARATOR.trim($pathOrFilename, '/');

        $directoryPath = $this->removeFilenameFromPath($path);

        if (! file_exists($directoryPath)) {
            mkdir($directoryPath, 0777, true);
            chown($directoryPath, 1000);
        }

        return $path;
    }

where 1000 is the uid of the user in my docker instance.

it could be nice addition to the medialibrary.php to let user set the user/group name while creating the directory and avoid such circumstances.

@freekmurze
Copy link
Member

Thank you very much for posting your solution 👍

@avanisolanki
Copy link

In which file we need to make this changes? I have raised issue #952.
I think this can help me out.

@ronydebnath
Copy link
Author

i had to modify in vendor/spatie/temporary-directory/src/TemporaryDirectory.php to change the user from 'root' to my docker instance user so that it can create directory inside of the medialibrary directory in laravel storage folder.

If the queue operation doesn't work, then use nonQueued() on conversion
Documentation here:
https://docs.spatie.be/laravel-medialibrary/v6/converting-images/defining-conversions/#queuing-conversions

@sezaiozarslan
Copy link

php artisan media:regenerate
1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%All done, but with some error messages:
Media id 2: mkdir(): Permission denied
All done!

sudo chmod 777 -R storage/
php artisan media:regenerate
2/2 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%All done, but with some error messages:
Media id 5: fopen(/home/.../public/media/5/conversions/20171203_162520-big.jpg): failed to open stream: Permission denied
Media id 6: fopen(/home/.../public/media/6/conversions/20171203_162520-big.jpg): failed to open stream: Permission denied
All done!

sudo chmod 777 -R public/media/
php artisan media:regenerate
2/2 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%All done!

It works :) But when I upload a new photo :

php artisan media:regenerate
3/3 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%All done, but with some error messages:
Media id 7: fopen(/home/.../public/media/7/conversions/20171203_162520-big.jpg): failed to open stream: Permission denied
All done!

Because new variation has "-rwxrwxrwx" permission.

Default variation permission must be "777"

@avanisolanki
Copy link

I followed the steps written here http://laravel-recipes.com/recipes/25/creating-an-apache-virtualhost to run laravel app using virtual host.

I changed user in envvars file with my system user. So It is working properly!

@Nova-Corp
Copy link

I followed the steps written here http://laravel-recipes.com/recipes/25/creating-an-apache-virtualhost to run laravel app using virtual host.

I changed user in envvars file with my system user. So It is working properly!

Given link was removed. can you please give me the solution?

@ronydebnath
Copy link
Author

I followed the steps written here http://laravel-recipes.com/recipes/25/creating-an-apache-virtualhost to run laravel app using virtual host.
I changed user in envvars file with my system user. So It is working properly!

Given link was removed. can you please give me the solution?

If you are in a Linux environment, you can follow the instruction from here:

https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-16-04

or if you are on windows and are using xampp, the process would be similar to this:

https://www.cloudways.com/blog/configure-virtual-host-on-windows-10-for-wordpress/

@Nova-Corp
Copy link

My actual problem is queued() conversion not working due to permission issue. So, currently i'm using nonQueued(). I tried @ronydebnath solution. but doesn't work.

@ronydebnath
Copy link
Author

Queue and virtualhost are two different things. For queued() function to work, you need Redis or beanstalk properly installed on your system and configured with your project. The scenario will differ based on project architecture.

@Nova-Corp
Copy link

Nova-Corp commented Dec 6, 2020

i'm using database queue with superuser for mail dispatching queue working fine. but, job failed and following error occurred League\Flysystem\Exception: Impossible to create the root directory.

my function is:

public function registerMediaConversions(Media $media = null)
    {
        $this->addMediaConversion('thumb_400')
            ->width(400)
            ->height(400)
            ->performOnCollections('books_image')
            ->nonQueued(); // without queue working fine.
         // ->queued(); // Above error occurred.
    }

any solution?

@ivnlabs
Copy link

ivnlabs commented Apr 20, 2021

after DAYS trying to make it finally your server must have this installed on Linux Ubuntu nginx

sudo apt-get install php7.4-gd

test workig the GD with = php -i | grep -i gd

then restart the nginx

Something Called GD is in charge of handle the IMage content on the server

@3ub41r
Copy link
Contributor

3ub41r commented Aug 25, 2021

i'm using database queue with superuser for mail dispatching queue working fine. but, job failed and following error occurred League\Flysystem\Exception: Impossible to create the root directory.

my function is:

public function registerMediaConversions(Media $media = null)
    {
        $this->addMediaConversion('thumb_400')
            ->width(400)
            ->height(400)
            ->performOnCollections('books_image')
            ->nonQueued(); // without queue working fine.
         // ->queued(); // Above error occurred.
    }

any solution?

In my case, it was because the queue was running with a different user as the web server, www-data. I think the reason you are able to run it with nonQueued is that the user performing the conversions is the same as the user that saved the original file (www-data).

I changed the horizon supervisor config to run the queue as the same user as the web server :

[program:horizon]
process_name=%(program_name)s
command=php /var/www/myapp/artisan horizon
autostart=true
autorestart=true
user=www-data
redirect_stderr=true
stdout_logfile=/var/www/myapp/storage/logs/horizon.log
stopwaitsecs=3600

Hope this helps.

@askerakbar
Copy link

I'm also getting this error when the queue worker runs the Job to Perform thumbnail conversion.

League\Flysystem\Exception: Impossible to create the root directory

;s:19:\\\"chainCatchCallbacks\\\";N;s:5:\\\"delay\\\";N;s:11:\\\"afterCommit\\\";N;s:10:\\\"middleware\\\";a:0:{}s:7:\\\"chained\\\";a:0:{}}\"}}', 'League\\Flysystem\\Exception: Impossible to create the root directory \"/var/www/html/myapp/storage/app/public/2470/conversions\". in /var/www/html/myapp/vendor/league/flysystem/src/Adapter/Local.php:112\nStack trace:\n#0 /var/www/html/myapp/vendor/league/flysystem/src/Adapter/Local.php(156): League\\Flysystem\\Adapter\\Local->ensureDirectory()\n#1 /var/www/html/myapp/vendor/league/flysystem/src/Filesystem.php(123): League\\Flysystem\\Adapter\\Local->writeStream()\n#2 /var/www/html/myapp/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php(260)

Laravel Framework : 8.80.0
Laravel-medialibrary : ^7.19

Has anyone solved this issue?

@3ub41r
Copy link
Contributor

3ub41r commented Apr 19, 2022

I'm also getting this error when the queue worker runs the Job to Perform thumbnail conversion.

League\Flysystem\Exception: Impossible to create the root directory

;s:19:\\\"chainCatchCallbacks\\\";N;s:5:\\\"delay\\\";N;s:11:\\\"afterCommit\\\";N;s:10:\\\"middleware\\\";a:0:{}s:7:\\\"chained\\\";a:0:{}}\"}}', 'League\\Flysystem\\Exception: Impossible to create the root directory \"/var/www/html/myapp/storage/app/public/2470/conversions\". in /var/www/html/myapp/vendor/league/flysystem/src/Adapter/Local.php:112\nStack trace:\n#0 /var/www/html/myapp/vendor/league/flysystem/src/Adapter/Local.php(156): League\\Flysystem\\Adapter\\Local->ensureDirectory()\n#1 /var/www/html/myapp/vendor/league/flysystem/src/Filesystem.php(123): League\\Flysystem\\Adapter\\Local->writeStream()\n#2 /var/www/html/myapp/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php(260)

Laravel Framework : 8.80.0 Laravel-medialibrary : ^7.19

Has anyone solved this issue?

Have you checked if the user running the queue, in my case Horizon, has permission to write to the thumbnails folder?

@said-alramadhani
Copy link

said-alramadhani commented Feb 27, 2023

For any one facing an issue with permissions, you could use the following filesystems.php configuration:

'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
            'permissions' => [
                'file' => [
                    'public' => 0664,
                    'private' => 0600,
                ],
                'dir' => [
                    'public' => 0775,
                    'private' => 0700,
                ],
            ],
        ],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants