Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving jpg to png increases size [duplicate]

I use the following code to save any format of image to png :

$crawl_outfile = 'webss_' . uniqid() . '.png';
imagepng(imagecreatefromstring(file_get_contents($src)),$crawl_outfile);

And it increases the size of 290 KB to 1.7 MB. Cannot understand the reason. Is there any way(parameter) to get smaller image ?

like image 688
Ravi Singh Avatar asked Sep 01 '25 17:09

Ravi Singh


2 Answers

JPEG is a lossy compression format (some detail in the image is lost), where PNG is not. Therefore, the PNG will be larger in file size.

PNG is efficient at compressing some things, like large areas of the same color. JPEG is better at compressing photos.

like image 95
Brad Avatar answered Sep 04 '25 08:09

Brad


PNG stands for Portable Network Graphics. It stores pixels precisely and its compression technique works best for pixel drawings and screenshots with large areas of solid colors. For continuous tone images (i.e., photos) where the color changes just slightly between each pixel, it is not able to compress them very much. The handy OptiPNG or PngCrush tools can reduce the size of a PNG file a bit, but the short answer is that you'll never get a PNG photo down to the size of a JPEG.

JPEG stands for Joint Photographic Experts Group. It is designed for continuous tone images and compresses them extremely well. On the other hand, its lossy compression technique loses color detail that is considered too subtle for humans to notice, and it copes badly with sharp edges.

Additionally note that PNG supports transparency, paletted images, and animation, which JPEG doesn't, but JPEG has more complicated metadata support, and a (rarely supported) lossless mode. So the two formats each have their own quirks. To minimize the file size you'll want to choose the format based on the visual characteristics of the image. Basically, use JPEG for photos and PNG for everything else.

like image 36
Boann Avatar answered Sep 04 '25 08:09

Boann