Make WordPress Core

Opened 9 years ago

Closed 8 years ago

Last modified 8 years ago

#37479 closed enhancement (wontfix)

Ambiguous use of first parameter in wp_calculate_image_sizes

Reported by: smerriman's profile smerriman Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.6
Component: Media Keywords:
Focuses: Cc:

Description

The wp_calculate_image_sizes function in WordPress takes $size as a parameter, documented as:

Image size. Accepts any valid image size, or an array of width and height values in pixels (in that order). Default 'thumbnail'.

The code in this function tests if the parameter is a string, and calculates the width if so; otherwise simply using the first value of the array.

However, every single call to this function passes an array, not the size name. For example, wp_get_attachment_image takes the size name, and converts it to an array before calling wp_calculate_image_sizes. No call is ever made with a size name string.

I would like to request the parameter is enforced to be an array. The reason for doing so is in the wp_calculate_image_sizes filter. To be strictly correct, any uses of this filter need to handle a string, thus need to duplicate all of the code for extracting a width. A theme like twentysixteen doesn't do this - it assumes the parameter is an array and uses $size[0] as the width.

Alternately, the calculated width could be passed as an additional parameter to the filter, so that it does not need recalculating.

Change History (3)

#1 @joemcgill
8 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

Hi @smerriman,

Thank you for the report.

The reason this function was designed to accept $size as both a string and an array is to be consistent with how $size is used in other image functions, like wp_get_attachment_image(). Internally in core, we are using an array for performance reasons (to avoid the extra metadata lookup if I recall correctly), but this function could also be used by theme or plugin authors to generate custom markup, in which case, being able to pass the named size to wp_calculate_image_sizes() is sometimes preferred.

#2 @smerriman
8 years ago

In that case, how about the alternative suggested in the last sentence?

While using the named size *may* provide some benefit to people using the *function*, passing the calculated width is virtually *guaranteed* to be required by anyone using the *filter*. Currently you need to duplicate all of the code for this calculation that was done in core (or, copy twentysixteen's method which will break if the named size was indeed used).

#3 @smerriman
8 years ago

(If not, this need to be filed as a bug in twentysixteen.)

Note: See TracTickets for help on using tickets.