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 |
/** * 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; }
/** * Get one image from a specified post in the following order: * Featured Image, first attached image, first image from the_content HTML * * @param int $id, The post ID to check * @param string $size The image size to return, defaults to 'featured-home-big'. * @param string|array $attr Optional. Query string or array of attributes. * @return string $thumb Thumbnail image with markup. */ function boardwalk_get_image($post_id = null, $size = 'post-thumbnail', $attr = '') { $post_id = null === $post_id ? get_the_ID() : $post_id; if ('' != get_the_post_thumbnail($post_id)) { return get_the_post_thumbnail($post_id, $size, $attr); } $attached_images = get_attached_media('image'); if (!empty($attached_images)) { $first_attached_image = array_shift($attached_images); return wp_get_attachment_image($first_attached_image->ID, $size, false, $attr); } if (class_exists('Jetpack_PostImages')) { global $_wp_additional_image_sizes; $args = array('from_thumbnail' => false, 'from_slideshow' => true, 'from_gallery' => true, 'from_attachment' => false); $image = Jetpack_PostImages::get_image($post_id, $args); if (!empty($image)) { $image['width'] = ''; $image['height'] = ''; if (array_key_exists($size, $_wp_additional_image_sizes)) { $image['width'] = $_wp_additional_image_sizes[$size]['width']; $image['height'] = $_wp_additional_image_sizes[$size]['height']; } $image_src = Jetpack_PostImages::fit_image_url($image['src'], $image['width'], $image['height']); return '<img src="' . esc_url($image_src) . '" title="' . esc_attr(strip_tags(get_the_title())) . '" class="attachment-' . esc_attr($size) . ' wp-post-image" />'; } } return false; }