Jetpack_PostImages::fit_image_url PHP Method

fit_image_url() static public method

Takes an image URL and pixel dimensions then returns a URL for the resized and croped image.
static public fit_image_url ( string $src, $width, $height ) : string
$src string
return string Transformed image URL
    static function fit_image_url($src, $width, $height)
    {
        $width = (int) $width;
        $height = (int) $height;
        // Umm...
        if ($width < 1 || $height < 1) {
            return $src;
        }
        // See if we should bypass WordPress.com SaaS resizing
        if (has_filter('jetpack_images_fit_image_url_override')) {
            /**
             * Filters the image URL used after dimensions are set by Photon.
             *
             * @since 3.3.0
             *
             * @param string $src Image URL.
             * @param int $width Image width.
             * @param int $width Image height.
             */
            return apply_filters('jetpack_images_fit_image_url_override', $src, $width, $height);
        }
        // If WPCOM hosted image use native transformations
        $img_host = parse_url($src, PHP_URL_HOST);
        if ('.files.wordpress.com' == substr($img_host, -20)) {
            return add_query_arg(array('w' => $width, 'h' => $height, 'crop' => 1), $src);
        }
        // Use Photon magic
        if (function_exists('jetpack_photon_url')) {
            return jetpack_photon_url($src, array('resize' => "{$width},{$height}"));
        }
        // Arg... no way to resize image using WordPress.com infrastructure!
        return $src;
    }

Usage Example

 /**
  * Generates the thumbnail image to be used for the post. Uses the
  * image as returned by Jetpack_PostImages::get_image()
  *
  * @param int $post_id
  * @uses self::get_options, apply_filters, Jetpack_PostImages::get_image, Jetpack_PostImages::fit_image_url
  * @return string
  */
 protected function _generate_related_post_image_params($post_id)
 {
     $options = $this->get_options();
     $image_params = array('src' => '', 'width' => 0, 'height' => 0);
     if (!$options['show_thumbnails']) {
         return $image_params;
     }
     $thumbnail_size = apply_filters('jetpack_relatedposts_filter_thumbnail_size', array('width' => 350, 'height' => 200));
     if (!is_array($thumbnail_size)) {
         $thumbnail_size = array('width' => (int) $thumbnail_size, 'height' => (int) $thumbnail_size);
     }
     // Try to get post image
     if (class_exists('Jetpack_PostImages')) {
         $img_url = '';
         $post_image = Jetpack_PostImages::get_image($post_id, $thumbnail_size);
         if (is_array($post_image)) {
             $img_url = $post_image['src'];
         } elseif (class_exists('Jetpack_Media_Summary')) {
             $media = Jetpack_Media_Summary::get($post_id);
             if (is_array($media) && !empty($media['image'])) {
                 $img_url = $media['image'];
             }
         }
         if (!empty($img_url)) {
             $image_params['width'] = $thumbnail_size['width'];
             $image_params['height'] = $thumbnail_size['height'];
             $image_params['src'] = Jetpack_PostImages::fit_image_url($img_url, $thumbnail_size['width'], $thumbnail_size['height']);
         }
     }
     return $image_params;
 }
All Usage Examples Of Jetpack_PostImages::fit_image_url