dswwuo1223 2019-06-25 13:04
浏览 55

使用带CakePHP2的GD无法调整图像大小并将其上传到数据库

I have successfully uploaded images to my database but now I need thumbnails for each of the image I am adding. I cannot seem to resize and store the image as a separate file in my webroot/img/thumbnail folder.

I need to upload a file and in the back end it should crop/resize the image and store that image as well as the original image in the database. Below is my controller function for when I press submit on the form.

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'];

                }

No errors. It just doesn't save to the DB

  • 写回答

1条回答 默认 最新

  • douchen4915 2019-07-04 13:14
    关注

    Here's the solution to the problem. To create a separate folder for thumbnails, I had to take images from the 'img' folder and resize them, AND THEN move it to the thumbnail folder. :))

    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'));
                }
            }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示