Square Images

Flickr.com when uploading an image, it provides a small thumbnail, but it does in a square image, this is of course almost impossible for any “normal” image, because most images are rectangular, and trying to make it square will give you an awful image as result.

But Flickr images are very good, so I did it in php the similar process, and I will explain below.

We will use this example image:
Square Images - Image Example

To create a square, we need to calculate the maximum square that fit on the image, this is the minimum size between height and width.
Square Images - Image Size

Next we create a square with that size, of course, we need to put in the center, because if not, the image could look very different to the original image.

Square Images - Square

This can be done easily with the following formula:

//if the square is adjusted to width
$remain = $height - $min_size;
$y = floor($remain / 2.0);
//if the square is adjusted to height like the example image
$remain = $width - $min_size;
$x = floor($remain / 2.0);

Square Images - Image Final

You can test on this site:

Or check the sources here (in txt):

The code is in two files and another php file which uses the other two, the files are:

  • ImageHandler.php this class provides basic handling of images, like openning and saving
  • ImageSquare.php this class extends the basic ImageHandler and provides a convert function which makes the image into square
  • index.php display the form and when an image is uploaded, it create the new square image and display it directly.