9

Laravel 5 のコマンド バスを使用していますが、バリデータ クラスの実装方法がわかりません。

画像のサイズを変更する前に、画像が実際に画像であることを確認する ResizeImageCommandValidator クラスを作成したいと思います。

抜き出したいコードは、ResizeImageCommandHandler resize メソッドからここにあります。

if (!($image instanceof Image))
{
    throw new ImageInvalidException('ResizeImageCommandHandler');
}

このアイデアは Laracasts Commands and Domain Eventsから生まれましたが、Jeffrey は Laravel 5 アーキテクチャを使用していません。

これがコードです。

ResizeImageCommandHandler.php

<?php namespace App\Handlers\Commands;

use App\Commands\ResizeImageCommand;

use App\Exceptions\ImageInvalidException;
use Illuminate\Queue\InteractsWithQueue;
use Intervention\Image\Image;

class ResizeImageCommandHandler {

    /**
     * Create the command handler.
     */
    public function __construct()
    {
    }
    /**
     * Handle the command.
     *
     * @param  ResizeImageCommand  $command
     * @return void
     */
    public function handle($command)
    {
        $this->resizeImage($command->image, $command->dimension);
    }
    /**
     * Resize the image by width, designed for square image only
     * @param Image $image Image to resize
     * @param $dimension
     * @throws ImageInvalidException
     */
    private function resizeImage(&$image, $dimension)
    {
        if (!($image instanceof Image))
        {
            throw new ImageInvalidException('ResizeImageCommandHandler');
        }
        $image->resize($dimension, null, $this->constrainAspectRatio());
    }
    /**
     * @return callable
     */
    private function constrainAspectRatio()
    {
        return function ($constraint) {
            $constraint->aspectRatio();
        };
    }


}      

ResizeImageCommand.php

<?php namespace App\Commands;

use App\Commands\Command;

use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldBeQueued;
use Image;

class ResizeImageCommand extends Command {
    use InteractsWithQueue, SerializesModels;

    public $image;
    public $savePath;
    public $dimension;

    /**
     * Create a new command instance.
     * @param Image $image
     * @param string $savePath
     * @param int $dimension
     * @param int $pose_id
     * @param int $state_id
     */
    public function __construct(&$image, $savePath, $dimension)
    {
        $this->image = $image;
        $this->savePath = $savePath;
        $this->dimension = $dimension;
    }

}
4

2 に答える 2

4

あなたの質問に答えるために、コマンドの部分にあまりこだわらないことをお勧めします。Laravel 5.1 では、そのフォルダーの名前が「Jobs」に変更されています。

https://laravel-news.com/2015/04/laravel-5-1/

これはまさに、人々が「コマンド」という言葉にとらわれすぎているとテイラーが感じたからです。

http://www.laravelpodcast.com/episodes/6823-episode-21-commands-pipelines-and-packagesおよびhttps://laracasts.com/lessons/laravel-5-commandsも参照してください

Illuminate パッケージのバリデータ クラスは非常に優れいます。

これに Command クラスを使用するやむを得ない理由がない限り、使用しないでください。参照: http://www.laravelpodcast.com/episodes/9313-episode-23-new-beginnings-envoyer-laravel-5-1

間違った質問をした可能性があり、コマンドを使用してこれを処理する必要はないかもしれません。

これはおそらくあなたが探している答えです: https://mattstauffer.co/blog/laravel-5.0-validateswhenresolved

use Illuminate\Contracts\Validation\ValidatesWhenResolved;

それでもうまくいかない場合は、Larachat ( http://larachat.co/ ) にサインアップしてください。Laravel ヘルプの最高の場所です。(もちろん、スタック オーバーフローを除く)

これは、画像の形式をチェックするために使用する小さなクラスです。これも役立つと思います。

<?php
Class FileUploadFormat
{
public function is_image($image_path)
  {
      if (!$f = fopen($image_path, 'rb'))
      {
          return false;
      }

      $data = fread($f, 8);
      fclose($f);

      // signature checking
      $unpacked = unpack("H12", $data);
      if (array_pop($unpacked) == '474946383961' || array_pop($unpacked) == '474946383761') return "gif";
      $unpacked = unpack("H4", $data);
      if (array_pop($unpacked) == 'ffd8') return "jpg";
      $unpacked = unpack("H16", $data);
      if (array_pop($unpacked) == '89504e470d0a1a0a') return "png";
      return false;
  }
}
于 2015-05-12T18:10:17.127 に答える
2

Laravel 5 のFormRequestクラスを使用して、コマンド バスに送信される前にリクエストをキャプチャできます。

public function postResizeImage(ResizeImageRequest $request) {
    // Send request to the Command Bus
}

次に、ResizeImageRequestクラスにルールを入れて、アップロードされたファイルが実際に画像であるかどうかを検証するためのカスタム検証が必要になる場合があります。

https://packagist.org/packages/intervention/imageを使用してイメージ ファイルを操作できます。または、これを使用できますhttps://github.com/cviebrock/image-validator

これについてさらにサポートが必要な場合は私に尋ねてください

于 2015-05-11T07:10:44.453 に答える