move_uploaded_file影响JPG图像质量(颜色)

时间:2016-01-19 17:01:03

标签: php image file-upload upload

我正在尝试使用move_uploaded_file()函数在文件夹中上传图像。图像通常是JPG。

问题在于移动过程会以某种方式影响图像质量。对我来说,我要上传的文件和上传的文件之间的颜色不一样。

到目前为止,我认为问题在于move_uploaded_file()函数,因为临时文件的颜色是正确的。

以下是我的代码以及上传前后的图片(上一张是在上传之前,另一张是在上传之后)。

这种行为不被接受,因为结果需要准备打印。

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

        if (!$this->request->data['TplElement']['file']) {
            $tplElementImage['TplElementImage']['original_id'] = 0;
            $tplElementImage['TplElementImage']['filepath'] = NULL;
            $tplElementImage['TplElementImage']['filepath_hires'] = NULL;
            $this->TplElementImage->save($tplElementImage);
        }
        else {
            //create the directory
            $path = APP.WEBROOT_DIR.DS.'uploads'.DS.'templates'.DS.$tplElement['TplElement']['tpl_asset_page_id'].DS.$tplElementImage['TplElementImage']['tpl_element_id'].DS.$elementID;

            if ($this->make_path($path.DS.'dummy.txt')) {
                $tplElementImage['TplElementImage']['original_file_name'] = $this->request->data['TplElement']['file']['name'];
                $filename = session_id().'_'.time().'_'.$tplElementImage['TplElementImage']['original_file_name'];
                if ($this->request->data['TplElement']['file']['size'] > 0) {
                    if(move_uploaded_file($this->request->data['TplElement']['file']['tmp_name'], $path.DS.$filename)) {
                        $tplElementImage['TplElementImage']['filepath'] = '/uploads' . '/' . 'templates' . '/' . $tplElement['TplElement']['tpl_asset_page_id'] . '/' . $tplElementImage['TplElementImage']['tpl_element_id'] . '/' . $elementID . '/' . $filename;

                        $imageSize = getimagesize($path . DS . $filename);

                        $imageWidth = $imageSize[0];
                        $imageHeight = $imageSize[1];

                        $zoom = 1;

                        $imageWidthMm = $imageWidth * 25.4 / 200;
                        $imageHeightMm = $imageHeight * 25.4 / 200;

                        $inBlockImageHeight = $imageHeight * $blockWidth / $imageWidth;
                        $inBlockImageWidth = $imageWidth * $blockHeight / $imageHeight;
                        if ($inBlockImageHeight < $blockHeight || $inBlockImageWidth < $blockWidth) {
                            $zoom = max($blockHeight / $imageHeightMm, $blockWidth / $imageWidthMm);
                        }

                        if ($zoom > 1) {
                            $this->Session->setFlash(__('Image too small'));
                            $this->redirect('/tpl_asset_pages/edit/' . $tplElement['TplElement']['tpl_asset_page_id']);
                            return;
                        }

                        $tplElementImage['TplElementImage']['zoom'] = $zoom;
                        $tplElementImage['TplElementImage']['original_width'] = $imageWidth;
                        $tplElementImage['TplElementImage']['original_height'] = $imageHeight;
                        $tplElementImage['TplElementImage']['top'] = 0;
                        $tplElementImage['TplElementImage']['left'] = 0;
                        $tplElementImage['TplElementImage']['original_id'] = 0;
                        $tplElementImage['TplElementImage']['filepath_hires'] = NULL;
                        $tplElementImage['TplElementImage']['max_zoom_value'] = $this->ElementImage->GetMaxZoom($imageWidth, $imageHeight, $blockWidth, $blockHeight);

                        if ($this->TplElementImage->save($tplElementImage)) {
                            $this->Session->setFlash(__('File successfully saved'));
                        } else {
                            $this->Session->setFlash("Unable to save data");
                        }
                    }else{
                        $this->Session->setFlash("Unable to move file");
                    }
                }
                else {
                    if ($this->request->data['TplElement']['file']['size'] > 0) {
                        $this->Session->setFlash("Unable to save uploaded file");
                    }
                    else {
                        $tplElementImage['TplElementImage']['filepath'] = NULL;
                        $this->TplElementImage->save($tplElementImage);
                        $this->Session->setFlash("Unable to save uploaded file");
                    }

                }
            }
            else {
                $this->Session->setFlash('Unable to create folder');
            }
        }
        $this->redirect('/tpl_asset_pages/edit/'.$tplElement['TplElement']['tpl_asset_page_id']);
    }

enter image description here

1 个答案:

答案 0 :(得分:0)

使用Imagick编写文件修复了问题。

$img = new Imagick($this->request->data['TplElement']['file']['tmp_name']);
$img->setImageFormat(substr(strrchr($tplElementImage['TplElementImage']['original_file_name'],'.'),1));
//stuff
if($img->writeImage($path.DS.$filename)) {
    //stuff
}
//stuff