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 an image which should be output on the page. The space is limited (width-max = 209px). Now I have to provide the image in a scaled size, if the user doesn't do it.

This is my code:

$src = wp_get_attachment_image_src($logo, 'full');
$width = $src[1];
$height = $src[2];
$new_width = 200;
$factor = $width/$new_width;
$new_height = round($height/$factor);
echo wp_get_attachment_image($logo, array($new_width, $new_height))

But the image used is the thumbnail image (150x150) and the width and height in the html code is 92!

<img width="92" height="92" title="my title" alt="Logo" class="attachment-200x92" src="http://www.test.com/wp-content/uploads/2012/07/logo-150x150.jpg">

In the docu it is stated that I should use add image size but the height can be different for each picture. Also what happens with already uploaded pictures?

How can I fix this?

Solution:

add_image_size( 'squeeze', 200, 1000, false ); and Regenerate Thumbnails did the job for me.

share|improve this question
1  
What was the original size of the uploaded image? –  TheDeadMedic Jul 18 '12 at 12:08
    
2677 (width) x 1232 (height) –  testing Jul 18 '12 at 13:00

1 Answer 1

up vote 3 down vote accepted

The real workhorses behind wp_get_attachment_image_src are image_downsize & image_get_intermediate_size.

Between the two, they'll attempt to find the largest image available that fits within the constraints of the required size.

The return value of wp_get_attachment_image_src will be:

Array(
    [0] => 'source of image that best fits',
    [1] => 'width of source image'
    [2] => 'height of source image',
)

Also what happens with already uploaded pictures?

Nothing. You'll need a plugin like Regenerate Thumbnails if you've changed your media settings/added image sizes, and want to apply them to all exisiting images.

share|improve this answer
    
Sorry, I don't get it. image_downsize gives me always thumb. image_get_intermediate_size also gave me a thumb (150x150). Also I don't know the exact height/width. I can only calculate the height. I want the following: 1) Extract the orginal width/height ->Done. 2) Calculate a matching height (width is set to a constant) -> Done. 3) Get a scaled image with the new width/height -> Problems –  testing Jul 19 '12 at 17:09
    
wp_get_attachment_image won't give you a scaled image, it'll give an image that best fits. Since you're asking for an image no wider than 200, the thumb is the only candidate. –  TheDeadMedic Jul 19 '12 at 17:12
    
So taking the original image size (2677 x 1232) and scaling it down with HTML is the only option for me? –  testing Jul 19 '12 at 17:24
    
Well, you could grab the medium & scale it, or register a new image size & regenerate the existing ones with Regenerate Thumbnails. –  TheDeadMedic Jul 19 '12 at 20:14
1  
Just use something like add_image_size( 'squeeze', 200, 1000, false ). This'll create a scaled image (because crop is false) no wider than 200, and no taller than 1000. Unless you upload a ridiculously exaggerated portrait image, scaled will always be 200px wide. –  TheDeadMedic Jul 20 '12 at 10:10

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.