PHP code example of minipng / laravel-minipng

1. Go to this page and download the library: Download minipng/laravel-minipng library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

minipng / laravel-minipng example snippets


return [
    'api_key' => env('MINIPNG_API_KEY', ''),
    'base_url' => env('MINIPNG_BASE_URL', 'https://minipng.com'),
    'api_version' => env('MINIPNG_API_VERSION', 'v1'),
    'timeout' => env('MINIPNG_TIMEOUT', 30),
    'retry_attempts' => env('MINIPNG_RETRY_ATTEMPTS', 3),
    'default_image_quality' => env('MINIPNG_DEFAULT_IMAGE_QUALITY', 85),
    'default_pdf_images_quality' => env('MINIPNG_DEFAULT_PDF_IMAGES_QUALITY', 'medium'),
    'default_pdf_images_format' => env('MINIPNG_DEFAULT_PDF_IMAGES_FORMAT', 'png'),
];

use MiniPNG\LaravelMiniPNG\Facades\MiniPNG;

// Compress an image
$result = MiniPNG::compressImage('https://example.com/image.jpg');

// Convert image format
$result = MiniPNG::convertImage('https://example.com/image.jpg', 'webp', 90);

// Compress PDF
$result = MiniPNG::compressPdf('https://example.com/document.pdf');

// Convert PDF to images
$result = MiniPNG::convertPdfToImages('https://example.com/document.pdf', 'high', 'jpg');

// Get user profile
$profile = MiniPNG::getProfile();

use MiniPNG\LaravelMiniPNG\Contracts\MiniPNGInterface;

class ImageController extends Controller
{
    public function __construct(private MiniPNGInterface $minipng)
    {
    }

    public function compress(Request $request)
    {
        $result = $this->minipng->compressImage($request->input('image_url'));
        return response()->json($result);
    }
}

$minipng = app('minipng');
$result = $minipng->compressImage('https://example.com/image.jpg');

$result = MiniPNG::compressImage('https://example.com/image.jpg');

// Convert to WebP with 90% quality
$result = MiniPNG::convertImage('https://example.com/image.jpg', 'webp', 90);

// Convert to PNG with default quality
$result = MiniPNG::convertImage('https://example.com/image.jpg', 'png');

$result = MiniPNG::compressPdf('https://example.com/document.pdf');

// Convert to high-quality JPG images
$result = MiniPNG::convertPdfToImages('https://example.com/document.pdf', 'high', 'jpg');

// Convert to medium-quality PNG images (default)
$result = MiniPNG::convertPdfToImages('https://example.com/document.pdf');

$profile = MiniPNG::getProfile();

use MiniPNG\LaravelMiniPNG\Exceptions\MiniPNGException;

try {
    $result = MiniPNG::compressImage('https://example.com/image.jpg');
} catch (MiniPNGException $e) {
    // Handle API errors
    Log::error('MiniPNG API Error: ' . $e->getMessage());
}



namespace App\Http\Controllers;

use MiniPNG\LaravelMiniPNG\Facades\MiniPNG;
use MiniPNG\LaravelMiniPNG\Exceptions\MiniPNGException;
use Illuminate\Http\Request;

class ImageController extends Controller
{
    public function compress(Request $request)
    {
        $request->validate([
            'image_url' => '            'success' => false,
                'message' => $e->getMessage(),
            ], 400);
        }
    }

    public function convert(Request $request)
    {
        $request->validate([
            'image_url' => 'son([
                'success' => false,
                'message' => $e->getMessage(),
            ], 400);
        }
    }
}



namespace App\Console\Commands;

use Illuminate\Console\Command;
use MiniPNG\LaravelMiniPNG\Facades\MiniPNG;

class CompressImage extends Command
{
    protected $signature = 'image:compress {url}';
    protected $description = 'Compress an image using MiniPNG API';

    public function handle()
    {
        $url = $this->argument('url');

        $this->info("Compressing image: {$url}");

        try {
            $result = MiniPNG::compressImage($url);
            
            $this->info("Compression successful!");
            $this->table(['Metric', 'Value'], [
                ['Original Size', $result['size_before'] . ' bytes'],
                ['Compressed Size', $result['size_after'] . ' bytes'],
                ['Compression', $result['compression_percentage']],
                ['Download URL', $result['download']],
            ]);
        } catch (\Exception $e) {
            $this->error("Compression failed: " . $e->getMessage());
        }
    }
}
bash
php artisan vendor:publish --tag=minipng-config