Skip to content

Instantly share code, notes, and snippets.

@rhcarlosweb
Last active April 14, 2023 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhcarlosweb/01287060b80813fce8ed9d97672664b8 to your computer and use it in GitHub Desktop.
Save rhcarlosweb/01287060b80813fce8ed9d97672664b8 to your computer and use it in GitHub Desktop.
Cut image on the fly by image ID, WordPress
<?php
function get_thumbnail($attachment_id = null, $width = null, $height = null, $crop = true, $url = false, $alt = "") {
// global $post variable is used later in the function to check if the current post has a thumbnail image
global $post;
// if an attachment ID is provided
if ($attachment_id) {
// get the path to the file on the server
$path = get_attached_file($attachment_id);
// if the file does not exist, return false
if (!file_exists($path)) {
return false;
}
// get the base URL for the uploads directory and the directory of the file
$upload = wp_upload_dir();
$path_info = pathinfo($path);
$base_url = $upload['baseurl'] . str_replace($upload['basedir'], '', $path_info['dirname']);
// get the attachment metadata
$meta = wp_get_attachment_metadata($attachment_id);
// check if a size with the desired width and height already exists
foreach ($meta['sizes'] as $size) {
if ($size['width'] == $width && $size['height'] == $height) {
// if a size with the desired dimensions exists, use it
$img_src = "{$base_url}/{$size['file']}";
break;
}
}
// if a size with the desired dimensions does not already exist
if (!isset($img_src)) {
// try to create a new size with the desired dimensions
$resized = image_make_intermediate_size($path, $width, $height, $crop);
// if the resizing was successful and not an error
if ($resized && !is_wp_error($resized)) {
// add the new size to the attachment metadata
$key = sprintf('resized-%dx%d', $width, $height);
$meta['sizes'][$key] = $resized;
wp_update_attachment_metadata($attachment_id, $meta);
// use the new size
$img_src = "{$base_url}/{$resized['file']}";
} else {
// if the resizing failed, use the original image
$img_src = "{$base_url}/{$path_info['basename']}";
}
}
}
// if no attachment ID is provided, but the global $post variable is set
elseif (!empty($post)) {
// if the post has a thumbnail image
if (has_post_thumbnail($post->ID)) {
// get the thumbnail ID and generate the thumbnail
$attachment_id = get_post_thumbnail_id($post->ID);
$img_src = get_thumbnail($attachment_id, $width, $height, $crop, true);
}
// if no thumbnail image is set for the post
else {
// try to find an image in the post content
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
// if an image is found
if ($output >= 1) {
// use the first image found
$img_src = $matches[1][0];
} else {
// if no image is found in the post, use a placeholder image
$img_src = "https://via.placeholder.com/${width}x${height}";
}
}
}
// if no attachment ID is provided and no global $post variable is set
else {
// use a placeholder image
$img_src = "https://via.placeholder.com/${width}x${height}";
}
// if the $url parameter is true, return the URL of the image
if ($url) {
return $img_src;
}
// if the $url parameter is not true, return an <img> element with the image's URL as the src attribute
else {
return '<img src="' . $img_src . '" alt="' . $alt . '" height="' . $height . '" width="' . $width . '">';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment