HITENDRA PATEL

MERGE MULTIPLE IMAGES INTO ONE IN LARAVEL

Published 4 years ago 2431 Views
image

In this tutorial we are going to merge multiple images into one image.

We’re going to use intervention image package for that.

Step 1 : Install Package

Install following package

composer require intervention/image

Step 2 : Add provider

Add service provider & alias in config file

<?php
return [
   "providers" => [
       Intervention\Image\ImageServiceProvider::class,
   ],
   "aliases"   => [
       "Image" => Intervention\Image\Facades\Image::class,
   ],
];

Step 3 : Create controller

create new controller & past below code.

<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Image;

class ImageController extends Controller {
	public $_ImgPath = "/app/public/image/";
	public function stepFirst(Request $_request) {
		ini_set("memory_limit", -1);
		/*...images name in array...*/
		if ($_request->has("images") && !empty($_request->images)) {
			$imageNameString = $_request->images[0];
			/*...if multiple images...*/
			if (count($_request->images) > 1) {
				$allWidth        = $allHeight        = [];
				$canvasWidth     = $canvasHeight     = 0;
				$imageNameString = Str::random(30) . ".png";
				/*...get all images height & width for make/merger new image...*/
				foreach ($_request->images as $imagesKey => $imagesName) {
					$allImg      = Image::make(storage_path($this->_ImgPath . "/" . $imagesName));
					$allWidth[]  = $allImg->width();
					$imgHeight   = $allImg->height();
					$allHeight[] = $imgHeight;
					$canvasHeight += $imgHeight;
				}
				/*...max width...*/
				$canvasWidth = max($allWidth);
				/*...make empty canvas with max height width...*/
				$img = Image::canvas($canvasWidth, $canvasHeight);
				/*...save...*/
				$img->save(storage_path($this->_ImgPath . "/" . $imageNameString));
				/*...append images to canvas...*/
				foreach ($_request->images as $imagesKey => $imagesName) {
					/*...get offset for append image at bottom of canvas...*/
					$totalWidth = $totalHeight = 0;
					for ($i = 0; $i < $imagesKey; $i++) {
						$totalWidth += $allWidth[$i];
						$totalHeight += $allHeight[$i];
					}
					/*...get canvas...*/
					$img = Image::make(storage_path($this->_ImgPath . "/" . $imageNameString));
					/*...append image at top with offset...*/
					$appendImg = Image::make(storage_path($this->_ImgPath . "/" . $imagesName));
					$img->insert($appendImg, "top", 0, $totalHeight);
					/*...save...*/
					$img->save(storage_path($this->_ImgPath . "/" . $imageNameString));
				}
			}
		}
		return redirect()->back();
	}
}

That's all friends.

A BLOG ABOUT DEVELOPMENT

Subscribe and get my latest blog post in your inbox.
image
DIGITAL OCEAN SETUP LARAVEL

Install & configure laravel project into live digital ocean server.

image
LARAVEL PUSH NOTIFICATION

Laravel push notification for devices with FCM

image
INSTALL COMPOSER ON MACOS

Install Composer on MacOS