Symfony3:如何验证最小图像宽度或高度?

时间:2017-05-26 11:49:03

标签: symfony

标准Symfony的Assert\Image验证程序允许检查minWidthminHeight。我的目标是拒绝宽度和高度都小于1000像素的图像。如果宽度或高度大于1000px,则图像应该有效。如何实现这个?

2 个答案:

答案 0 :(得分:1)

您需要custom constraint Validator

我会通过添加import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class HKtimeZone { public static void main(String [] args ) throws ParseException{ String dateString = "1900-01-01 08:00:00.000"; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); Date endDate = df.parse(dateString); System.out.println(endDate); } } 之类的其他验证选项来扩展Symfony\Component\Validator\Constraints\ImageValidatorSymfony\Component\Validator\Constraints\Image

答案 1 :(得分:0)

使用Callback约束:

// src/AppBundle/Entity/Person.php
namespace AppBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\HttpFoundation\File\File;


class Person
{
    /**
     * @var File
     */
    protected $image;

    /**
     * @Assert\Callback
     */
    public function validate(ExecutionContextInterface $context, $payload)
    {
        // get width and height from image
        $info = getimagesize($this->getImage());
        list($width, $height) = $info;

        // check if the name is actually a fake name
        if ($width < 1000 && $height < 1000) {
            $context->buildViolation('The image should have at least a width or height of 1000!')
                    ->atPath('image')
                    ->addViolation();
        }
    }
}