Image Manipulation in Laravel 5

2024-03-04 05:18:17 Tarun Laravel

An introduction to Intervention Image

Intervention Image is an open source PHP library for image handling and manipulation.Intervention image provides image cropping, editing, composing in easier and faster way in Laravel file. In this method coding lines are reduced.It is a library of manipulation of image where you can crop, upload, edit a image.

If you want to create watermark on image or want to write something on image else want to format small and large images, these types of task can be done by intervention in more expressive way. There are many tasks which are performed by intervention easily are:

  • Cropping of image
  • Editing of image
  • Formatting of images
  • Compose image
  • Gamma
  • Inverting of image
  • Creating of image
  • Resizing of image
  • Colorization of image
  • Interlacing of image
  • Rotating
  • Blurring of image
  • Black and white
  • Uploading of image
  • Handling of the image
  • Manipulation of image
  • Reading images
  • Outputting images
  • Drawing images

Let us take an example of editing of an image which will cover most of the functions.

  • Make('name of the image')
  • Resize(height, width)
  • Insert ('the word which you have to write on image or watermark')
  • And finally save it using save function.

Editing an Image in Laravel using Intervention Image

// open an image file
$img = Image::make('public/foo.jpg');
// now you are able to resize the instance
$img->resize(320, 240);
// and insert a watermark for example
// finally we save the image as a new file

Below are the name of the few functions used in editing image in Laravel via intervention Image :

  • Resizecanvas()
  • Trim()
  • Widen()
  • Resize()
  • Fit()
  • Heighten()
  • Flip()
  • Filter()
  • Mask()
  • Invert()
  • Colorize()
  • Gamma()
  • Brightens()
  • Grayscale()
  • Blur()
  • Rotate()
  • Pixelate()
  • Iptc()
  • Width()
  • Height()
  • Text()
  • Rectangle()
  • Pixel()
  • Line()
  • Square()
  • Circle()
  • Ellipse()
  • Exif()
  • Mime()
  • Crop()

These are the different commands used in editing of an image and intervention makes it more easier and reduce the time.

Creating an image

If you want to create an image then firstly you have to call canvas method. By passing height, width of the image as arguments. The colorization can be done in this method if you want to give the background color of the image then it is also possible to pass it as an argument.

  • Canvas(xx,xx,'#xx')
  • Canvas(height, width, '#background color')

Background color of the image is optional.


$img = Image::canvas(800, 600, '#ccc');

Reading of image

Invention of laravel 5 makes it easier to read an image. Now you do not have to do any complicated or annoying work to read an image. For this you have to call the make function and provide the path of the image where the image is stored.

  • Make(‘path of the image’)


$img = Image::make('foo/bar/baz.jpg');

This post is submitted by one of our members. You may submit a new post here.

Related Tricks