在我的控制器中我使用功能上传网站徽标,但我想验证徽标图片的高度为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));
}
}
答案 0 :(得分:0)
使用PHP的getimagesize()
。该函数将图像的路径作为输入,并返回图像数据的数组。第一个数组键分别包含宽度和高度。
我不确定您何时要验证高度(在文件上传之前或之后),但如果您希望验证成为模型验证规则的一部分,您可以编写自定义验证函数as documented here。验证规则函数的第一个变量包含相关的表单数据(在文档中名为$check
),然后您可以将其与getimagesize()
结合使用。