stefangabos / zebra_image
一个单文件、轻量级的PHP库,用于高效图像处理,具有修改图像和应用滤镜的方法
Requires
- php: >=5.0.0
Requires (Dev)
README
Zebra Image 
一个单文件、轻量级的PHP库,用于高效图像处理,具有修改图像和应用滤镜的方法
使用Zebra Image可以调整图像大小、翻转、旋转、裁剪和锐化。该库支持加载和保存BMP、GIF、JPEG、PNG和WEBP格式的图像,并保留GIF、PNG8、PNG24和WEBP图像的透明度,且无需除了GD2扩展(PHP通常默认预编译的)之外的其他任何外部库。
可以将PHP支持的所有过滤器应用于图像。这些过滤器包括反相、灰度、亮度、对比度、着色、边缘检测、浮雕、高斯模糊、选择性模糊、均值去除、平滑和像素化;可以一次应用多个过滤器以创建自定义过滤器。
代码注释丰富,当PHP的错误报告级别设置为E_ALL时不会生成警告/错误/通知。
使用此库可以将图像调整到指定的宽度和高度,同时通过以下方法之一保持纵横比:
-
图像将被缩放,使其适应给定宽度和高度的框,然后将在框中水平和垂直居中。空白区域将用指定的颜色填充。
-
图像将被缩放,使其可能适应给定宽度和高度的框,但不会限制在给定宽度和高度的框内
-
在将图像缩放,使其宽度和高度分别等于或大于所需宽度和高度后,将从结果图像的左上角裁剪出一个所需宽度和高度的区域。
-
在将图像缩放,使其宽度和高度分别等于或大于所需宽度和高度后,将从结果图像的中心裁剪出一个所需宽度和高度的区域。
以下是使用上述方法之一将800×573像素的图像调整到200×200像素的图像,并保持纵横比的结果
功能
- 可用于调整大小、翻转、旋转、裁剪和锐化图像
- 支持处理BMP、GIF、JPG、PNG和WEBP图像
- 可以将PHP支持的所有过滤器应用于图像:反相、灰度、亮度、对比度、着色、边缘检测、浮雕、高斯模糊、选择性模糊、均值去除、平滑和像素化;可以一次应用多个过滤器以创建自定义过滤器;
- 可以通过自动裁剪图像来调整图像大小,以保持精确的尺寸和纵横比
- 保留GIF、PNG8、PNG24和WEBP图像的透明度
- 支持创建交错JPEG图像
- 代码注释详细,当PHP的错误报告级别设置为E_ALL时不会生成警告/错误/通知
- 可自动修复不正确的图像方向
- 拥有出色的文档
📔 文档
查看出色的文档!
🎂 支持本项目的发展
您的支持非常受欢迎,它使我继续在开源项目上工作的动力。如果您喜欢这个项目,请通过点击页面顶部的星标按钮给它加星。如果您觉得慷慨,也可以通过PayPal购买我一杯咖啡或成为赞助商。 感谢您的支持! 🎉
需求
PHP 5+,捆绑GD 2.0.28+(WEBP支持需要PHP 7.0.0+,BMP支持需要PHP 7.2.0+)
PHP需要编译时启用--enable-exif
(Windows用户在php.ini
中启用php_mbstring.dll
和php_exif.dll
扩展)以自动修复图像旋转,确保图像始终显示正确,无论拍照时相机如何持握。
安装
您可以通过Composer安装Zebra Image
# get the latest stable release composer require stefangabos/zebra_image # get the latest commit composer require stefangabos/zebra_image:dev-master
或者您可以通过下载最新版本,解压它,然后将它包含到您的项目中手动安装
require_once 'Zebra_Image.php';
如何使用
<?php // load the image manipulation class // (you don't need this if you are using Composer) require 'path/to/Zebra_Image.php'; // create a new instance of the class $image = new Zebra_Image(); // if you handle image uploads from users and you have enabled exif-support with --enable-exif // (or, on a Windows machine you have enabled php_mbstring.dll and php_exif.dll in php.ini) // set this property to TRUE in order to fix rotation so you always see images in correct position $image->auto_handle_exif_orientation = false; // indicate a source image (a GIF, PNG, JPEG or WEBP file) $image->source_path = 'path/to/image.png'; // indicate a target image // note that there's no extra property to set in order to specify the target // image's type -simply by writing '.jpg' as extension will instruct the script // to create a 'jpg' file $image->target_path = 'path/to/image.jpg'; // since in this example we're going to have a jpeg file, let's set the output // image's quality $image->jpeg_quality = 100; // some additional properties that can be set // read about them in the documentation $image->preserve_aspect_ratio = true; $image->enlarge_smaller_images = true; $image->preserve_time = true; $image->handle_exif_orientation_tag = true; // resize the image to exactly 100x100 pixels by using the "crop from center" method // (read more in the overview section or in the documentation) // and if there is an error, check what the error is about if (!$image->resize(100, 100, ZEBRA_IMAGE_CROP_CENTER)) { // if there was an error, let's see what the error is about switch ($image->error) { case 1: echo 'Source file could not be found'; break; case 2: echo 'Source file is not readable'; break; case 3: echo 'Could not write target file'; break; case 4: echo 'Unsupported source file type'; break; case 5: echo 'Unsupported target file type'; break; case 6: echo 'GD library version does not support target file format'; break; case 7: echo 'GD library is not installed'; break; case 8: echo '"chmod" command is disabled via configuration'; break; case 9: echo '"exif_read_data" function is not available'; break; } // if no errors } else echo 'Success!';