Featured image of post 【Laravel】Intervention Image v3 图片套件图片处理范例:参数、图片 Meta 资讯、resize 图片缩放、scale 图片等比例缩放、cover 裁切合併、crop 裁切、brightness 亮度、contrast 对比、gamma 校正、colorize 颜色校正、greyscale 灰阶、flop 左右镜像翻转、flip 上下镜像翻转、rotate 旋转、blur 模糊、invert 颜色反转、pixelate 像素化、reduceColors 减少颜色数量、quality 品质

【Laravel】Intervention Image v3 图片套件图片处理范例:参数、图片 Meta 资讯、resize 图片缩放、scale 图片等比例缩放、cover 裁切合併、crop 裁切、brightness 亮度、contrast 对比、gamma 校正、colorize 颜色校正、greyscale 灰阶、flop 左右镜像翻转、flip 上下镜像翻转、rotate 旋转、blur 模糊、invert 颜色反转、pixelate 像素化、reduceColors 减少颜色数量、quality 品质

【Laravel】Intervention Image v3 图片套件图片处理范例:参数、图片 Meta 资讯、resize 图片缩放、scale 图片等比例缩放、cover 裁切合併、crop 裁切、brightness 亮度、contrast 对比、gamma 校正、colorize 颜色校正、greyscale 灰阶、flop 左右镜像翻转、flip 上下镜像翻转、rotate 旋转、blur 模糊、invert 颜色反转、pixelate 像素化、reduceColors 减少颜色数量、quality 品质

参数

Format

// vendor/intervention/image/src/Format.php
use Intervention\Image\Format;

Intervention\Image\Format::AVIF;
Intervention\Image\Format::BMP;
Intervention\Image\Format::GIF;
Intervention\Image\Format::HEIC;
Intervention\Image\Format::JP2;
Intervention\Image\Format::JPEG;
Intervention\Image\Format::PNG;
Intervention\Image\Format::TIFF;
Intervention\Image\Format::WEBP;

MediaType

// vendor/intervention/image/src/MediaType.php
use Intervention\Image\MediaType;

Intervention\Image\MediaType::IMAGE_JPEG = 'image/jpeg';
Intervention\Image\MediaType::IMAGE_JPG = 'image/jpg';
Intervention\Image\MediaType::IMAGE_PJPEG = 'image/pjpeg';
Intervention\Image\MediaType::IMAGE_WEBP = 'image/webp';
Intervention\Image\MediaType::IMAGE_X_WEBP = 'image/x-webp';
Intervention\Image\MediaType::IMAGE_GIF = 'image/gif';
Intervention\Image\MediaType::IMAGE_PNG = 'image/png';
Intervention\Image\MediaType::IMAGE_X_PNG = 'image/x-png';
Intervention\Image\MediaType::IMAGE_AVIF = 'image/avif';
Intervention\Image\MediaType::IMAGE_X_AVIF = 'image/x-avif';
Intervention\Image\MediaType::IMAGE_BMP = 'image/bmp';
Intervention\Image\MediaType::IMAGE_MS_BMP = 'image/ms-bmp';
Intervention\Image\MediaType::IMAGE_X_BITMAP = 'image/x-bitmap';
Intervention\Image\MediaType::IMAGE_X_BMP = 'image/x-bmp';
Intervention\Image\MediaType::IMAGE_X_MS_BMP = 'image/x-ms-bmp';
Intervention\Image\MediaType::IMAGE_X_WINDOWS_BMP = 'image/x-windows-bmp';
Intervention\Image\MediaType::IMAGE_X_WIN_BITMAP = 'image/x-win-bitmap';
Intervention\Image\MediaType::IMAGE_X_XBITMAP = 'image/x-xbitmap';
Intervention\Image\MediaType::IMAGE_TIFF = 'image/tiff';
Intervention\Image\MediaType::IMAGE_JP2 = 'image/jp2';
Intervention\Image\MediaType::IMAGE_JPX = 'image/jpx';
Intervention\Image\MediaType::IMAGE_JPM = 'image/jpm';
Intervention\Image\MediaType::IMAGE_HEIC = 'image/heic';
Intervention\Image\MediaType::IMAGE_HEIF = 'image/heif';

FileExtension

// vendor/intervention/image/src/FileExtension.php
use Intervention\Image\FileExtension;


Intervention\Image\FileExtension::JPG = 'jpg';
Intervention\Image\FileExtension::JPEG = 'jpeg';
Intervention\Image\FileExtension::WEBP = 'webp';
Intervention\Image\FileExtension::AVIF = 'avif';
Intervention\Image\FileExtension::BMP = 'bmp';
Intervention\Image\FileExtension::GIF = 'gif';
Intervention\Image\FileExtension::PNG = 'png';
Intervention\Image\FileExtension::TIF = 'tif';
Intervention\Image\FileExtension::TIFF = 'tiff';
Intervention\Image\FileExtension::JP2 = 'jp2';
Intervention\Image\FileExtension::J2K = 'j2k';
Intervention\Image\FileExtension::JPF = 'jpf';
Intervention\Image\FileExtension::JPM = 'jpm';
Intervention\Image\FileExtension::JPG2 = 'jpg2';
Intervention\Image\FileExtension::J2C = 'j2c';
Intervention\Image\FileExtension::JPC = 'jpc';
Intervention\Image\FileExtension::JPX = 'jpx';
Intervention\Image\FileExtension::HEIC = 'heic';
Intervention\Image\FileExtension::HEIF = 'heif';

Driver

// vendor/intervention/image/src/Drivers/Imagick/Driver.php
use Intervention\Image\Drivers\Imagick\Driver;

// vendor/intervention/image/src/Drivers/Gd/Driver.php
use Intervention\Image\Drivers\Gd\Driver;

Decoder

// vendor/intervention/image/src/Decoders/Base64ImageDecoder.php
use Intervention\Image\Decoders\Base64ImageDecoder;

// vendor/intervention/image/src/Decoders/BinaryImageDecoder.php
use Intervention\Image\Decoders\BinaryImageDecoder;

// vendor/intervention/image/src/Decoders/DataUriImageDecoder.php
use Intervention\Image\Decoders\DataUriImageDecoder;

// vendor/intervention/image/src/Decoders/FilePathImageDecoder.php
use Intervention\Image\Decoders\FilePathImageDecoder;

// vendor/intervention/image/src/Decoders/FilePointerImageDecoder.php
use Intervention\Image\Decoders\FilePointerImageDecoder;

// vendor/intervention/image/src/Decoders/FilePathImageDecoder.php
use Intervention\Image\Decoders\FilePathImageDecoder;

// vendor/intervention/image/src/Decoders/ImageObjectDecoder.php
use Intervention\Image\Decoders\ImageObjectDecoder;

// vendor/intervention/image/src/Decoders/NativeObjectDecoder.php
use Intervention\Image\Decoders\NativeObjectDecoder;

// vendor/intervention/image/src/Decoders/SplFileInfoImageDecoder.php
use Intervention\Image\Decoders\SplFileInfoImageDecoder;

Encoder

Intervention\Image\Encoders\AutoEncoder::class
Intervention\Image\Encoders\JpegEncoder::class
Intervention\Image\Encoders\WebpEncoder::class
Intervention\Image\Encoders\PngEncoder::class
Intervention\Image\Encoders\GifEncoder::class
Intervention\Image\Encoders\AvifEncoder::class
Intervention\Image\Encoders\BmpEncoder::class
Intervention\Image\Encoders\TiffEncoder::class
Intervention\Image\Encoders\Jpeg2000Encoder::class
Intervention\Image\Encoders\HeicEncoder::class

Colorspaces

Intervention\Image\Colors\Rgb\Colorspace::class;
Intervention\Image\Colors\Cmyk\Colorspace::class;
Intervention\Image\Colors\Hsv\Colorspace::class;
Intervention\Image\Colors\Hsl\Colorspace::class;

图片 Meta 资讯

// create new manager instance with desired driver
$manager = new ImageManager(new Driver());

// reading an image
$image = $manager->read('images/example.png');


// reading the image width
$width = $image->width();
// reading the image height
$height = $image->height();
// read image size
$size = $image->size();
// read aspect ratio
$ratio = $size->aspectRatio();



// determine image format
$is_portrait = $size->isPortrait(); // true
$is_landscape = $size->isLandscape(); // false
// read width from size
$width = $size()->width();



// read image resolution object
$resolution = $image->resolution();
// convert resolution to dpcm
$resolution = $resolution->perCm();
// read resolution for each axis
$x = $resolution->x();
$y = $resolution->y();



// read the specific exif data
$camera = $image->exif('IFD0.Model');
// read all exif information
$all = $image->exif();
// the exif data block can be queried as well
$camera = $all->get('IFD0.Model');

图片大小

原始图片

Photo by Giuliano Di Paolo on Unsplash

resize 图片缩放

$image->resize(300, 200);

resize 图片缩放

// resize only image height to 200 pixel
$image->resize(height: 200);

resize 图片缩放

resizeDown 图片缩放:不要超过原始图片宽高

$image = $image->resizeDown(2000, 100); // 800 x 100

resizeDown 图片缩放:不要超过原始图片宽高

// resize only image width to 200 pixel and do not exceed the origial width
$image->resizeDown(width: 200);

resizeDown 图片缩放:不要超过原始图片宽高

scale 图片等比例缩放

// scale to fixed height
$image->scale(height: 300);

scale 图片等比例缩放

// scale to 120 x 100 pixel
$image->scale(120, 100);

scale 图片等比例缩放

scaleDown 图片等比例缩放:不要超过原始图片宽高

// scale down to fixed width
$image->scaleDown(width: 200); 

scaleDown 图片等比例缩放:不要超过原始图片宽高

// scale down to fixed height
$image->scaleDown(height: 300);

scaleDown 图片等比例缩放:不要超过原始图片宽高

cover 裁切合併

// crop the best fitting 5:3 (600x360) ratio and resize to 600x360 pixel
$image->cover(600, 360);

cover 裁切合併

// crop the best fitting 1:1 ratio (200x200) and resize to 200x200 pixel
$image->cover(200, 200);

cover 裁切合併

// cover a size of 300x300 and position crop on the left
$image->cover(300, 300, 'left'); // 300 x 300 px

cover 裁切合併

coverDown 裁切合併 :不要超过原始图片宽高

// resize down to 1200x720 (5:3)
$image->coverDown(1200, 720); // 800 x 480 (5:3)

coverDown 裁切合併 :不要超过原始图片宽高

// resize down to 900x900 (1:1)
$image->coverDown(900, 900); // 600 x 600

coverDown 裁切合併 :不要超过原始图片宽高

// resize down to 300x300 (1:1)
$image->coverDown(300, 300);

coverDown 裁切合併 :不要超过原始图片宽高

// resize down to 300x300 (1:1) and position left
$image->coverDown(300, 300, 'left');

coverDown 裁切合併 :不要超过原始图片宽高

pad 等比例缩放,空白处填上颜色

// resize padded to 300 x 300
$image->pad(300, 300, 'ccc');

pad 等比例缩放,空白处填上颜色

// resize padded with positioning
$image->pad(300, 300, position: 'top-left');

pad 等比例缩放,空白处填上颜色

// resize padded with positioning
$image->pad(300, 300, position: 'top-left', background: 'ccc');

pad 等比例缩放,空白处填上颜色

contain 等比例缩放,空白处填上颜色:不要超过原始图片宽高

// resize padded without upsizing
$image->contain(300, 300);

contain 等比例缩放,空白处填上颜色:不要超过原始图片宽高

// resize padded without upsizing
$image->contain(300, 300, 'efefef');

contain 等比例缩放,空白处填上颜色:不要超过原始图片宽高

crop 裁切

// cut out a 200 x 150 pixel cutout at position 45,90
$image->crop(200, 150, 45, 90);

$image->crop(
    width: 200, 
    height: 150, 
    offset_x: 45, 
    offset_y: 90
);

crop 裁切

// crop a 250 x 150 pixel cutout from the bottom-right and move it 30 pixel down
$image->crop(
    width: 200,
    height: 150,
    offset_x: 0,
    offset_y: 30,
    position: 'bottom-right'
);

crop 裁切

$image->crop(
    width: 200,
    height: 150,
    offset_x: 0,
    offset_y: 30,
    position: 'top-right'
);

crop 裁切

resizeCanvas 图片缩放到 Canvas,原始图片不重新取样

// $image->resize(450, 300);
$image->resizeCanvas(500, 500, 'ff0');

resizeCanvas 图片缩放到 Canvas,原始图片不重新取样

图片效果

原始图片

Photo by Giuliano Di Paolo on Unsplash

brightness 亮度

// increase brightness
$image = $image->brightness(37);

brightness 亮度

// increase brightness
$image = $image->brightness(73);

brightness 亮度

contrast 对比

// decreasing the contrast
$image = $image->contrast(73);

contrast 对比

// decreasing the contrast
$image = $image->contrast(-73);

contrast 对比

gamma 校正

// apply gamma correction
$image = $image->gamma(7.3);

gamma 校正

// apply gamma correction
$image = $image->gamma(3.7);

gamma 校正

colorize 颜色校正

// change colors to a blue & green tone
$image = $image->colorize(blue: 15, green: 10);

colorize 颜色校正

// change colors to a blue & green tone
$image = $image->colorize(blue: 37, green: 73);

colorize 颜色校正

greyscale 灰阶

// turn image into a greyscale version
$image = $image->greyscale();

greyscale 灰阶

flop 左右镜像翻转

// mirror image
$image = $image->flop();

flop 左右镜像翻转

flip 上下镜像翻转

$image = $image->flip();

flip 上下镜像翻转

rotate 旋转

// rotate image 45 degrees clockwise 
$image = $image->rotate(-45);

rotate 旋转

blur 模糊

$image = $image->blur(7);

blur 模糊

$image = $image->blur(37);

blur 模糊

$image = $image->blur(73);

blur 模糊

invert 颜色反转

// invert colors
$image = $image->invert();

invert 颜色反转

pixelate 像素化

// apply the pixelation effect
$image = $image->pixelate(7);

pixelate 像素化

// apply the pixelation effect
$image = $image->pixelate(30);

pixelate 像素化

reduceColors 减少颜色数量

// quantize colors to a maximum of 7
$image = $image->reduceColors(7);

reduceColors 减少颜色数量

// quantize colors to a maximum of 37
$image = $image->reduceColors(37);

reduceColors 减少颜色数量

图片输出

quality 品质

$image->save($save_file_path, quality: 70);

quality 品质

$image->save($save_file_path, quality: 30);

quality 品质

$image->save($save_file_path, quality: 10);

quality 品质

原始图片物件 Imagick

可以直接取用原始图片物件,自行设定自己想要的参数

详细函数可参考PHP: ImageMagick - Manual

$manager = ImageManager::imagick();
$image = $manager->read('test.png');

// access Imagick instance directly
$imagick = $image->core()->native();

// use external Imagick function
$imagick->oilPaintImage(4.5);

原始图片物件 Imagick oilPaintImage

Reference

Donate KJ 贊助作者喝咖啡

如果這篇文章對你有幫助的話,可以透過下面支付方式贊助作者喝咖啡,如果有什麼建議或想說的話可以贊助並留言給我
If this article has been helpful to you, you can support the author by treating them to a coffee through the payment options below. If you have any suggestions or comments, feel free to sponsor and leave a message for me!
方式 Method 贊助 Donate
PayPal https://paypal.me/kejyun
綠界 ECPay https://p.ecpay.com.tw/AC218F1
歐付寶 OPay https://payment.opay.tw/Broadcaster/Donate/BD2BD896029F2155041C8C8FAED3A6F8
All rights reserved,未經允許不得隨意轉載
Built with Hugo
主题 StackJimmy 设计