Dynamic-Image-Resize Plugin does not output anything

The question:

I’m having trouble using the “Dynamic-Image-Resize” plugin by “Kaiser Franz Josef”.
In my template I’m adding following code:

$imgsrc = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "Full");
$image_path = $imgsrc[0];

dynamic_image_resize( array(
    'src'     => $image_path    
    ,'width'   => 60
    ,'height'  => 100
    ,'classes' => 'thumb'
) );

It just outputs nothing, neither it give any error. I have made sure that the image path is correct.
How can I find out where is the problem?

If I var_dump the output of the above function, I get following:

object(oxoDynamicImageResize)#507 (1) { ["atts"]=> array(4) { ["src"]=> string(55) "http://localhost:81/wp/wp-content/uploads/2013/02/1.jpg" ["width"]=> int(60) ["height"]=> int(100) ["classes"]=> string(5) "thumb" } } 

I will highly appreciate any help.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Ask the right guy

Which you just did, so… here’s your answer from the developer. 🙂

What does the right output tell us?

First of all, the public dynamic_image_resize() API function is just a wrapper for the singleton itself. When you take a closer look at the class, you’ll notice a __toString() method, which is the magical method that returns the output.

As you’ve seen from your var_dump(), you get back the exact object (the class name indicates that), but not the string. So the __toString() method didn’t trigger and therefore doesn’t call the output. It just called the __construct() method with your attributes, so you see the exact same thing set as the class properties, that you defined when calling the class.

How to “fix” something that isn’t broken?

The class, as stated previously, uses the __toString() method. This method – as most magical PHP methods – only trigger under a certain condition. In this case, when you echo the output.

So your solution would simply be to echo dynamic_image_resize(). Here’s it written quickly (not tested).

echo dynamic_image_resize( array(
     'src'     => array_shift( wp_get_attachment_image_src( 
         get_post_thumbnail_id( get_the_ID() )
     ) )
    ,'width'   => 60
    ,'height'  => 100
    ,'classes' => 'thumb'
) );

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment