Skip to content

All topics  /  Laravel

How To Merge Two Images In laravel?

Laravel ·

Hi Friends,

I am going to explain you example of how to merge two images in laravel?. Inside this article we will see the how to combine two images in php / laravel. This article contains a very classified information about the basic concept of laravel | imagecopymerge() Function.

We will see the concept of Join two images intervention/image. This example use to create laravel multiple upload images and displaying on one view.

So let's start following step.

Step : 1 - Installation of Laravel Application


Use this command then download laravel project setup :

composer create-project --prefer-dist laravel/laravel blog

Step 2 : Create route

route/web.php

<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('merge-img', array('as'=> 'merge.img', 'uses' => 'ImageController@mergeImg'));

Step 3 : Create Controller

php artisan make:controller ImageController

app/Http/Controller/ImageController.php

<?php

  
namespace App\Http\Controllers;

  
use Illuminate\Http\Request;

  
class ImageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function mergeImg()
    {
        $image1 = public_path('/images/frame1.jpg');
        $image2 = public_path('/images/frame2.jpg');
        list($width,$height) = getimagesize($image2);
        $image1 = imagecreatefromstring(file_get_contents($image1));
        $image2 = imagecreatefromstring(file_get_contents($image2));
        imagecopymerge($image1,$image2,80,45,30,0,185,140,100);
        header('Content-Type:image/png');
        imagepng($image1);
        $masterImg = imagepng($image1,'merged.png');
        dd($masterImg);
    }
}

Frame : 1

Frame : 2

Output: