Search code examples
laravelcontrollercommandlaravel-artisan

Laravel Image Intervention not working in artisan command


I am getting game informations and images from IGDB Api with guzzle and then I am storing images in my disk and informations in db. Its working on Controller file but same code is not working on artisan Command file(Only Image Intervention is not working).

GameController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Game;
use Image;
use Carbon\Carbon;
use GuzzleHttp\Client;

class GameController extends Controller
{

  public function index()
  {

    $client = new Client(['headers' => ['user-key' => 'xxxxx']]);
    $response = $client->request('GET', 'https://api-endpoint.igdb.com/games/96209');
    $result = json_decode($response->getBody(), true);

    $nameGame = $result[0]['name'];
    $imageGame = $result[0]['artworks'][0]['cloudinary_id'];
    $summaryGame = $result[0]['summary'];
    $releaseGame = $result[0]['first_release_date'];

    $releaseGameDate = Carbon::createFromTimestamp($releaseGame/1000)->toDateTimeString();

    $postImage = 'https://images.igdb.com/igdb/image/upload/t_1080p/' . $imageGame . '.jpg';
    $filename = $imageGame. '.jpg';
    $height = Image::make($postImage)->height();

    Image::make($postImage)
    ->resize(null, 1920, function ($constraint) {
             $constraint->aspectRatio();
             $constraint->upsize();
           })->save( public_path('/images/games/image/large/' . $filename ), 75)
    ->resize(null, 480, function ($constraint) {
             $constraint->aspectRatio();
             })->save( public_path('/images/games/image/medium/' . $filename ), 75)
    ->resize(null, 128, function ($constraint) {
             $constraint->aspectRatio();
             })->save( public_path('/images/games/image/small/' . $filename ) );

    $addit = new Game;
    $addit->name = $nameGame;
    $addit->image = $filename;
    $addit->summary = $summaryGame;
    $addit->release_date = $releaseGameDate;
    $addit->save();

}
}

And Same Code in addGames.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Game;
use Image;
use Carbon\Carbon;
use GuzzleHttp\Client;

class addGames extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:addGames';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'This command adding games to db';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

      $client = new Client(['headers' => ['user-key' => 'xxxx']]);
      $response = $client->request('GET', 'https://api-endpoint.igdb.com/games/96209');
      $result = json_decode($response->getBody(), true);

      $nameGame = $result[0]['name'];
      $imageGame = $result[0]['artworks'][0]['cloudinary_id'];
      $summaryGame = $result[0]['summary'];
      $releaseGame = $result[0]['first_release_date'];

      $releaseGameDate = Carbon::createFromTimestamp($releaseGame/1000)->toDateTimeString();

      $postImage = 'https://images.igdb.com/igdb/image/upload/t_1080p/' . $imageGame . '.jpg';
      $filename = $imageGame. '.jpg';
      $height = Image::make($postImage)->height();

      Image::make($postImage)
      ->resize(null, 1920, function ($constraint) {
               $constraint->aspectRatio();
               $constraint->upsize();
             })->save( public_path('/images/games/image/large/' . $filename ), 75)
      ->resize(null, 480, function ($constraint) {
               $constraint->aspectRatio();
               })->save( public_path('/images/games/image/medium/' . $filename ), 75)
      ->resize(null, 128, function ($constraint) {
               $constraint->aspectRatio();
               })->save( public_path('/images/games/image/small/' . $filename ) );

      $addit = new Game;
      $addit->name = $nameGame;
      $addit->image = $filename;
      $addit->summary = $summaryGame;
      $addit->release_date = $releaseGameDate;
      $addit->save();


    }
}

I tried to search why same code is not working in artisan command file, I could not find any informations about it so I thought I will find a solution on stackoverflow. Thanks in advance :)


Solution

  • I solved it, thanks to @niklaz for log suggest :) I did saw public_path is coming wrong when I run artisan command, I did register public path in AppServiceProvider.php and its OK now :)