未加星标

A package to easily manipulate images in PHP

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二04 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

Today we released a new package called image that makes manipulation images inphp extremely easy. In this post I’d like to explain why we built it and how it can be used.

Manipulating images in PHP

To manipulate images in PHP there are already a lot of options. You can go hardcore and use the Gd or Imagick functions directly. By using them you get a lot of control, but most of these functions aren’t very developer friendly. If you need something easier to work with try out the Intervention Image library by Oliver Vogel . It provides a nice abstraction and will make working with images a lot easier.

Another option is a package by Jonathan Reinink called Glide . This one is a bit special. Glide can generate images on the fly using url parameters. With Glide installed in your project a cropped version of the image in this bit of html will be automatically generated.

<img src="image.jpg?w=300&h=400&fit=crop" />

It’s API is really nice to work with. Under the hood Glide leverages the aforementioned Intervention Image but it hides some of the complex operations behind easy to use parameters. Unfortunately Glide only exposes it’s API via http, there’s no way to easily programmatorically work with it.

Our image package

Enter spatie/image . Our new package wraps up Glide so all it’s methods can be used using PHP code.

Basic manipulation

In the remainder of this article we are going to manipulate this beautiful photograph taken in New York City.


A package to easily manipulate images in PHP

Let’s start of with a simple manipulation.

Image::load('new-york.jpg') ->sepia() ->blur(50) ->save();

Here’s the result:


A package to easily manipulate images in PHP

Let’s perform crop out that Starbucks storefront.

Image::load('new-york.jpg') ->manualCrop(600, 400, 20, 620) ->save();
A package to easily manipulate images in PHP

Check out the documentation to learn which operations the package support.

Converting to other formats

Imagine you want to convert a jpg to a png . Here’s how to do that.

Image::load('new-york.jpg')->save('converted.png');

It couldn’t be more simpler, the package will just use the extension of the output file to determine the output format.

Applying changes

By default every manipulation will only be applied once to your image. When calling a manipulation method multiple times only the last call will be applied when the image is saved.

Take a look at this code.

Image::load('new-york.jpg') ->brightness(-40) ->brightness(-20) ->save();

The second call to brightness will override the value set by the first call to brightness . So the resulting image will only have it’s brightness reduced by 20%.


A package to easily manipulate images in PHP

The apply method will apply all previous manipulations to the image before continuing with the next manipulations.

Image::load('new-york.jpg') ->brightness(-40) ->apply() ->brightness(-20) ->save();

That code will lower the brightness by 40%, then lower it again by 20%. The result:


A package to easily manipulate images in PHP
Preparing manipulations

In the previous examples all manipulations like brightness and blur were called directly on the Image instance. You could also opt to build up a Manipulations instance.

$manipulations = (new Manipulations()) ->blur(20) ->brightness(-20);

and then you can use that to manipulate a collection of images.

//using Laravel's collect function collect($images)->each(function(Image $image) use ($manipulations) { $image ->manipulate($manipulations) ->save(); }

The manipulate function can also accept a closure.

$image->manipulate(function(Manipulations $manipulations) { $manipulations ->blur(20) ->brightness(-20); }); In closing

You’ll find more examples of how you can use spatie/image in the documentation . Right now we’re using Glide behind the scenes to perform the manipulations, but we’ve built up our package in such a way that, in theory, we could swap Glide out for something else, without making any breaking changes.

If you use Laravel and need a good solution to handle images and other files in your app, then check out our laravel-medialibrary package . We’re releasing a new major version soon where all image manipulations are powered by spatie/image .

Also take a look at the PHP and Laravel packages we’ve previously built. Maybe we’ve made something that you can use in your next project.


A package to easily manipulate images in PHP

Freek Van der Herten

Freek Van der Herten is a partner and developer at Spatie , an Antwerp based company that specializes in creating web apps with Laravel. After hours he writes about modern PHP and Laravel on this blog. When not coding he’s probably rehearsing with his kraut rock band .

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

主题: PHPLaravel
分页:12
转载请注明
本文标题:A package to easily manipulate images in PHP
本站链接:http://www.codesec.net/view/530833.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(14)