WordPress.org

Make WordPress Core

Opened 11 months ago

Last modified 4 months ago

#24380 new enhancement

Missing Compression Parameter in WP_Image_Editor_GD

Reported by: MuViMoTV Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 3.5.1
Component: Media Keywords: has-patch 3.9-early commit
Focuses: Cc:

Description (last modified by SergeyBiryukov)

Setting the image quality parameter has no effect on png files.

Going through the wp-includes/class-wp-image-editor-gd.php

I noticed that the compression parameter for the imagepng function call is missing.

the current quality parameter only affects jpeg files.

for jpeg, quality goes from 0->100 from bad to good

for png, from 0->9 from good to bad.

in the elseif block starting at line 337 i have added a variable called compression and changed the code as follow:

elseif ( 'image/png' == $mime_type ) {
			// convert from full colors to index colors, like original PNG.
			if ( function_exists('imageistruecolor') && ! imageistruecolor( $image ) )
				imagetruecolortopalette( $image, false, imagecolorstotal( $image ) );

			$compression = -((9/100*$this->quality)-9);
			
			if ( ! $this->make_image( $filename, 'imagepng', array( $image, $filename, $compression ) ) )
				return new WP_Error( 'image_save_error', __('Image Editor Save Failed') );
		}

This convert the scale 0->100 into a 0->9 scale that matches the parameters from imaging.

This gives back control to the user on image quality for png files...

Attachments (4)

class-wp-image-editor-gd.php (10.4 KB) - added by MuViMoTV 11 months ago.
class-wp-image-editor-gd.diff (777 bytes) - added by MuViMoTV 11 months ago.
class-wp-image-editor-gd.2.diff (818 bytes) - added by markoheijnen 6 months ago.
Spacing and add comment
24380.diff (817 bytes) - added by SergeyBiryukov 5 months ago.

Download all attachments as: .zip

Change History (25)

comment:1 SergeyBiryukov11 months ago

  • Component changed from Editor to Media
  • Description modified (diff)

comment:2 markoheijnen11 months ago

  • Type changed from defect (bug) to feature request

I thought we did added it but it was never in core before. Also you need to add a patch and not the whole file.

comment:3 MuViMoTV11 months ago

i wouldn't have a clue how to do a "patch" this is my first bug report and i have zero experience in team work...
can you point me in the right direction?
thanks

comment:5 MuViMoTV11 months ago

Hmmm well it doesn't seems to work...

I checked out the trunk version

svn co http://core.svn.wordpress.org/trunk/ .

Then changed the file manually and finally issued this command

svn diff class-wp-image-editor-gd.php class-wp-image-editor-gd.diff

and got the following :

svn: E150000: '/Users/MuViMoTV/wordpress_trunk/class-wp-image-editor-gd.php' is not under version control

comment:6 markoheijnen11 months ago

That file is in the root and not in wp-includes.

So it should be: svn diff wp-includes/class-wp-image-editor-gd.php > class-wp-image-editor-gd.diff

But I always do: svn diff > class-wp-image-editor-gd.diff

comment:7 MuViMoTV11 months ago

sweet! thank you so much for that...

I finally managed to get it to work...

i feel a bit stupid though having missed the folder part...

I've attached the diff file...

comment:8 SergeyBiryukov11 months ago

  • Keywords has-patch added; needs-patch removed

comment:9 SergeyBiryukov11 months ago

  • Type changed from feature request to enhancement

markoheijnen6 months ago

Spacing and add comment

comment:10 markoheijnen6 months ago

  • Milestone changed from Awaiting Review to 3.8

Moving to 3.8. Updated patch to add some spacing and comment.

comment:11 valeriosza6 months ago

  • Keywords 2nd-opinion needs-testing added

comment:12 markoheijnen6 months ago

  • Keywords needs-testing removed

This isn't something that can be tested well.

comment:13 nacin5 months ago

Is this ready to go? This math is confusing, a comment would be useful.

comment:14 markoheijnen5 months ago

I would say yes. Math is a bit weird but thats the logic of GD. So my comment is to short I guess ;)

If quality is 100 - no compression
- ( ( 0.09 * 100 ) - 9 ) = 0

If quality is 0 - with full compression
- ( ( 0.09 * 0 ) - 9 ) = 9

comment:15 markoheijnen5 months ago

Will add a patch with a better comment tomorrow that also rounds the result since the documentation described the field as an integer

comment:16 SergeyBiryukov5 months ago

Looks like this can be simplified to 9 - 0.09 * $this->quality.

SergeyBiryukov5 months ago

comment:17 SergeyBiryukov5 months ago

  • Keywords commit added; 2nd-opinion removed

comment:18 follow-up: markoheijnen5 months ago

Good to me. I guess we don't need better documentation

Version 0, edited 5 months ago by markoheijnen (next)

comment:19 in reply to: ↑ 18 DrewAPicture5 months ago

Replying to markoheijnen:

Good to me. I guess we don't need better documentation now since the code is now better to read

We can *always* improve documentation :)

comment:20 nacin4 months ago

  • Milestone changed from 3.8 to Future Release

Per IRC, moving to 3.9 for this enhancement.

I think this could still benefit from a few lines of documentation that explains what it is doing to $this->quality. Seems like it is converting 0-100 (or 1-100?) to 9-1 (or 9-0, or 1-9, or 0-9)? These are magic numbers and it would be nice to know why it needs to do this math.

comment:21 SergeyBiryukov4 months ago

  • Keywords 3.9-early added
Note: See TracTickets for help on using tickets.