简体   繁体   English

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

[英]move_uploaded_file affects JPG image quality(colors)

I am trying to upload an image in a folder using the move_uploaded_file() function. 我正在尝试使用move_uploaded_file()函数将图像上传到文件夹中。 The image usually is a JPG. 该图像通常是JPG。

The problem is that the moving process somehow affects the image quality. 问题在于移动过程会以某种方式影响图像质量。 To me mor eprecises, the colors are not the same between the file that i want to upload and the uploaded file. 对我来说,我要上传的文件和上传的文件之间的颜色并不相同。

So far i think the problem lies withing the move_uploaded_file() function, as the colors of the temp file are correct. 到目前为止,我认为问题在于move_uploaded_file()函数,因为临时文件的颜色正确。

Here is my code and the images before and after the upload(the top one is before uploading and the bottom one ois after upload). 这是我的代码以及上传之前和之后的图像(最上面的是上传之前,最下面的是ois)。

This kind of behaviour isn't accepted as the results need to be print ready. 由于需要打印结果,因此不接受这种行为。

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

在此处输入图片说明

The problem has been fixed using Imagick to write the file. 使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM