WordPress.org

Make WordPress Core

Changeset 22850


Ignore:
Timestamp:
11/26/2012 10:46:56 PM (7 years ago)
Author:
ryan
Message:

Make wp_prepare_attachment_for_js() compatible with plugins that disable all intermediate image sizes (so no images are created on upload), and then create them on the fly using the image_downsize filter (say, with a URL that can dynamically create images on the fly).

Props nacin
fixes #22598

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/media.php

    r22846 r22850  
    13321332    if ( $meta && 'image' === $type ) {
    13331333        $sizes = array();
    1334         $base_url = str_replace( wp_basename( $attachment_url ), '', $attachment_url );
    1335 
    1336         if ( isset( $meta['sizes'] ) ) {
    1337             foreach ( $meta['sizes'] as $slug => $size ) {
    1338                 $sizes[ $slug ] = array(
    1339                     'height'      => $size['height'],
    1340                     'width'       => $size['width'],
    1341                     'url'         => $base_url . $size['file'],
    1342                     'orientation' => $size['height'] > $size['width'] ? 'portrait' : 'landscape',
     1334        $possible_sizes = apply_filters( 'image_size_names_choose', array(
     1335            'thumbnail' => __('Thumbnail'),
     1336            'medium'    => __('Medium'),
     1337            'large'     => __('Large'),
     1338            'full'      => __('Full Size'),
     1339        ) );
     1340        unset( $possible_sizes['full'] );
     1341
     1342        // Loop through all potential sizes that may be chosen. Try to do this with some efficiency.
     1343        // First: run the image_downsize filter. If it returns something, we can use its data.
     1344        // If the filter does not return something, then image_downsize() is just an expensive
     1345        // way to check the image metadata, which we do second.
     1346        foreach ( $possible_sizes as $size => $label ) {
     1347            if ( $downsize = apply_filters( 'image_downsize', false, $attachment->ID, $size ) ) {
     1348                if ( ! $downsize[3] )
     1349                    continue;
     1350                $sizes[ $size ] = array(
     1351                    'height'      => $downsize[2],
     1352                    'width'       => $downsize[1],
     1353                    'url'         => $downsize[0],
     1354                    'orientation' => $downsize[2] > $downsize[1] ? 'portrait' : 'landscape',
     1355                );
     1356            } elseif ( isset( $meta['sizes'][ $size ] ) ) {
     1357                if ( ! isset( $base_url ) )
     1358                    $base_url = str_replace( wp_basename( $attachment_url ), '', $attachment_url );
     1359
     1360                // Nothing from the filter, so consult image metadata if we have it.
     1361                $size_meta = $meta['sizes'][ $size ];
     1362                $sizes[ $size ] = array(
     1363                    'height'      => $size_meta['height'],
     1364                    'width'       => $size_meta['width'],
     1365                    'url'         => $base_url . $size_meta['file'],
     1366                    'orientation' => $size_meta['height'] > $size_meta['width'] ? 'portrait' : 'landscape',
    13431367                );
    13441368            }
Note: See TracChangeset for help on using the changeset viewer.