Difference between getThumbnail and outputThumbnail and how to use to control thumbnail size

Permalink 2 users found helpful
Hi There,

I have a custom pagelist template that outputs thumbnails to the size I would like using the getThumbnail property - everything works great.

I also have a custom template for rendering related entries based on tags which uses the outputThumbnail method - which also works but it won't render the thumbnails any bigger no matter what I enter on this line:
$imageHelper->outputThumbnail($thumbnail,240,155);


If I try to copy the getThumbnail code into the template I get a PHP error so have assumed this method can't be used in this template for some reason.

Is there a way to output custom sizes when using the outputThumbnail method?

Any help would be appreciated - have been struggle with this for a while and clearly don't understand PHP.

Cheers

Ben

 
jshannon replied on at Permalink Reply
jshannon
Hi.

I think your problem might be more complex than you think.

The difference between getThumbnail and outputThumbnail is pretty simple.

The former returns an object from which you most likely care about the src attribute (ie, the path). The latter returns an <img /> tag.

In fact, the latter calls the former, gets the src and puts it into the <img /> tag. Which is why it doesn't make much sense that one is causing an error (outputThumbnail actually calls getThumbnail behind the scenes).

For the size, create() (the function that does the actual sizing) won't create a thumbnail any larger than the original pic -- that might be your problem.

For the PHP error, I suspect you might have some bad code and be calling it incorrectly (maybe incorrect arguments). Feel free to paste the code and error here, but I don't thin that fixing getThumbnail() will get you bigger thumbnails.

James
cmscss replied on at Permalink Reply
Thank you so much for the reply - very new to c5 (in addition to a lack of php knowledge) which is not helping things.

Here is the code from the page list custom template that resizes the thumbnail bigger than the original:

<?php foreach ($cArray as $cobj):
   $title = $cobj->getCollectionName();
   $link = $nh->getLinkToCollection($cobj);
   $target = $cobj->getAttribute('nav_target');
      if (!empty($target)) {
         $target = 'target="' . $target . '"';
         }
   // load image helper
   $imageHelper = Loader::helper("image");
   // load page attribute 'main_image' and apply thumbnail size
   $thumbnail = $cobj->getAttribute('main_image');
      if ($thumbnail) {
         $thumbnailObj = $imageHelper->getThumbnail($thumbnail, 240, 155);
         }
?>


And here is the code that doesn't resize:
<p><a <?php echo $target ?> href="<?php echo $link ?>">
            <?php
            // load page attribute 'main_image' and apply thumbnail size
            $thumbnail = $cobj->getAttribute('main_image');
            if ($thumbnail instanceof File) {
               $imageHelper->outputThumbnail($thumbnail,240,155);
            }
            ?>
         </a></p>


And here is what I tried to enter in order to get it to resize bigger:
<?php
defined('C5_EXECUTE') or die("Access Denied.");
$textHelper = Loader::helper("text");
$navigation = Loader::helper('navigation');
$imageHelper = Loader::helper("image");      
?>
<?php foreach ($cArray as $cobj):
   $title = $cobj->getCollectionName();
   $link = $nh->getLinkToCollection($cobj);
   $target = $cobj->getAttribute('nav_target');
      if (!empty($target)) {
         $target = 'target="' . $target . '"';
         }
   // load image helper
   $imageHelper = Loader::helper("image");


This isn't all the code as I'm loading a few page attributes as well but you're right, I don't understand how this all fits together and have made it thus far by looking at templates that work and copying/pasting - I'm used to CMS tags as opposed to PHP sorry.

Any pointers in the right direction would be much appreciated.

Cheers

Ben
jshannon replied on at Permalink Reply
jshannon
My first question is what are your error messages? What's happening (or not) on the ones that aren't working?

My guess is that your main_image attribute isn't actually a file, but a text attribute. (What do you store in the attribute? A path? Or is there a file picker where you specifically choose a file?) In your second example try removing " instanceof File" from the if statement.... just to test.

James
TheRealSean replied on at Permalink Reply
TheRealSean
Just to rule this out the image is larger then the dimensions you mention?

Concrete5 will not make an image larger then its current dimensions, you could stretch it using the height and width on the image tag but that would not resize the image, only distort it.

Second make sure you cache is turned off and cleared sometimes its a caching issue and the image could have been made larger but not been displayed? clicking on edit, then update, exit edit mode usually resolves this for me.
cmscss replied on at Permalink Reply
That's correct, I'm trying to generate a thumbnail which is bigger than the original which I realise is not possible - so I added a width="" attribute to the output which works great.

But in the second example I can't work out where you would add a width attribute because ether's no echo:
<p><a <?php echo $target ?> href="<?php echo $link ?>">
            <?php
            // load page attribute 'main_image' and apply thumbnail size
            $thumbnail = $cobj->getAttribute('main_image');
            if ($thumbnail instanceof File) {
               $imageHelper->outputThumbnail($thumbnail,240,500);
            }
            ?>
         </a></p>


I realise in an ideal world I wouldn't be doing this but I'm stuck and need a quick fix and PHP is quite full-on compared with the usual CMS tags I'm used to (Modx, Expression Engine).
jshannon replied on at Permalink Reply
jshannon
That's the point of the outputHelper() helper function... it does everything for you, including the width tag.

If you want to specify the width, you need to get the getThumbnail() working.
TheRealSean replied on at Permalink Reply
TheRealSean
Did you get this sorted?
cmscss replied on at Permalink Reply
Thanks mate I did - to be honest it was just trial and error as I haven't got me head around all the syntax at this point.

Cheers

Ben
mkly replied on at Permalink Reply
mkly
To anyone else who comes across this.

getThumbnail does not return a string, but instead an Object. So if you take the output of getThumnail and try to build a string with it it will fail.

$tmb = $im->getThumbnail($f, 50, 50);
echo '<img src="' . $tmb '" />';

=FAIL

$tmb = $im->getThumbnail($f, 50, 50);
echo '<img src=' . $tmb->src . '" width="' . $tmb->width . '" height="' $tmb->height . "' />';

=WIN
jshannon replied on at Permalink Reply
jshannon
Yeah... except if you do that, you're duplicating the outputThumbnail() functionality....
mkly replied on at Permalink Reply
mkly
Thanks Captain Code.

I couldn't figure out how to add an additional class with outputThumbnail().

Any suggestions?
jordanlev replied on at Permalink Reply
jordanlev
I respectfully disagree with @jshannon on this one -- you are not duplicating the outputThumbnail functionality because outputThumbnail is very inflexible, always outputting the image tag just like this:
<img class="ccm-output-thumbnail" alt="XXX" src="XXX" width="XXX" height="XXX" />


What if I don't want that class on my image? Or I want a different class? Or I want an id instead? Or I want to stretch the image to be larger than its original size (as the OP seems to have wanted to do)? Then I need different functionality, so not really duplication at all in my book.

-Jordan
jshannon replied on at Permalink Reply
jshannon
True to both of you...

I was mostly referring to the one case where you're doing a "default" image, like the example I was replying to.

James