大佬教程收集整理的这篇文章主要介绍了GraphicsMagick实现云服务商基础图片处理,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
背景: 搭建图片处理服务的主要目的是为自研云存储提供图片处理能力,参考目前使用的云服务商阿里云、七牛云,搭建图片在线处理服务。本文手把手教会你使用Graphicsmagick工具实现云服务商提供的基础图片处理功能需求。
如果之前本地还没有安装Graphicsmagick
的话,建议现在立刻马上安装一下,然后跟着本文一起玩转图片处理功能!
gm convert ${sourceImgPath} -resize ${width}x${height}${modE} ${distImgPath}
通过图片缩放参数,调整图片大小。
参数 | 定义 |
---|---|
sourceImgPath | 源图片路径 |
width | 指定目标缩放图的宽度 |
height | 指定目标缩放图的高度 |
' ' [lfit](默认值):等比缩放,缩放图限制为指定w和h的矩阵内最大图片'^' [mfit]:等比缩放,缩放图为延伸出指定w与h的矩形框外的最小图片'!' [fixed]:固定宽高,强制缩放'%' [percent]:百分比缩放,将宽度和高度声明视为百分比值,而不是像素长度 | |
distImgPath | 目标图片路径 |
example参考: https://Help.aliyun.com/document_detail/44688.htm?spm=a2c4g.11186623.2.9.742df2eejHaW9d#concept-hxj-c4n-vdb 原图外网访问地址:https://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg
等比缩放
按宽高缩放
gm convert example.jpg -resize 100x100 test.jpg
gm convert example.jpg -resize 100x100^ test.jpg
固定宽高缩放
gm convert example.jpg -resize 100x100! test.jpg
按比例缩放
gm convert example.jpg -resize 50% test.jpg
gm convert ${sourceImgPath} -auto-orient ${distImgPath}
通过自适应方向参数指定原图是否按自适应方向旋转
备注: EXIF(Exchangeable Image File)是 “可交换图像文件” 的缩写,当中包含了专门为数码相机的照片而定制的元数据,可以记录数码照片的拍摄参数、缩略图及其他属性信息,简单来说,Exif 信息是镶嵌在 JPEG/TIFF 图像文件格式内的一组拍摄参数,需要注意的是 EXIF 信息是不支持 png,webp 等图片格式的。
参数 | 定义 |
---|---|
sourceImgPath | 源图片路径 |
distImgPath | 目标图片路径 |
example参考: https://developer.qiniu.com/dora/kb/1560/information-about-photo-exif-rotation-parameters-in-the-understanding-of-orientation 原图外网访问地址:https://cvte-dev-public.seewo.com/shan-test/4354acf302f44722b905d289847c4d58
gm convert example.png -auto-orient test.png
gm convert ${sourceImgPath} -quality ${q} ${distImgPath}
使用原图本身的格式对图片进行压缩,通过质量变换参数,修改原图的质量。
参数 | 定义 |
---|---|
sourceImgPath | 源图片路径 |
q | 压缩比例,压缩后体积并不会严格按照比例缩小,取值为 [1, 100] |
distImgPath | 目标图片路径 |
gm convert example.png -quality 50 test.png
gm convert ${sourceImgPath} ${distImgPath}.${format}
直接设置图片后缀即可。
参数 | 定义 |
---|---|
sourceImgPath | 源图片路径 |
distImgPath | 目标图片路径 |
format | 图片后缀,如:jpg/jpeg/png/webp/bmp/gif/tiff |
gm convert example.png test.jpg
gm convert ${sourceImgPath} -crop ${width}x${height}+${x}+${y} ${distImgPath}
通过自定义裁剪参数,在原图上裁剪指定大小的矩形图片。
参数 | 定义 |
---|---|
sourceImgPath | 源图片路径 |
width | 指定裁剪宽度,[0, 图片宽度],默认为最大值 |
height | 指定裁剪高度,[0, 图片高度],默认为最大值 |
x | 指定裁剪起点横坐标(默认左上角为原点),[0, 图片边界] |
y | 指定裁剪起点纵坐标(默认左上角为原点),[0, 图片边界] |
distImgPath | 目标图片路径 |
example参考: https://Help.aliyun.com/document_detail/44693.html?spm=a2c4g.11186623.6.738.7df8390a36Vp9k 原图外网访问地址:https://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg
从(100,50)开始,裁剪至图片边界
gm convert example.jpg -crop +100+50 test.jpg
从(100, 50)开始,裁剪 100px * 100px 大小的图片
gm convert example.jpg -crop 100x100+100+50 test.jpg
gm identify -format '[output] %w %h %m %bn' ${sourceImgPath}
获取图片相关信息(宽高、格式、大小)。
参数 | 定义 |
---|---|
-format | %w:宽度 %h:高度 %m:图片格式 %b:图片大小 |
sourceImgPath | 源图片路径 |
图片处理命令:gm identify -format '[output] %w %h %m %bn' example.jpg
输出:[output] 400 267 JPEG 21.3Ki
1、阿里云图片处理指南:https://Help.aliyun.com/document_detail/183902.html?spm=a2c4g.11186623.6.746.59677fd8jdjVGq 2、七牛云图片处理指南:https://developer.qiniu.com/dora/3683/img-directions-for-use 3、GM官方文档:http://www.graphicsmagick.org/utilities.html 4、ImageMagick命令:https://www.thisfaner.com/p/imagemagick/
以上是大佬教程为你收集整理的GraphicsMagick实现云服务商基础图片处理全部内容,希望文章能够帮你解决GraphicsMagick实现云服务商基础图片处理所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。