0

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.

2
  • 1
    What was the original size of the uploaded image? Jul 18, 2012 at 12:08
  • 2677 (width) x 1232 (height)
    – testing
    Jul 18, 2012 at 13:00

1 Answer 1

3

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.

8
  • 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, 2012 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. Jul 19, 2012 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, 2012 at 17:24
  • Well, you could grab the medium & scale it, or register a new image size & regenerate the existing ones with Regenerate Thumbnails. Jul 19, 2012 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. Jul 20, 2012 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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