[wp-trac] [WordPress Trac] #34955: The choices of images to add to srcset shouldn't be via a ratio

WordPress Trac noreply at wordpress.org
Sat Dec 12 15:23:35 UTC 2015


#34955: The choices of images to add to srcset shouldn't be via a ratio
--------------------------------------+------------------------
 Reporter:  smerriman                 |       Owner:  joemcgill
     Type:  defect (bug)              |      Status:  assigned
 Priority:  normal                    |   Milestone:  4.4.1
Component:  Media                     |     Version:  4.4
 Severity:  normal                    |  Resolution:
 Keywords:  has-patch has-unit-tests  |     Focuses:
--------------------------------------+------------------------

Comment (by jaspermdegroot):

 I have tested 34955.2.diff with all cases mentioned in this ticket,
 #34931, and #34810. It fixes the cases where images currently don't get a
 `srcset` or an incomplete one, and I didn't see sources in the `srcset`
 attributes that shouldn't have been included.

 I also tested with some other image sizes. The only problem I could find
 has to do with the 1px difference that we allow.
 For example: If you set 400x320px hard cropped as post-thumbnail size and
 upload a 800x642px image, `wp_constrain_dimensions()` will return an array
 with 400 and 321 which is only 1px difference so we include it in the
 `srcset` and it will be used on HD screens. Currently we would not include
 this image because the aspect ratio difference is 0.0025.

 As long as you don't override the `width` and `height` attributes in your
 theme CSS the image will still be shown at 400x320px. But many themes,
 including the WordPress default themes, set {{{img { max-width: 100%;
 height: auto; } }}} which will make the image element expand to 400x321px.
 If your theme layout depends on the exact height of an image you will have
 to make sure you don't override the `height` attribute.

 I think that no matter what solution we use, we will always have to allow
 a small difference somewhere. Using {{{wp_constrain_dimensions()}}} seems
 to be the best solution, assuming it doesn't affect performance too much,
 but I suggest that we make an exception for square images because for
 those it's easy to avoid including a source that is one pixel off.

 I also suggest to change variable {{{$image}}} to {{{$image_size}}} in the
 {{{foreach}}} loop to make the code easier to read.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/34955#comment:10>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list