This paper mainly introduces the installation, use and use of the intervention/image Image processing expansion pack in Laravel, and the solution to the pits that can be encountered in using the sample code, which is introduced in detail in this article, which has a certain reference learning value to everyone's study or work. The friends who need to study together with the small series. We hope to help you.
Objective
Intervention/image is a custom image processing tool for Laravel that provides a set of easy-to-express ways to create and edit images.
Demo
Demo
Demo Run
Please refer to the documentation on how to quickly run a Laravel project with Homestead.
Article overview
The following is a detailed explanation.
1. Installation
1). Install with composer:
Composer require intervention/image
The above command will
2). Modify app/config/app.php Add ServiceProvider:
Add the following code to the providers array ' providers ' = [//...] Intervention\image\imageserviceprovider::class,//...],//add the following code to the aliases array ' aliases ' + = [//... ' Image ' = intervention\image\facades\image::class,//...],
2. Configuration of Image Processing library
This extension package uses PHP's GD library by default for image processing, but because the GD library is less efficient at processing images than the ImageMagick library, it is recommended to replace the ImageMagick library for image processing.
Before you start, you have to make sure that GD or Imagick is installed locally.
When using intervention Image, you only need to pass an array parameter to Imagemanager to accomplish the mutual switch between GD and Imagick library.
As shown below:
Introduction of composer Autoloadrequire ' vendor/autoload.php ';//import Intervention Image Manager classuse intervention\image\ imagemanager;//creates an Image manager instance by specifying driver $manager = new Imagemanager (Array (' Driver ' = ' imagick '));//FINAL IMA creation GE instance $image = $manager->make (' public/foo.jpg ')->resize (300, 200);
Alternatively, you can use the static version of Imagemanager, as shown below:
Introduction of composer Autoloadrequire ' vendor/autoload.php ';//import Intervention Image Manager classuse intervention\image\ Imagemanagerstatic as image;//creates an Image manager instance by specifying driver (using GD by default) image::configure (Array (' Driver ' = ' imagick ' );//Create image instance $image = Image::make (' public/foo.jpg ')->resize (300, 200);
To generate the config/image.php configuration file:
PHP artisan vendor:publish--provider= "Intervention\image\imageserviceproviderlaravel5"
After running the above command, the config/image.php configuration file is generated in the project, the file is opened and the driver is modified to Imagick:
Return Array (' driver ' = ' imagick ');
In this case, the expansion package is installed successfully!
3. Basic usage
Modifies the size of the specified picture $img = Image::make (' images/avatar.jpg ')->resize (200, 200);//insert watermark, watermark position in the lower right corner of the original picture, 10 pixels from the bottom margin, 15 pixels from the right margin $i Mg->insert (' images/watermark.png ', ' bottom-right ', 15, 10);//Save the processed picture back to the other path $img->save (' Images/new_ Avatar.jpg ');/* The above logic can be done by chaining expressions */$img = Image::make (' images/avatar.jpg ')->resize ("->insert") images/ New_avatar.jpg ', ' bottom-right ', 15, 10);
4. Featured Features
In addition to the basic usage described above, this expansion pack also supports:
For more examples, please refer to the official documentation.
A small pit in Intervention/image and the method of its crack
In fact, INTERVENTION/IAMGE has been used for some time, its API design is very concise, the document is very comprehensive, very handy.
But recently there was a small hole in the air. Because I need to synthesize the QR code with the avatar, I use Image::make ($AVATARURL) (where the $AVATARURL is a link to the avatar) to generate the avatar and then synthesize it into the QR code image (including some other actions, such as using the template background, writing the text).
After the completion of the operation, the discovery is quite slow, the average time is about 23 seconds. Originally thought is because the synthesis process carries on the operation more, the size is bigger, originally should be this speed. But later on, trying to optimize, even if it doesn't improve speed, at least to figure out what the reason is so time-consuming.
This pass toss down, find out the truth unexpectedly with the synthesis operation of how much, size does not have much relation. And the key is the pose I created for my avatar data.
In order to illustrate this problem, the following code was deliberately written to compare.
Record start Time $starttimestamp = Microtime (true); $url = ' http://wx.qlogo.cn/mmopen/ Xxt9tiaj1ibf06tnrcmjqads4opdhvqlgulzhpqkrlvujyzicvjw4iaoalpskis0kpz3f6864zzibyobyiaucuqsrdp4pftndyipxw/0 '; $ Avatar = \image::make ($url);//record end time $endtimestamp = Microtime (true); info ($startTimestamp); info ($endTimestamp); info ($endTimestamp-$startTimestamp);
The above code uses the form of Image::make ($url) to generate the avatar directly from the URL. From the recorded log data, the time-consuming is basically about 16 seconds.
Later, thought of a new posture, in fact, is in the process of trying to optimize the thought of tossing. See the following code:
$startTimestamp = Microtime (true); $client = new \guzzlehttp\client (); $url = ' http://wx.qlogo.cn/mmopen/ Xxt9tiaj1ibf06tnrcmjqads4opdhvqlgulzhpqkrlvujyzicvjw4iaoalpskis0kpz3f6864zzibyobyiaucuqsrdp4pftndyipxw/0 '; $ Avatarresponse = $client->get ($url); $avatar = \image::make ($avatarResponse->getbody ()->getcontents ()); $ Endtimestamp = Microtime (true); info ($startTimestamp); info ($endTimestamp); info ($endTimestamp-$startTimestamp);
Here I'll use guzzlehttp to get the avatar, and then use Image::make ($data) to create the avatar.
Notice that it's going to climax ... sunglasses
Take a look at the following log, three times the average time is about 0.07 seconds, and the previous 16 seconds compared to 200 times worse.
As to why this phenomenon occurs, I do not understand, but it is undoubtedly a bit more useful and small experience.
Related recommendations:
A detailed laravel localization module
Detailed description of how to get route parameters in Laravel
Detailed description of loading process and principle of facade in laravel