常用的图片处理工具有GD,ImageMagick,GraphicsMagick等等。GD就是个阿斗,略过不提;ImageMagick是目前最流行的图片处理工具,它的功能非常丰富;GraphicsMagick的功能略逊于ImageMagick,但是它的效率更强悍,就好比Apache和Nginx一样,一个功能更强,一个效率更胜。
现在更看重效率,所以本文就以GraphicsMagick为例来说说:
对于PHPer来说,有两种使用GraphicsMagick的方式:
1:使用PECL Gmagick扩展。
2:使用GraphicsMagick命令行。
PECL扩展的方式我并不喜欢,一来PECL代码Bug多多,二来PECL扩展的实现,代码写起来很罗嗦:比如缩放一个GIF动画图片,如果你用命令行的方式,一句话就搞定,而用PECL扩展的话,还得先算动画有几帧,再循环处理,很麻烦。所以说我更倾向于使用命令行的方式,虽然命令行操作听起来很“重”,但如果建立若干台图片服务器,通过Gearman连接起来,其实很是很有弹性的。
下面我们就以GraphicsMagick为例,采用命令行的方式来看看如何使用缩略图功能:
先上一个原始图片(input.jpg:160x120),以后的各个例子都会用到它:
BTW:列位看官现在可以咽口水了。
缩略图1
gm convert input.jpg -thumbnail '100x100' output_1.jpg
实际生成的图片大小是:100x75,也就是说说按此命令,会保持图片比例不变生成缩略图。这样很不错,但是有一个潜在的问题:我们不能简单明了的知道图片的最终大小,结果是前端显示的时候,无法设置img标签的width和height属性,如果我没记错的话,一般是推荐设定width和height属性的,否则浏览器渲染起来可能会稍稍慢一点。
缩略图2
gm convert input.jpg -thumbnail '100x100!' output_2.jpg
这次实际生成的图片大小按定义来,但图片变形了,有时候这是不能接受的。
缩略图3
gm convert input.jpg -thumbnail '100x100^' \
-gravity center -extent 100x100 output_3.jpg
这次不仅保证了大小,还保证了比例。不过图片经过了裁剪。
缩略图4
gm convert input.jpg -thumbnail '100x100' \
-background gray -gravity center -extent 100x100 output_4.jpg
这次不仅保证了大小,还保证了比例,同时没有对图片进行任何裁剪,多余的部分按指定颜色进行填充。
缩略图5
gm convert input.jpg -thumbnail '10000@' \
-background gray -gravity center -extent 100x100 output_5.jpg
这次保证了大小和比例,其中的10000就是100x100的乘积,同时在填充和裁剪之间做了一个平衡。
明白了以上几个例子,缩略图基本就能通吃了,肯定有一种会适合你的需求。GraphicsMagick的资料非常少,但好消息是GraphicsMagick和ImageMagick的用法基本兼容,所以你可以通过ImageMagick的资料来套用。
补充:如果想让用户手动裁剪头片的话,imgAreaSelect是个好选择。

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment