PHP PNG images are being uploaded with mime “image/jpeg” on KCFINDER 3.12

我与影子孤独终老i 提交于 2019-12-09 06:03:26
Killrawr

This was pretty confusing issue, I was having. But I have managed to fix it.

Steps involved are as follows

Open the file kcfinder/core/class/uploader.php find the function imageResize. Then modify the this.output code.

Before:

    // WRITE TO FILE
    return $img->output("jpeg", array(
        'file' => $file,
        'quality' => $this->config['jpegQuality']
    ));

After:

    // Check the EXTENSION OF THIS FILE
    if($file && is_string($file) && file_exists($file)) {
        $file_imgsize = @getimagesize($file);
        // Get the EXPECTED EXTENSION from this MIME
        if($file_imgsize && !empty($file_imgsize)) {
            $fileMimeInteger = $file_imgsize[2];
            $outputFileExtension = @image_type_to_extension($fileMimeInteger);
            $outputFileExtension = str_replace('.', '', $outputFileExtension);
        }
    }

    // Force Jpeg
    if(!$outputFileExtension) {
        $outputFileExtension = "jpeg";
    }

    // WRITE TO FILE
    return $img->output($outputFileExtension, array(
        'file' => $file,
        'quality' => $this->config['jpegQuality']
    ));

Function:

protected function imageResize($image, $file=null) {

        if (!($image instanceof image)) {
            $img = image::factory($this->imageDriver, $image);
            if ($img->initError) return false;
            $file = $image;
        } elseif ($file === null) {
             return false;
        }
        else {
            $img = $image;
        }

        $orientation = 1;
        if (function_exists("exif_read_data")) {
            $orientation = @exif_read_data($file);
            $orientation = isset($orientation['Orientation']) ? $orientation['Orientation'] : 1;
        }

        // IMAGE WILL NOT BE RESIZED WHEN NO WATERMARK AND SIZE IS ACCEPTABLE
        if ((
                !isset($this->config['watermark']['file']) ||
                (!strlen(trim($this->config['watermark']['file'])))
            ) && (
                (
                    !$this->config['maxImageWidth'] &&
                    !$this->config['maxImageHeight']
                ) || (
                    ($img->width <= $this->config['maxImageWidth']) &&
                    ($img->height <= $this->config['maxImageHeight'])
                )
            ) &&
            ($orientation == 1)
        )
            return true;

        // PROPORTIONAL RESIZE
        if ((!$this->config['maxImageWidth'] || !$this->config['maxImageHeight'])) {

            if ($this->config['maxImageWidth'] &&
                ($this->config['maxImageWidth'] < $img->width)
            ) {
                $width = $this->config['maxImageWidth'];
                $height = $img->getPropHeight($width);

            } elseif (
                $this->config['maxImageHeight'] &&
                ($this->config['maxImageHeight'] < $img->height)
            ) {
                $height = $this->config['maxImageHeight'];
                $width = $img->getPropWidth($height);
            }

            if (isset($width) && isset($height) && !$img->resize($width, $height))
                return false;

        // RESIZE TO FIT
        } elseif (
            $this->config['maxImageWidth'] && $this->config['maxImageHeight'] &&
            !$img->resizeFit($this->config['maxImageWidth'], $this->config['maxImageHeight'])
        )
            return false;

        // AUTO FLIP AND ROTATE FROM EXIF
        if ((($orientation == 2) && !$img->flipHorizontal()) ||
            (($orientation == 3) && !$img->rotate(180)) ||
            (($orientation == 4) && !$img->flipVertical()) ||
            (($orientation == 5) && (!$img->flipVertical() || !$img->rotate(90))) ||
            (($orientation == 6) && !$img->rotate(90)) ||
            (($orientation == 7) && (!$img->flipHorizontal() || !$img->rotate(90))) ||
            (($orientation == 8) && !$img->rotate(270))
        )
            return false;
        if (($orientation >= 2) && ($orientation <= 8) && ($this->imageDriver == "imagick"))
            try {
                $img->image->setImageProperty('exif:Orientation', "1");
            } catch (\Exception $e) {}

        // WATERMARK
        if (isset($this->config['watermark']['file']) &&
            is_file($this->config['watermark']['file'])
        ) {
            $left = isset($this->config['watermark']['left'])
                ? $this->config['watermark']['left'] : false;
            $top = isset($this->config['watermark']['top'])
                ? $this->config['watermark']['top'] : false;
            $img->watermark($this->config['watermark']['file'], $left, $top);
        }

        // Check the EXTENSION OF THIS FILE
        if($file && is_string($file) && file_exists($file)) {
            $file_imgsize = @getimagesize($file);
            // Get the EXPECTED EXTENSION from this MIME
            if($file_imgsize && !empty($file_imgsize)) {
                $fileMimeInteger = $file_imgsize[2];
                $outputFileExtension = @image_type_to_extension($fileMimeInteger);
                $outputFileExtension = str_replace('.', '', $outputFileExtension);
            }
        }

        // Force Jpeg
        if(!$outputFileExtension) {
            $outputFileExtension = "jpeg";
        }

        // WRITE TO FILE
        return $img->output($outputFileExtension, array(
            'file' => $file,
            'quality' => $this->config['jpegQuality']
        ));

    }

Next, the file class_image_gd.php in path kcfinder/lib/class_image_gd.php look for the function output_png make changes to how the quality is worked out (this fixes a PHP related error, with "Compression must be between 0-9").

protected function output_png(array $options=array()) {
    $file = isset($options['file']) ? $options['file'] : null;
    $quality = isset($options['quality']) ? $options['quality'] : null;
    $filters = isset($options['filters']) ? $options['filters'] : null;

    if (($file === null) && !headers_sent()) {
        header("Content-Type: image/png");
    }

    // Compression must be between 0-9 - 2/02/2016
    if($quality && is_numeric($quality)) {
        $quality = $quality < 100 ? round(($quality / 100) * 10) : null; 
    } else {
        $quality = null;
    }

    return imagepng($this->image, $file, $quality, $filters);
}

Result:


(source: iforce.co.nz)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!