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 got this annoying issue I can't solve :

if( $meta1 = '' && $meta2 = '' ) { 
$image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), my_own_size_function());
echo  $image_attributes[0];
} elseif(  $meta1 != '' ) { 
    echo $meta1; 
} elseif ( $meta2  != '' ){
    $image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), my_own_size_function() );
    echo $image_attributes[0];
} else {
    echo "ko";
}

when I print_r my_own_size_function() I get an array like this :

Array
(
     [0] => 600
     [1] => 400
)

But when I print_r $image_attributesI get another result :

Array
(
    [0] => http://localhost/wordpress/wp-content/uploads/2013/07/my_image.jpg
    [1] => 200
    [2] => 150
    [3] => 
)

Any idea of what can be the reason for this? Thanks in advance.

share|improve this question
add comment

2 Answers 2

up vote 0 down vote accepted

The following is entirely based on experiment over the last 15 minutes or so, so I wouldn't call it canonical but ...

wp_get_attachment_image_src() uses the image_downsize() function, which will attempt to find and return the image closest in size to the supplied size values. If you experiment with know image sizes (look at the actual images saved to wp-content/uploads) you will see what I mean. Despite the name, it will sometimes return larger images but the height and width components of the array should be set to the supplied values, or to some attempt at a scaled approximation (so it seems) if those values are wildly inaccurate. I can't tell if that "scaling" is what is happening in your case but it might be and it seems likely.

Let me give an example. I have an image that saved as 150x150, 293x300, and full size. This...

var_dump(wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), array(20, 20)));

... returns an array with the 150x150 image and the sizes that I supplied-- 20x20. However this...

var_dump(wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), array(200, 250)));

... returns the 150x150 image URL but with 200x204 for the sizes. That is not what I supplied. The size values have been altered by some algorithm. On further investigation it seem like image_constrain_size_for_editor is the more likely candidate.

You may also check to see if anything has applied a function to the image_downsize filter, as that will completely hijack the function.

share|improve this answer
    
Thanks for you answer. Good read anyway but the problem is the size does not even change. I've include a size selector for featured image. It does not seem to come from my_own_size_function() simply because I get what I want when I print_r –  JMau Jul 30 '13 at 15:37
    
I guess I'm the weakest link! I checked uploads and I was dealing with the only image who has not the appropriate size. –  JMau Jul 30 '13 at 15:43
add comment

Change the function my_own_size_function(); to array(600, 400)

$image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), array(600, 400));
share|improve this answer
    
Thanks dude but this would be array(600,400) instead. Anyway I already tried that but I checked the function in codex and they say $size can be an array, my function already return array(600,400); –  JMau Jul 30 '13 at 14:51
1  
And you have the image in that size uploaded? –  Pontus Abrahamsson Jul 30 '13 at 14:55
    
this was actually an upload issue. Thanks –  JMau Jul 30 '13 at 15:43
add comment

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.