gpt4 book ai didi

php - 上传图像(JPEG)时有没有办法检查 DPI?

转载 作者:搜寻专家 更新时间:2023-10-31 20:45:01 25 4
gpt4 key购买 nike

有没有办法在上传图片(JPEG)时检查DPI?

我想把它集成到一个表单中,作为一个验证器。

最佳答案

您必须使用 Imagick(或 Gmagick)打开图像,然后调用 getImageResolution .

$image = new Imagick($path_to_image);
var_dump($image->getImageResolution());

结果:

Array
(
[x]=>75
[y]=>75
)

编辑:

要集成到 symfony 中,您可以为此使用自定义验证器。您扩展默认值以验证文件并添加 DPI 限制。

将这个创建到 /lib/validator/myCustomValidatorFile .class.php:

<?php

class myCustomValidatorFile extends sfValidatorFile
{
protected function configure($options = array(), $messages = array())
{
parent::configure($options, $messages);

$this->addOption('resolution_dpi');
$this->addMessage('resolution_dpi', 'DPI resolution is wrong, you should use image with %resolution_dpi% DPI.');
}

protected function doClean($value)
{
$validated_file = parent::doClean($value);

$image = new Imagick($validated_file->getTempName());
$resolution = $image->getImageResolution();

if (empty($resolution))
{
throw new sfValidatorError($this, 'invalid');
}

if ((isset($resolution['x']) && $resolution['x'] < $this->getOption('resolution_dpi')) || (isset($resolution['y']) && $resolution['y'] < $this->getOption('resolution_dpi')))
{
throw new sfValidatorError($this, 'resolution_dpi', array('resolution_dpi' => $this->getOption('resolution_dpi')));
}

return $validated_file;
}
}

然后,在您的表单中,为您的文件使用此验证器:

$this->validatorSchema['file'] = new myCustomValidatorFile(array(
'resolution_dpi' => 300,
'mime_types' => 'web_images',
'path' => sfConfig::get('sf_upload_dir'),
'required' => true
));

关于php - 上传图像(JPEG)时有没有办法检查 DPI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14358524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com