PHP code example of migears / image

1. Go to this page and download the library: Download migears/image 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/ */

    

migears / image example snippets


use MiGears\Image\GDImage;
use MiGears\Image\ImageType;

// Load from file
$img = new GDImage('/path/to/image.jpg');

// Chain operations and save
$img->resizeToWidth(800)
    ->cropCenter(400, 300)
    ->save('/path/to/output.jpg', ImageType::JPEG, 90);

// Generate thumbnail (resize then crop, centered)
$img->thumbnail(200, 200)->save('/path/to/thumb.jpg');

// Add image watermark
$img->imageWatermark('/path/to/watermark.png', 10, 10, 70)
    ->save('/path/to/wm.jpg');

// Add text watermark
$img->textWatermark(
    text: 'Copyright',
    fontFile: '/path/to/font.ttf',
    fontSize: 14,
    x: 10,
    y: 10,
    opacity: 80,
    color: 0xFFFFFF,
)->save('/path/to/text-wm.jpg');

// Format conversion
$img->save('/path/to/output.webp', ImageType::WEBP);

use MiGears\Image\GDImage;

$img = new GDImage('/path/to/photo.jpg');

// Rotate 90 degrees clockwise
$img->rotate(90)->save('/path/to/rotated.jpg');

// Rotate with custom background color (for non-90° angles)
$img->rotate(45, 0xFFFFFF)->save('/path/to/tilted.jpg');

// Flip horizontally (mirror effect)
$img->flipHorizontal()->save('/path/to/mirrored.jpg');

// Flip vertically
$img->flipVertical()->save('/path/to/flipped.jpg');

use MiGears\Image\GDImage;
use MiGears\Image\ImageType;

// Generate avatar dynamically
$img = new GDImage('/path/to/avatar.png');
$img->resizeToWidth(128);

// Set Content-Type and output
header('Content-Type: ' . ImageType::PNG->mime());
echo $img->output(ImageType::PNG);

use MiGears\Image\GDImage;
use MiGears\Image\ImageType;

function generateThumbnails(string $sourcePath, string $outputDir): void
{
    $img = new GDImage($sourcePath);

    // Large: 800px wide
    $img->resizeToWidth(800)
        ->save("{$outputDir}/large.jpg", ImageType::JPEG, 85);

    // Medium: 400px wide
    $img->resizeToWidth(400)
        ->save("{$outputDir}/medium.jpg", ImageType::JPEG, 80);

    // Square thumbnail: 200x200 (center crop)
    $img->thumbnail(200, 200)
        ->save("{$outputDir}/thumb.jpg", ImageType::JPEG, 75);

    $img->destroy();
}

// From file path — the type is detected from the file content
new GDImage(string $filePath);

// Passing a type for a file path is optional and may only confirm the detected type,
// otherwise ImageException is thrown
new GDImage(string $filePath, ImageType $type);

// From GD resource — there is no file to inspect, so $type is used as-is (defaults to PNG)
new GDImage(\GdImage $resource, ?ImageType $type = null);

$info = $img->info();

$info->width;       // int
$info->height;      // int
$info->type;        // ImageType enum
$info->mime();      // string
$info->extension(); // string
$info->aspectRatio(); // float
$info->isSquare();  // bool
$info->isLandscape(); // bool
$info->isPortrait();  // bool

ImageType::JPEG;
ImageType::PNG;
ImageType::GIF;
ImageType::WEBP;

ImageType::fromExtension('jpg');
ImageType::fromMime('image/jpeg');

use MiGears\Image\GDImage;
use MiGears\Image\ImageType;

// 从文件加载
$img = new GDImage('/path/to/image.jpg');

// 链式操作并保存
$img->resizeToWidth(800)
    ->cropCenter(400, 300)
    ->save('/path/to/output.jpg', ImageType::JPEG, 90);

// 生成缩略图(先缩放后裁剪,居中)
$img->thumbnail(200, 200)->save('/path/to/thumb.jpg');

// 添加图片水印
$img->imageWatermark('/path/to/watermark.png', 10, 10, 70)
    ->save('/path/to/wm.jpg');

// 添加文字水印
$img->textWatermark(
    text: 'Copyright',
    fontFile: '/path/to/font.ttf',
    fontSize: 14,
    x: 10,
    y: 10,
    opacity: 80,
    color: 0xFFFFFF,
)->save('/path/to/text-wm.jpg');

// 格式转换
$img->save('/path/to/output.webp', ImageType::WEBP);

use MiGears\Image\GDImage;

$img = new GDImage('/path/to/photo.jpg');

// 顺时针旋转 90 度
$img->rotate(90)->save('/path/to/rotated.jpg');

// 带自定义背景色旋转(非 90° 角度时)
$img->rotate(45, 0xFFFFFF)->save('/path/to/tilted.jpg');

// 水平翻转(镜像效果)
$img->flipHorizontal()->save('/path/to/mirrored.jpg');

// 垂直翻转
$img->flipVertical()->save('/path/to/flipped.jpg');

use MiGears\Image\GDImage;
use MiGears\Image\ImageType;

// 动态生成头像
$img = new GDImage('/path/to/avatar.png');
$img->resizeToWidth(128);

// 设置 Content-Type 并输出
header('Content-Type: ' . ImageType::PNG->mime());
echo $img->output(ImageType::PNG);

use MiGears\Image\GDImage;
use MiGears\Image\ImageType;

function generateThumbnails(string $sourcePath, string $outputDir): void
{
    $img = new GDImage($sourcePath);

    // 大图:宽 800px
    $img->resizeToWidth(800)
        ->save("{$outputDir}/large.jpg", ImageType::JPEG, 85);

    // 中图:宽 400px
    $img->resizeToWidth(400)
        ->save("{$outputDir}/medium.jpg", ImageType::JPEG, 80);

    // 方形缩略图:200x200(居中裁剪)
    $img->thumbnail(200, 200)
        ->save("{$outputDir}/thumb.jpg", ImageType::JPEG, 75);

    $img->destroy();
}

// 从文件路径 —— 类型由文件内容自动检测
new GDImage(string $filePath);

// 文件路径下 $type 为可选,仅用于确认检测结果;不一致时抛出 ImageException
new GDImage(string $filePath, ImageType $type);

// 从 GD 资源 —— 无文件可检测,$type 直接生效(省略时默认 PNG)
new GDImage(\GdImage $resource, ?ImageType $type = null);

$info = $img->info();

$info->width;       // int
$info->height;      // int
$info->type;        // ImageType 枚举
$info->mime();      // string
$info->extension(); // string
$info->aspectRatio(); // float
$info->isSquare();  // bool
$info->isLandscape(); // bool
$info->isPortrait();  // bool

ImageType::JPEG;
ImageType::PNG;
ImageType::GIF;
ImageType::WEBP;

ImageType::fromExtension('jpg');
ImageType::fromMime('image/jpeg');