无法使用带有 CakePHP2 的 GD 调整图像大小并将其上传到数据库
Cannot resize image and upload it to DB using GD with CakePHP2
我已成功将图像上传到我的数据库,但现在我需要为我添加的每张图像添加缩略图。我似乎无法调整图像大小并将图像作为单独的文件存储在我的 webroot/img/thumbnail 文件夹中。
我需要上传一个文件,在后端它应该 crop/resize 图像并将该图像以及原始图像存储在数据库中。
下面是我在表单上按提交时的控制器功能。
if(!empty($this->request->data['Student']['imagePath']))
{
$file = $this->request->data['Student']['imagePath']; //put the data into a var for easy
move_uploaded_file($file['tmp_name'], 'img/thumbnail/' . $file['name']);
$this->request->data['Student']['imagePath'] = $file['name'];
move_uploaded_file($file['tmp_name'], 'img/' . $file['name']);
$this->request->data['Student']['resizeImage'] = $file['name'];
}
没有错误。它只是不保存到数据库
这是问题的解决方案。要为缩略图创建一个单独的文件夹,我必须从 'img' 文件夹中获取图像并调整它们的大小,然后将其移动到缩略图文件夹。 :))
public function add() {
$data = $this->Department->find('list', array(
'fields' => array('departmentname'),
'contain' => array(
'Department'
)
)
);
$this->set('depdata', $data);
if ($this->request->is('post')) {
if (!empty($this->request->data['Student']['cvsFile'])) {
$cvs = $this->request->data['Student']['cvsFile'];
$ext = substr(strtolower(strrchr($cvs['name'], '.')), 1);
if ($this->Student->validate) {
move_uploaded_file($cvs['tmp_name'], 'files/' . $cvs['name']);
$this->request->data['Student']['cvsFile'] = $cvs['name'];
}
}
if (!empty($this->request->data['Student']['imagePath'])) {
$file = $this->request->data['Student']['imagePath'];
move_uploaded_file($file['tmp_name'], 'img/' . $file['name']);
if ($file['name'] == "") {
}
else {
list($old_width, $old_height) = getimagesize(WWW_ROOT . "img/" . $file['name']);
$new_width = 50;
$new_height = 50;
$ext = substr(strtolower(strrchr($file['name'], '.')), 1);
if ($ext == 'png') {
$new_image = imagecreatetruecolor($new_width, $new_height);
$old_image = imagecreatefrompng(WWW_ROOT . "img/" . $file['name']);
imagecopyresampled($new_image, $old_image, 0, 0, 0, 1, $new_width, $new_height, $old_width, $old_height);
imagepng($new_image, WWW_ROOT . "img/thumbnail/" . $file['name'], 3);
}
elseif ($ext == 'jpg') {
$new_image = imagecreatetruecolor($new_width, $new_height);
$old_image = imagecreatefromjpeg(WWW_ROOT . "img/" . $file['name']);
imagecopyresampled($new_image, $old_image, 0, 0, 1, 1, $new_width, $new_height, $old_width, $old_height);
imagejpeg($new_image, WWW_ROOT . "img/thumbnail/" . $file['name'], 30);
}
$this->request->data['Student']['imagePath'] = 'thumbnail/' . $file['name'];
}
if ($this->Student->save($this->request->data)) {
$this->Flash->set("Student has been added.");
$this->redirect(array('action' => 'add'));
}
}
}
}
我已成功将图像上传到我的数据库,但现在我需要为我添加的每张图像添加缩略图。我似乎无法调整图像大小并将图像作为单独的文件存储在我的 webroot/img/thumbnail 文件夹中。
我需要上传一个文件,在后端它应该 crop/resize 图像并将该图像以及原始图像存储在数据库中。 下面是我在表单上按提交时的控制器功能。
if(!empty($this->request->data['Student']['imagePath']))
{
$file = $this->request->data['Student']['imagePath']; //put the data into a var for easy
move_uploaded_file($file['tmp_name'], 'img/thumbnail/' . $file['name']);
$this->request->data['Student']['imagePath'] = $file['name'];
move_uploaded_file($file['tmp_name'], 'img/' . $file['name']);
$this->request->data['Student']['resizeImage'] = $file['name'];
}
没有错误。它只是不保存到数据库
这是问题的解决方案。要为缩略图创建一个单独的文件夹,我必须从 'img' 文件夹中获取图像并调整它们的大小,然后将其移动到缩略图文件夹。 :))
public function add() {
$data = $this->Department->find('list', array(
'fields' => array('departmentname'),
'contain' => array(
'Department'
)
)
);
$this->set('depdata', $data);
if ($this->request->is('post')) {
if (!empty($this->request->data['Student']['cvsFile'])) {
$cvs = $this->request->data['Student']['cvsFile'];
$ext = substr(strtolower(strrchr($cvs['name'], '.')), 1);
if ($this->Student->validate) {
move_uploaded_file($cvs['tmp_name'], 'files/' . $cvs['name']);
$this->request->data['Student']['cvsFile'] = $cvs['name'];
}
}
if (!empty($this->request->data['Student']['imagePath'])) {
$file = $this->request->data['Student']['imagePath'];
move_uploaded_file($file['tmp_name'], 'img/' . $file['name']);
if ($file['name'] == "") {
}
else {
list($old_width, $old_height) = getimagesize(WWW_ROOT . "img/" . $file['name']);
$new_width = 50;
$new_height = 50;
$ext = substr(strtolower(strrchr($file['name'], '.')), 1);
if ($ext == 'png') {
$new_image = imagecreatetruecolor($new_width, $new_height);
$old_image = imagecreatefrompng(WWW_ROOT . "img/" . $file['name']);
imagecopyresampled($new_image, $old_image, 0, 0, 0, 1, $new_width, $new_height, $old_width, $old_height);
imagepng($new_image, WWW_ROOT . "img/thumbnail/" . $file['name'], 3);
}
elseif ($ext == 'jpg') {
$new_image = imagecreatetruecolor($new_width, $new_height);
$old_image = imagecreatefromjpeg(WWW_ROOT . "img/" . $file['name']);
imagecopyresampled($new_image, $old_image, 0, 0, 1, 1, $new_width, $new_height, $old_width, $old_height);
imagejpeg($new_image, WWW_ROOT . "img/thumbnail/" . $file['name'], 30);
}
$this->request->data['Student']['imagePath'] = 'thumbnail/' . $file['name'];
}
if ($this->Student->save($this->request->data)) {
$this->Flash->set("Student has been added.");
$this->redirect(array('action' => 'add'));
}
}
}
}