Thursday, March 9, 2017

Image migration script - Allowed memory size of 134217728 bytes exhausted [duplicate]

I am trying to import around 130k images via url. I thought it would be convenient to write a cli artisan command.

I am using laravel and "Spatie/MediaLibrary".

This is my script:

public function handle()
    {
       Article::where('index_block_position',0)->each(function($article){
            if($article->featured_image != ''){
                try{
                    $featuredImage = $this->mediaLibrary->addMediaFromUrl($article, 'http://myoldwebsite.com' . $article->featured_image, 'featured_image');
                    $libraryImage = $this->mediaLibrary->addMediaFromUrl(MediaLibrary::first(), 'http://myoldwebsite.com' . $article->featured_image);
                    $article->index_block_position = 1;
                    $article->save();
                    $this->info("Article {$article->id} image: {$featuredImage->id} - Library image: {$libraryImage->id} - added");
                    Log::useDailyFiles(storage_path().'/logs/featured-image-migration.log');
                    Log::info("Article {$article->id} image: {$featuredImage->id} - Library image: {$libraryImage->id} - added");
                }catch (\Exception $e){
                    $this->info("image: {$article->featured_image} - exception: {$e->getMessage()}");
                    Log::useDailyFiles(storage_path().'/logs/featured-image-migration.log');
                    Log::info($e->getMessage());
                }
            }
        });
        $this->info('All done!');
    }

Unfortunately, after 10 or so images added I get the following error:

Symfony\Component\Debug\Exception\FatalErrorException: Allowed memory size of 134217728 bytes exhausted (tried to allocate 24576 bytes)

I am not sure how i can make this work because i have 130k images to import and the memory limit of 128M is already exhausted after 10 images...

I would appreciate suggestions.



via Adnan Mujkanovic

Advertisement