Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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' ];
}
share|improve this answer
    
Thank you. I have taken the largest part of your code and I have edited it a little. Now it works perfect –  user2416687 Jul 7 '13 at 12:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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