Thumbnail generation is very important for image-heavy projects. They help to reduce website’s outgoing traffic and they’re one of key factors for website’s load time optimisation.

Today, I want to share with you very simple way to achieve thumbnail generation right after image upload.

Before we start, I want to inform you about my server’s configuration and build. First of all, I prefer using DigitalOcean servers.

OS: Debian PHP: PHP-FPM Dedicated Server with open SSH access

I’m assuming that, you’re using dedicated or virtual server (not cPanel based), you have SSH access to your server. And your OS is Debian (or one of it’s distros). Also you may or not use framework. But I will provide example based on Yii2 Framework.

Step 1: Install Imagick extension

if you already have this extension then skip this step. In other case install and restart php5-fpm service.

sudo apt-get install php5-imagick service php5-fpm restart

Step 2: Write PHP code like below

cropThumbnailImage(100,100); $imagick->writeImage($fileThumbPath); $file->saveAs($filePath); } } That’s it. ## So what we’ve done? - We got uploaded file on first line of function. - Then we extracted file’s extension for future use. - We created new file name using PHP’s uniqid() function. - Then we created instance of imageMagick, generated 100×100 px thumbnail and saved - Then we saved original image