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-packageshttps://laracasts.com/lessons/laravel-5-commands

Illuminate 包中的验证器类非常棒, http: //laravel.com/api/5.0/Illuminate/Validation/Validator.html - 我猜我不确定这是什么问题。

我会说,除非您有令人信服的理由为此使用 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/ - 用于此类事情的 Slack 频道。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

FormRequest您甚至可以在将请求发送到命令总线之前使用 Laravel 5 的类来捕获您的请求:

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 回答