Cropping a blog post thumbnail in pagelist view?

Permalink
Hi all,

I've added the code to add a thumbnail image to a blog post.

<div class="image-link">
         <a <?php   if ($target != '') { ?> target="<?php  echo $target?>" <?php   } ?> href="<?php  echo $nh->getLinkToCollection($cobj)?>">
         <?php  
            $ts = $cobj->getBlocks('Thumbnail Image');
            if (is_object($ts[0])) {
               $tsb = $ts[0]->getInstance();
               $thumb = $tsb->getFileObject();
               if($thumb){
                  $imgHelper->outputThumbnail($thumb, 100, 100, $title);
               }
            }
         ?>
         </a>
      </div>


Question is, how do I get it so that it crops images to make a square (in this instance) image. I don't mind how it crops it, I would just prefer to have a full square image rather than an image that shrinks proportionately, the largest dimension to 100px.

I see that this has been something bounded around the forums before, and there are hints to suggest this functionality is built in, but I can't see how to make it work with the above code.

Any pointers would be appreciated.

thank you in advance :)

 
JohntheFish replied on at Permalink Best Answer Reply
JohntheFish
Looking at the source in core/helpers/image.php
public function outputThumbnail($obj, $maxWidth, $maxHeight, $alt = null, $return = false, $crop = false)


I would hazard a guess that calling with $crop = true would do what you want.
jjcdesign replied on at Permalink Reply
Thanks for the reply. I must admit I had seen that and I tried adding that bit, I also thought I'd tried various different variants to try and get it to work without success, but low and behold, I now have it working.

changing to the following works:
$imgHelper->outputThumbnail($thumb, 100, 100, $title, $return = false, $crop = true);


but just adding the following doesn't:
$imgHelper->outputThumbnail($thumb, 100, 100, $title, $crop = true);


I made sure I cleared the cache each time just incase. NOt sure where I went wrong before, but hey, it's working now.


So there we go, success. Thanks JohntheFish for getting me to try it again :)



EDIT: See JohntheFish comment below - all you need to do is:
$imgHelper->outputThumbnail($thumb, 100, 100, $title, false, true);
JohntheFish replied on at Permalink Reply
JohntheFish
You may have php default function parameters confused with actually making the call. When calling a function, you only need to do:
$imgHelper->outputThumbnail($thumb, 100, 100, $title, false, true);


What the $crop=false etc in the function definition does is tell php that if you don't specify that parameter, the function will assume the default value.

If you count the parameters used, what you previously had would have put the 'true' value in the wrong parameter. Hence not do what you wanted.
jjcdesign replied on at Permalink Reply
Hi, thanks for the info.

I thought I'd tried that earlier in the day without success, but it maybe that like you mention, I was putting true in the wrong parameter, or perhaps I just needed to clear the cache for it to work when I'd actually put it in right! doh!

I have now changed it to just "false, true" and as you say, it still works.


Thanks again for your time and wisdom on this, and on all your other valuable contributions across the forums.