【问题标题】:CakePHP Uploader: Change resize values depending on image dimensionCakePHP Uploader:根据图像尺寸更改调整大小值
【发布时间】:2014-05-13 15:28:08
【问题描述】:

我正在尝试在我的应用程序中实现 Miles Johnson 出色的上传器组件。但问题是:根据上传图片的尺寸,我需要更改调整尺寸。

我尝试在回调中修改转换:

    public $actsAs = array(
        'Uploader.Attachment' => array(
            'image' => array(
                'nameCallback' => 'formatName',
                'tempDir' => TMP,
                'uploadDir' => UPLOADDIR,
                'finalPath' => '/img/photos/',
                'overwrite' => false,
                'stopSave' => true,
                'allowEmpty' => false,
                'transforms' => array(
                    array(
                        'class' => 'exif',
                        'self' => true
                    ),
                    'image_sized' => array(
                        'class' => 'resize',
                        'nameCallback' => 'transformSizedNameCallback',
                        'width' => 1680,
                        'height' => 980,
                        'aspect' => true  
                    )
                )
            )
        ),
        'Uploader.FileValidation' => array(
            'image' => array(
                'extension' => array(
                    'value' => array('jpg', 'png', 'jpeg'),
                    'error' => 'Nicht unterstütztes Dateiformat - bitte JPG- oder PNG-Datei hochladen.'
                ),
                'minHeight' => array(
                    'value' => 980,
                    'error' => 'Das Bild muss mindestens 980 Pixel hoch sein.'
                ),
                'minWidth' => array(
                    'value' => 980,
                    'error' => 'Das Bild muss mindestens 980 Pixel breit sein.'
                ),
                'required' => array(
                    'value' => true,
                    'error' => 'Bitte wählen Sie ein Bild aus.'
                )
            )
        )
    );

    public function beforeTransform($options) {
        if($options['dbColumn'] == 'image_sized') {
            if($height > $width) {
                $options['width'] = 980;
                $options['height'] = 1680;
            }
        }
        return $options;
    }

我能够确定正确的变换,但是如何访问要在 beforeTransform 内变换的图像的尺寸?我从哪里获得$width$height

【问题讨论】:

    标签: cakephp uploader


    【解决方案1】:

    我不熟悉它,但是从查看代码看来,您当时唯一的选择是使用 dbColumn 值来访问当前处理的字段数据,类似于

    $file = $this->data[$this->alias][$options['dbColumn']];
    

    当然这需要dbColumn 值来匹配输入字段名称!如果不是这种情况,那么您将需要一个额外的选项来保存字段名称并使用该选项。

    现在$file 只是原始数据,很可能是file upload array。假设一个文件,请自行检查tmp_name 的尺寸,或使用Transite\File 类,该类可以处理文件上传数组并公开检索可能图像尺寸的方法:

    $transitFile = new File($file);
    $dimensions = $transitFile->dimensions();
    

    https://github.com/milesj/transit/blob/1.5.1/src/Transit/File.php#L121

    所以最后你可以这样做:

    public function beforeTransform($options) {
        if($options['dbColumn'] == 'image_sized') {
            $file = $this->data[$this->alias][$options['dbColumn']];
            $transitFile = new \Transit\File($file);
            $dimensions = $transitFile->dimensions();
    
            if($dimensions === null) {
                // not an image or something else gone wrong,
                // maybe throw an exception or wave your arms and call for help
            } elseif($dimensions['height'] > $dimensions['width']) {
                $options['width'] = 980;
                $options['height'] = 1680;
            }
        }
        return $options;
    }
    

    请注意,这都是未经测试的示例代码。

    【讨论】:

      猜你喜欢
      • 2012-01-27
      • 2015-03-12
      • 2014-12-06
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      • 2020-03-06
      • 1970-01-01
      • 2012-12-16
      相关资源
      最近更新 更多