[wp-trac] [WordPress Trac] #51780: Add ability to easily get path to sized images (such as parameter to get_attached_file() or wp_get_attachment_thumb_file())
WordPress Trac
noreply at wordpress.org
Sun Nov 15 17:27:36 UTC 2020
#51780: Add ability to easily get path to sized images (such as parameter to
get_attached_file() or wp_get_attachment_thumb_file())
---------------------------+-----------------------------
Reporter: paulschreiber | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Media | Version: 5.5.3
Severity: normal | Keywords:
Focuses: |
---------------------------+-----------------------------
Currently, you can call `get_attached_file()` and get the path to the
full-sized version of the image.
However, if you want the path to a smaller variant of the image, you end
up with something like this:
{{{#!php
<?php
$imagedata = wp_get_attachment_metadata( $image_id );
$file_path = get_attached_file( $image_id );
$thumbnail_file = str_replace( wp_basename( $file_path ),
$imagedata['sizes'][ $size ]['file'], $file_path );
}}}
Or like this:
{{{#!php
<?php
$imagedata = image_get_intermediate_size( $image_id, $size );
$upload_directory = wp_get_upload_dir();
$thumbnail_file = $upload_directory['basedir'] . '/' . $imagedata['path'];
}}}
For brevity, these examples omit the null dereference checks, which triple
the size of the code needed.
This is similar to the behaviour of `wp_get_attachment_thumb_file()`, but
that function hard-codes `thumb` as the only size.
It would be very helpful if this existed:
{{{#!php
<?php
get_attached_file( int $attachment_id, bool $unfiltered = false, string
$size = false )
}}}
Or this existed:
{{{#!php
<?php
wp_get_attachment_thumb_file( int $post_id, string $size = false )
}}}
See related tickets #33959 (from 2015) and #17262 (from 2011).
I'm happy to make a patch for either/both of these approaches, but don't
know which one you prefer, and don't want this to languish for 11 years.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/51780>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list