Laravel Image Intervention在手工艺命令中不起作用

时间:2019-01-25 11:29:30

标签: laravel controller command artisan

我正在从IGDB Api中获取游戏信息和图像,然后将图像存储在磁盘中并将信息存储在db中。它适用于Controller文件,但相同的代码不适用于artisan Command文件(仅Image Intervention不起作用)。

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();

}
}

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();


    }
}

我试图搜索为什么相同的代码在artisan命令文件中不起作用,我找不到有关它的任何信息,所以我想我会找到一个关于stackoverflow的解决方案。在此先感谢:)

1 个答案:

答案 0 :(得分:0)

我解决了它,感谢@niklaz的日志建议:)我确实看到我在运行artisan命令时遇到了错误,我确实在AppServiceProvider.php中注册了公共路径,现在可以了:)