strip_image_dimensions_maybe() protected static method

Checks if the file exists before it passes the file to photon
protected static strip_image_dimensions_maybe ( string $src ) : string
$src string The image URL
return string
Ejemplo n.º 1
0
 /**
  * Filters an array of image `srcset` values, replacing each URL with its Photon equivalent.
  *
  * @since 3.8.0
  * @since 4.0.4 Added automatically additional sizes beyond declared image sizes.
  * @param array $sources An array of image urls and widths.
  * @uses self::validate_image_url, jetpack_photon_url, Jetpack_Photon::parse_from_filename
  * @uses Jetpack_Photon::strip_image_dimensions_maybe, Jetpack::get_content_width
  * @return array An array of Photon image urls and widths.
  */
 public function filter_srcset_array($sources, $size_array, $image_src, $image_meta)
 {
     $upload_dir = wp_upload_dir();
     foreach ($sources as $i => $source) {
         if (!self::validate_image_url($source['url'])) {
             continue;
         }
         /** This filter is already documented in class.photon.php */
         if (apply_filters('jetpack_photon_skip_image', false, $source['url'], $source)) {
             continue;
         }
         $url = $source['url'];
         list($width, $height) = Jetpack_Photon::parse_dimensions_from_filename($url);
         // It's quicker to get the full size with the data we have already, if available
         if (isset($image_meta['file'])) {
             $url = trailingslashit($upload_dir['baseurl']) . $image_meta['file'];
         } else {
             $url = Jetpack_Photon::strip_image_dimensions_maybe($url);
         }
         $args = array();
         if ('w' === $source['descriptor']) {
             if ($height && $source['value'] == $width) {
                 $args['resize'] = $width . ',' . $height;
             } else {
                 $args['w'] = $source['value'];
             }
         }
         $sources[$i]['url'] = jetpack_photon_url($url, $args);
     }
     /**
      * At this point, $sources is the original srcset with Photonized URLs.
      * Now, we're going to construct additional sizes based on multiples of the content_width.
      * This will reduce the gap between the largest defined size and the original image.
      */
     /**
      * Filter the multiplier Photon uses to create new srcset items.
      * Return false to short-circuit and bypass auto-generation.
      *
      * @module photon
      *
      * @since 4.0.4
      *
      * @param array|bool $multipliers Array of multipliers to use or false to bypass.
      */
     $multipliers = apply_filters('jetpack_photon_srcset_multipliers', array(2, 3));
     $url = trailingslashit($upload_dir['baseurl']) . $image_meta['file'];
     if (is_array($multipliers) && !apply_filters('jetpack_photon_skip_image', false, $url, null) && isset($image_meta['width']) && isset($image_meta['height']) && isset($image_meta['file']) && isset($size_array[0]) && isset($size_array[1])) {
         $fullwidth = $image_meta['width'];
         $fullheight = $image_meta['height'];
         $reqwidth = $size_array[0];
         $reqheight = $size_array[1];
         $constrained_size = wp_constrain_dimensions($fullwidth, $fullheight, $reqwidth);
         $expected_size = array($reqwidth, $reqheight);
         if (abs($constrained_size[0] - $expected_size[0]) <= 1 && abs($constrained_size[1] - $expected_size[1]) <= 1) {
             $crop = 'soft';
             $base = Jetpack::get_content_width() ? Jetpack::get_content_width() : 1000;
             // Provide a default width if none set by the theme.
         } else {
             $crop = 'hard';
             $base = $reqwidth;
         }
         $currentwidths = array_keys($sources);
         $newsources = null;
         foreach ($multipliers as $multiplier) {
             $newwidth = $base * $multiplier;
             foreach ($currentwidths as $currentwidth) {
                 // If a new width would be within 100 pixes of an existing one or larger than the full size image, skip.
                 if (abs($currentwidth - $newwidth) < 50 || $newwidth > $fullwidth) {
                     continue 2;
                     // Back to the foreach ( $multipliers as $multiplier )
                 }
             }
             // foreach ( $currentwidths as $currentwidth ){
             if ('soft' == $crop) {
                 $args = array('w' => $newwidth);
             } else {
                 // hard crop, e.g. add_image_size( 'example', 200, 200, true );
                 $args = array('zoom' => $multiplier, 'resize' => $reqwidth . ',' . $reqheight);
             }
             $newsources[$newwidth] = array('url' => jetpack_photon_url($url, $args), 'descriptor' => 'w', 'value' => $newwidth);
         }
         // foreach ( $multipliers as $multiplier )
         if (is_array($newsources)) {
             $sources = array_merge($sources, $newsources);
         }
     }
     // if ( isset( $image_meta['width'] ) && isset( $image_meta['file'] ) )
     return $sources;
 }
 /**
  * Filters an array of image `srcset` values, replacing each URL with its Photon equivalent.
  *
  * @since 3.8.0
  * @param array $sources An array of image urls and widths.
  * @uses self::validate_image_url, jetpack_photon_url
  * @return array An array of Photon image urls and widths.
  */
 public function filter_srcset_array($sources)
 {
     foreach ($sources as $i => $source) {
         if (!self::validate_image_url($source['url'])) {
             continue;
         }
         $url = Jetpack_Photon::strip_image_dimensions_maybe($source['url']);
         $args = array();
         if ('w' === $source['descriptor']) {
             $args['w'] = $source['value'];
         }
         $sources[$i]['url'] = jetpack_photon_url($url, $args);
     }
     return $sources;
 }
Ejemplo n.º 3
0
 /**
  * Filters an array of image `srcset` values, replacing each URL with its Photon equivalent.
  *
  * @since 3.8.0
  * @param array $sources An array of image urls and widths.
  * @uses self::validate_image_url, jetpack_photon_url
  * @return array An array of Photon image urls and widths.
  */
 public function filter_srcset_array($sources, $size_array, $image_src, $image_meta)
 {
     $upload_dir = wp_upload_dir();
     foreach ($sources as $i => $source) {
         if (!self::validate_image_url($source['url'])) {
             continue;
         }
         $url = $source['url'];
         list($width, $height) = Jetpack_Photon::parse_dimensions_from_filename($url);
         // It's quicker to get the full size with the data we have already, if available
         if (isset($image_meta['file'])) {
             $url = trailingslashit($upload_dir['baseurl']) . $image_meta['file'];
         } else {
             $url = Jetpack_Photon::strip_image_dimensions_maybe($url);
         }
         $args = array();
         if ('w' === $source['descriptor']) {
             if ($height && $source['value'] == $width) {
                 $args['resize'] = $width . ',' . $height;
             } else {
                 $args['w'] = $source['value'];
             }
         }
         $sources[$i]['url'] = jetpack_photon_url($url, $args);
     }
     return $sources;
 }