管理中的奏鸣曲媒体验证 [英] Sonata media validation at admin
问题描述
我正在尝试验证图像.我看到了答案Sonata Media:找不到文件
I'm trying to validate image. I saw answer at Sonata Media: The file could not be found
如何验证图像(宽度和高度).我需要这方面的帮助.网络上没有合适的文档.
How to validate image (width and height). I need help on that. There is no proper doc in net.
推荐答案
要使用 Sonata 媒体验证图像尺寸,您需要覆盖 Sonata 媒体的 ImageProvider
类,sonata 使用此类来处理图像处理.如果您已经拥有扩展包的奏鸣曲媒体包然后在 services.yml 文件中,您可以定义自己的提供程序,如下所示,确保您的 yml 文件包含在主 config.yml 中
To validate image dimensions using sonata media you need to override sonata media's ImageProvider
class, sonata uses this class to handle image manipulation.If you already have an extended bundle of sonata media bundle then in services.yml file you can define your own provider as below ,make sure your yml file is included in main config.yml
parameters:
sonata.media.provider.file.class: Application\Sonata\MediaBundle\Provider\ImageProvider
现在创建您的提供者并使用奏鸣曲媒体的 ImageProvider
覆盖器 validate()
函数对其进行扩展,并定义您自己的验证或可以覆盖 buildCreateForm()/buildEditForm()
并为 binaryContent
字段定义断言
Now create your provider and extend it with sonata media's ImageProvider
overrider validate()
function and define your own validation or can override buildCreateForm()/buildEditForm()
and define your asserts for binaryContent
field
命名空间 Application\Sonata\MediaBundle\Provider;
namespace Application\Sonata\MediaBundle\Provider;
//... other uses classes
use Sonata\MediaBundle\Provider\ImageProvider as BaseProvider;
class ImageProvider extends BaseProvider
{
public function __construct($name, Filesystem $filesystem, CDNInterface $cdn, GeneratorInterface $pathGenerator, ThumbnailInterface $thumbnail, array $allowedExtensions = array(), array $allowedMimeTypes = array(), MetadataBuilderInterface $metadata = null)
{
parent::__construct($name, $filesystem, $cdn, $pathGenerator, $thumbnail);
$this->allowedExtensions = $allowedExtensions;
$this->allowedMimeTypes = $allowedMimeTypes;
$this->metadata = $metadata;
}
/**
* {@inheritdoc}
*/
public function validate(ErrorElement $errorElement, MediaInterface $media)
{
if (!$media->getBinaryContent() instanceof \SplFileInfo) {
return;
}
if ($media->getBinaryContent() instanceof UploadedFile) {
$fileName = $media->getBinaryContent()->getClientOriginalName();
} elseif ($media->getBinaryContent() instanceof File) {
$fileName = $media->getBinaryContent()->getFilename();
} else {
throw new \RuntimeException(sprintf('Invalid binary content type: %s', get_class($media->getBinaryContent())));
}
if (!in_array(strtolower(pathinfo($fileName, PATHINFO_EXTENSION)), $this->allowedExtensions)) {
$errorElement
->with('binaryContent')
->addViolation('Invalid extensions')
->end();
}
if (!in_array($media->getBinaryContent()->getMimeType(), $this->allowedMimeTypes)) {
$errorElement
->with('binaryContent')
->addViolation('Invalid mime type : ' . $media->getBinaryContent()->getMimeType())
->end();
}
if ($media->getWidth() > '1280' || $media->getHeight() > 1280) {
$errorElement
->with('binaryContent')
->addViolation('Invalid File Dimension : Please upload 1280px * (1280px) image')
->end();
}
}
}
这篇关于管理中的奏鸣曲媒体验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!