<-- all posts
08. Jun 2025

Compress images with Tinify in Laravel

Installation

composer require tinify/tinify

add to .env

TINIFY_API_KEY=your_tinify_api_key

Controller Example

This Laravel controller provides an image processing utility via the processImage method.

This method:

  1. Receives an uploaded image
  2. Resizes it to multiple widths (full, medium, small, mini)
  3. Compresses the resized images using Tinify (if the monthly quota allows)
  4. Saves the optimized images to public/storage/images/{size}
  5. Returns the new image filename
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Tinify\Tinify;
use Tinify\Source;
use Intervention\Image\Laravel\Facades\Image;

abstract class Controller
{

    protected function processImage(Request $request)
    {
        $imageWidth = [
            'full' => 1500,
            'medium' => 950,
            'small' => 600,
            'mini' => 150,
        ];
        $image = $request->file('image');
        $filename = pathinfo($image->getClientOriginalName(), PATHINFO_FILENAME);
        $extension = $image->getClientOriginalExtension();
        $newFilename = $filename . '_' . time() . '.' . $extension;
        \Tinify\setKey(env('TINIFY_API_KEY'));
        // Check usage count
        $useTinify = false;
        try {
            $compressionCount = \Tinify\compressionCount();
            if ($compressionCount < 500) {
                $useTinify = true;
            }
        } catch (\Tinify\Exception $e) {
            \Log::error('Tinify API error: ' . $e->getMessage());
        }
        // Read image
        $image = Image::read($image);
        foreach ($imageWidth as $folder => $maxWidth) {
            $resizedImage = $image->scaleDown(width: $maxWidth);
            $tempPath = storage_path('app/public/temp_' . $newFilename);
            $resizedImage->save($tempPath);
            $optimizedPath = public_path('storage/images/' . $folder . '/' . $newFilename);
            if ($useTinify) {
                try {
                    // Optimize with Tinify
                    $source = \Tinify\fromFile($tempPath);
                    $source->toFile($optimizedPath);
                } catch (\Tinify\Exception $e) {
                    \Log::error('Tinify optimization failed: ' . $e->getMessage());
                    // Fallback: Save the resized image without Tinify
                    $resizedImage->save($optimizedPath);
                }
            } else {
                // If Tinify limit is reached, save the resized image normally
                $resizedImage->save($optimizedPath);
            }
            // Delete temp file
            unlink($tempPath);
        }
        return $newFilename;
    }

    
}

Usage

$image = $this->processImage($request)

Contact - Data Protection - Imprint