简体   繁体   中英

move_uploaded_file affects JPG image quality(colors)

I am trying to upload an image in a folder using the move_uploaded_file() function. The image usually is a 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.

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).

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.

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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