# How to Create Square Thumbnails in K2 for Joomla! 1.5

K2 is definitely one of my favorite Joomla! components. It does everything so well that I rarely need to make edits to anything other than template files. The one exception is creating square thumbnails. Most of my customers incapable of creating square thumbnails, so we need the system to be as user friendly as possible.

This is a quick tutorial on creating square cropped thumbnails with K2. We are going to make the small images square, but you could apply this code to any image size.

*This modification will be lost with each upgrade to k2, so be sure to keep a backup of this code handy.

Search for “//Small image”

We are going to replace the entire block of code responsible for the image resizing.

Replace this:

//Small image
$handle->image_resize = true;$handle->image_ratio_y = true;
$handle->image_convert = 'jpg';$handle->jpeg_quality = $params->get('imagesQuality');$handle->file_auto_rename = false;
$handle->file_overwrite = true;$handle->file_new_name_body = $filename.'_S'; if (JRequest::getInt('itemImageS')) {$imageWidth = JRequest::getInt('itemImageS');
} else {
$imageWidth =$params->get('itemImageS', '200');
}
$handle->image_x =$imageWidth;
$handle->Process($savepath);

With:

//Small image
$handle->image_resize = true;$handle->image_convert = 'jpg';
$handle->jpeg_quality =$params->get('imagesQuality');
$handle->file_auto_rename = false;$handle->file_overwrite = true;
$handle->file_new_name_body =$filename.'_S';
if (JRequest::getInt('itemImageS')) {
$imageWidth = JRequest::getInt('itemImageS'); } else {$imageWidth = $params->get('itemImageS', '200'); }$handle->image_x = $imageWidth;$handle->image_y = $imageWidth;$handle->image_ratio_crop = true;
$handle->Process($savepath);

That’s it!

You can now upload your images to your items. All small images (images set as small) will now be cropped square. The width and height will match the default size for the small image size, or the custom size set by you in the category parameters.

Be Sociable, Share!
Tarik Assagai
Tarik (@tarik916) is a web developer and owner of ACS Digital Media, a Sacramento based web development agency specializing in open source CMS systems. If you are looking for support for your current or upcoming projects, visit www.acs-digital.com.

1. I hate mixed thumbnail ratios.
This is one of those little things which make a huge difference!!! Should be standard and optional for all images. Hope to see something like that in K2 future releases!

2. This is really sweet. Agreeing that it would be a nice radio button option for core K2. Thanks!

3. Very nice tip for K2. This works very well for clients who manage the content of their own sites and they don’t have any clue about crop images from Photoshop or any other programs. Thank you.

4. Krakaos /

5. Jafar /

wow, thanks for this.

It’s so easy to crop an image in the code, why wouldn’t this feature be added to the admin?

6. How do you apply this to K2_v2.5.1, can’t seem to find this in the item.php file?

• tarik916 /

Hi Jason,
Are you searching under the administrator folder (administrator\components\com_k2\models\item.php)? In version 2.5.1, the image resize code starts around line 198.

• Raphael /

I did that in the latest k2 version and I’m afraid it doesn’t work. any clues??

• tarik916 /

I am pretty sure I made the same change in the latest version and it worked properly. Do you not see the resize code inside of the item.php file?

7. dalibor /

Is there any way how to crop images in K2 2.5.4?
thank you

8. Hello here

I am trying to add this code to my website.

Unfortunately, image doesnt wants to resize..

It can be seen here:

Why could be this problem ?

Thank You.

Tom

9. Debbie /

Nice improvement to k2 but i couldn’t help wondering…
is it possible to do this with the category image?

• tarik916 /

This is possible, but I have not had to do it yet since categories are added less frequently than content items. Most people just upload a square image. If you want to do it through the code, you can edit administrator/components/com_k2/models/category.php.

• PeterK /

Hi Tarek,
I found the code at the category.php but it is another one then the code at the item.php. How is the right syntax to do the same crop as item.php?

10. mark /

Cant find the code in the image.php of k2 version 2.5.7.

has it moved and if so where to i cant find it?

Thank you

11. Hmmm, I found item.php but it’s not resizing my thumbnails here:
http://penelopes.xyonet.com/index.php/unique-products/carriers

Can’t wait for Joomla/K2 to build this in.

12. hi tariq! assalamo alekum
thanks for the best code …

13. i’m trying it on k2 2.6.2 but it doesn’t work at all. Have you check it on this version?