0

在我的控制器中,我正在使用函数上传网站徽标,但我想验证徽标图像的高度为 65px 我如何在 cakephp 中实现这一点。谢谢

   if ($this->request->is('post')||$this->request->is('put')) {

        if (!empty($this->data['GlobalAdminSetting']['site_logo']['name']) && is_uploaded_file($this->data['GlobalAdminSetting']['site_logo']['tmp_name'])) {
            $allowedType = array('image/jpeg', 'image/pjpeg', 'image/jpeg', 'image/pjpeg', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png');
            if (!is_dir(APP . DS . WEBROOT_DIR . DS . 'siteImg/')) {
                mkdir(APP . DS . WEBROOT_DIR . DS . 'siteImg/');
                chmod(APP . DS . WEBROOT_DIR . DS . 'siteImg/', 0777);
            }

            $imageName = $this->data['GlobalAdminSetting']['site_logo'];
            unset($this->request->data['GlobalAdminSetting']['site_logo']);
            if (!in_array($imageName['type'], $allowedType)) {
                $this->request->data['GlobalAdminSetting']['site_logo'] = 'invalidFormat';
                $this->Session->setFlash(__('Sorry, Logo could not be uploaded. only jpeg,png are allowed.'));

            } else {
                if ($imageName != '') {
                    $filePath = APP . DS . WEBROOT_DIR . DS . 'siteImg/' . $imageName['name'];
                    move_uploaded_file($imageName['tmp_name'], $filePath);
                    chmod(APP . DS . WEBROOT_DIR . DS . 'siteImg/'. $imageName['name'], 0777);
                    $this->GlobalAdminSetting->save(array('id' => 1, 'site_logo' => 'siteImg/'. $imageName['name']));
                    $this->Session->setFlash(__('site logo is uploaded Successfully.'));
                } else {
                    $this->Session->setFlash(__('Image Not Found to upload.'));
                }
            }
        }else{
            $this->Session->setFlash(__('Image Not Found to upload.'));
        }
        $this->redirect(array('controller' => 'global_admin_settings', 'action' => 'logo'));
    } else{
   $this->set('logo',$this->GlobalAdminSetting->findById(1));
    }
}
4

1 回答 1

0

使用 PHP 的getimagesize(). 该函数将图像的路径作为输入并返回图像数据数组。第一个数组键分别包含宽度和高度。

我不确定您何时要验证高度(在文件上传之前或之后),但如果您希望验证成为模型验证规则的一部分,您可以编写一个自定义验证函数,如此处所述。验证规则函数的第一个变量包含相关的表单数据($check在文档中命名),然后您可以将其与getimagesize().

于 2013-04-15T10:17:01.803 回答