PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 10, 2022

[FIXED] Why do images saved to file differ in size from resources with imagejpeg()?

 October 10, 2022     compression, gd, image, image-processing, php     No comments   

Issue

I'm generating thumbnails from images automatically with the PHP GD library.
I then save the resized image to a file using imagejpeg() with 95% quality.

When I output this file using echo file_get_contents($file), the filesize of the image is always about 2.5 - 4 times as large compared to when I directly output the resized image to the browser using imagejpeg($resource_handle).

Google PageSpeed also tells me images could be compressed by 75%, which confirms the 1/4 ratio noticed.

What could be the reason for this?


Solution

I found the manual (http://php.net/manual/en/function.imagejpeg.php) mentions that the default (without passing the quality argument) quality setting is around 75%.

This explains why outputting an image directly without any other arguments passed to imagejpeg() results in a smaller image then when setting a quality higher than 75 manually.



Answered By - Bastiaan ten Klooster
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing