Function for cropping thumbnails in php

During my web development work, I need to make pretty looking square thumbnails, which will be smartly cut out from the bigger picture. Experimenting a lot with various methods, I found the best suitable one for me.

First of all, I want to show the function I use for these purposes

This function is intended to cut thumbnail $dest_image of a proper form from $image, which left top corner will start on $indexX, $indexY coordinates and will have sizes $width,$height.

So, let’s look at an example.
Imagine, we have a picture size 600x1000px. We need to get thumbnail size 150x150px. So, the picture smaller size will be reduced to 150px and the largest will be cut, so that to get the picture from the middle.
First of all, we’ll create a smaller version of our big file. We are using function createthumb, as it was described in this article. In this case we are making picture smaller by the smallest size.

As a result our $targetFile_temp image will have the following size 150x250px.