0
votes

I have been trying to use imagemagic in my codeigniter API. The problem is that below code works fine with my jpg image compression. When I upload ".jpg" image of 2MB size it successfully resizes it to perfect 80kb file on server but when I use ".png" extension the size goes down but not that much it still gives me image size of 300kb and I observed that with png format "quality" does not effect. I tried putting quality 100 and 50 but still same result of big image size for ".png".

And also that if I remove "width" and "height" config parameter than only "quality" does not affect the image file it uploads image of very big size... no image file size compression bases on "quality" parameter.

Can anyone help where I am making mistake or missing something???

My API:

public function multi_image_upload()
    {
        $aa = array();
        if (!empty($_FILES['images'])) {
            foreach ($_FILES['images']['name'] as $key => $image) {
                $extFile = $_FILES['images']['name'][$key];
                $ext = pathinfo($extFile, PATHINFO_EXTENSION);
                $nm = date('ymdhis') . '-bingo' . rand(11111, 99999);
                $filnm = $nm . ".png";
                $file = APPPATH . '../upld/biz/' . $filnm;
                $newname = $filnm;
                $source_image = $file;
                if (move_uploaded_file($_FILES['images']['tmp_name'][$key], $file)) {
                    $this->fun->imageupload("upld/biz/", $newname, $source_image);
                    if (file_exists($file)) {
                        unlink($file);
                    }
                }
            }
            $res = $this->responce(false, $aa);
        } else {
            $res = $this->responce(true, "Please select image");
        }
        echo $res;
    }

IMAGE UPLOAD MODEL:

public function imageupload($url, $newname, $source_image)
        {
            $path = APPPATH . '../' . $url;
            $imgsz = array(256,700);
            foreach ($imgsz as $imxs) {
                $sizefolder = $imxs . "x" . $imxs . "/";
                if (!file_exists($path . $sizefolder)) {
                    mkdir($path . $sizefolder, 0777);
                }
                $size = getimagesize($source_image);
                $filekb = filesize($source_image); //            $config['image_library'] = 'gd2';
                $config['image_library'] = 'ImageMagick';
                $config['library_path'] = '/usr/bin/';
                $config['overwrite'] = TRUE;
                $config['source_image'] = $source_image;
                $config['new_image'] = $path . $sizefolder . $newname;
                $config['quality'] = 100;
                if ($size[1] > $size[0]) {
                    $config['height'] = $imxs;
                } else {
                    $config['width'] = $imxs;
                }
                $this->load->library('image_lib');
                $this->image_lib->initialize($config);
                $this->image_lib->resize();
            }
        }
1
Does this answer your question? Compress a PNG image with ImageMagickJirka Hrazdil
quality in ImageMagick uses different value ranges for JPG than fo PNG. See imagemagick.org/script/command-line-options.php#qualityfmw42

1 Answers

0
votes

JPEG is a lossy compression, meaning that you can limit the size of the output image by setting quality settings, resulting in lower quality image and also lower file size. PNG is a lossless compression, so you cannot limit quality - it always stays the same. But you can limit compression - the effort that Imagemagick puts into making the PNG file smaller - but like with e. g. ZIP compression the output size is largely dependent on data type - and most images do not compress particularly well.

Also see Compress a PNG image with ImageMagick for some more options on limiting PNG file sizes.