我正在使用 foresight.js 为视网膜设备加载高分辨率图像。 Foresight 尝试用 2 倍像素密度图像替换低分辨率图像。由于 foresight 尝试在页面呈现之前替换低分辨率图像,因此我无法在模板中使用 GD 图像调整大小方法来调整图像大小。因此,我允许 SS 3.1 cms 用户上传一张大图像,并让系统在上传后重新调整其大小 - 在 Assets 文件夹中保留 1x 和 2x 图像。
我的问题是,如果 cms 用户没有上传足够大的图像,如何设置自定义验证错误消息?
这是在上传时调整图像大小的代码。
class ResampledImage extends Image {
static $default_lores_x = 250;
static $default_lores_y = 250;
static $default_hires_x = 500;
static $default_hires_y = 500;
static $default_assets_dir = 'Uploads';
static $hires_flag = '2x';
function getLoResX() {
return ( static::$lores_x ) ? static::$lores_x : self::$default_lores_x;
}
function getLoResY() {
return ( static::$lores_y ) ? static::$lores_y : self::$default_lores_y;
}
function getHiResX() {
return ( static::$hires_x ) ? static::$hires_x : self::$default_hires_x;
}
function getHiResY() {
return ( static::$hires_y ) ? static::$hires_y : self::$default_hires_y;
}
function getAssetsDir() {
return ( static::$assets_dir ) ? static::$assets_dir : self::$default_assets_dir;
}
function onAfterUpload() {
$this->createResampledImages();
}
function onAfterWrite() {
$this->createResampledImages();
}
function createResampledImages() {
$extension = strtolower($this->getExtension());
if( $this->getHeight() >= $this->getHiResX() || $this->getWidth() >= $this->getHiResY() ) {
$original = $this->getFullPath();
$resampled = $original. '.tmp.'. $extension;
$orig_title = $this->getTitle();
$path_to_hires = Director::baseFolder() . '/' . ASSETS_DIR . '/' . $this->getAssetsDir();
$hires = $path_to_hires . '/' . $orig_title . self::$hires_flag . '.' . $extension;
$gd_lg = new GD($original);
$gd_sm = new GD($original);
if ( $gd_lg->hasImageResource() ) {
$gd_lg = $gd_lg->resizeRatio($this->getHiResX(), $this->getHiResY());
if ( $gd_lg )
$gd_lg->writeTo($hires);
}
if($gd_sm->hasImageResource()) {
$gd_sm = $gd_sm->resizeRatio($this->getLoResX(), $this->getLoResY());
if($gd_sm) {
$gd_sm->writeTo($resampled);
unlink($original);
rename($resampled, $original);
}
}
}
}
查看 UploadField::setFileEditValidator() ,我似乎可以在扩展 Image 类上指定一个方法用作验证器,以便我可以检查 $this->getWidth() 和 $this->getHeight()如果它们不够大,则返回错误。
这可能吗?
我尝试将以下方法添加到 ResampledImage,但没有成功:
function MyValidator() {
$valid = true;
if ( $this->getHeight() < $this->getHiResX() || $this->getWidth() < $this->getHiResY() ) {
$this->validationError("Thumbnail",'Please upload a larger image');
$valid = false;
}
return $valid;
}
最佳答案
我认为fileEditValidator
实际上是在图像上传后使用的,并且在显示/编辑时用于EditForm
。
似乎您正在寻找的是验证上传
。您可以在 UploadField
上使用 setValidator($validator)
设置自定义 Upload_Validator
。
所以我会尝试创建一个扩展 Upload_Validator
的自定义验证器类(可能名为 CustomUploadValidator)(源代码可以在框架中的 Upload.php 文件中找到)。所以,沿着这些思路:
$myValidator = new CustomUploadValidator();
$uploadField->setValidator($myValidator);
在您的自定义验证器类中,可以创建一个方法 isImageLargeEnough()
,您将在 validate()
方法中调用该方法:
public function validate() {
if(!$this->isImageLargeEnough()) {
$this->errors[] = 'Image size is not large enough';
return false;
}
return parent::validate();
}
在isImageLargeEnough()
中,您可以通过$this->tmpFile
访问上传的图像。所以也许可以做这样的事情:
public function isImageLargeEnough()
{
$imageSize = getimagesize( $this->tmpFile["tmp_name"] );
if ($imageSize !== false)
{
if ( $imageSize[0] < 500 || $imageSize[1] < 500 )
{
return false;
}
}
return true;
}
这里的最小宽度/高度被硬编码为 500,但您可以实现一个 setMinImageSizes
方法,将它们存储在自定义验证器类中的变量上。可以这样调用 $uploadField->getValidator()->setMinImageSize(543, 876);
这些都没有经过实际测试,但希望它能为您提供一些有关寻找内容的指导。
关于Uploadfield 上的 Silverstripe 自定义验证器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18242663/