【问题标题】:Laravel Image Intervention not working in artisan commandLaravel 图像干预在工匠命令中不起作用
【发布时间】:2019-06-19 05:44:32
【问题描述】:

我正在使用 guzzle 从 IGDB Api 获取游戏信息和图像,然后我将图像存储在我的磁盘中,并将信息存储在数据库中。它适用于控制器文件,但相同的代码不适用于工匠命令文件(只有图像干预不起作用)。

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 上找到解决方案。在此先感谢:)

【问题讨论】:

  • 嗨,你的命令句柄中的其他所有东西都可以工作,除了干预?
  • @niklaz 是的,当我从命令文件中删除干预代码时,它运行良好,但不能使用干预代码:/
  • 您是否尝试在 try/catch、调用日志中封装干预代码块,并查看输出结果?
  • @niklaz 我解决了,感谢日志建议 :) 当我运行 artisan 命令时,我确实看到 public_path 出错了,我确实在 AppServiceProvider.php 中注册了公共路径,现在可以了 :)跨度>
  • 很高兴听到 :) 您可以将其添加为问题的答案并完成问题 :)

标签: laravel controller command laravel-artisan


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2013-09-25
    • 2015-09-24
    • 2017-06-21
    • 2013-02-05
    • 2015-03-12
    • 2016-12-10
    • 2021-11-04
    • 2014-08-30
    • 2019-03-05
    相关资源
    最近更新 更多