0

i have the url of an image (full size). During the upload it already generates a smaller version (i added this in functions). How can i get this cropped version when i have the full size url?

The Image is not in a post so i can't use post-thumbanil() The image is in a slider on the Homepage

1

Get the attachment ID for the image, then get the post meta field _wp_attachment_metadata for it. This is a serialized array. The field sizes in that array contains all available image sizes.

Pseudo-code:

$sizes = array();

$attachment_url  = 'http://examle.com/wp-content-uploads/foo.png';
$attachment_dir  = dirname( $attachment_url );
$attachment_id   = get_attachment_id( $attachment_url );
$attachment_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', TRUE );

$sizes[] = $attachment_url; // main file

if ( ! empty ( $attachment_meta[ 'sizes' ] ) )
{
    foreach ( $attachment_meta[ 'sizes' ] as $size )
        $sizes[] = "$attachment_dir/" . $size[ 'file' ];
}
  • Thank you. I have taken the largest part of your code and I have edited it a little. Now it works perfect – Nepo Znat Jul 7 '13 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.