How To Rotate Image In Laravel Application?

10-Apr-2023

.

Admin

How To Rotate Image In Laravel  Application?

Hi Friends,

I am going to explain you example of laravel rotate image example. Inside this article we will see the laravel is rotating the image when uploaded. This article contains a very classified information about the basic concept of preview and rotate image before upload using laravel.

We will see the concept of rotate, Intervention\Image laravel code examples. In this tutorial, I will show you uploaded image is rotated. We will use laravel is rotating the image when uploaded.

You can use this example with laravel 6, laravel 7 and laravel 8 version. so let's follow bellow tutorial.

Step : 1 - Installation of Laravel Application


Use this command then download laravel project setup :

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

Step : 2 - You have to add package on your laravel project by using composer like

composer require intervention/image

Step 3 : Then you have to add provider path and alias path in config/app.php file like

config/app.php

'providers' => [

Intervention\Image\ImageServiceProvider::class,

]

'aliases' => [

'Image' => Intervention\Image\Facades\Image::class,

]

Step 4 : 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('rotate-image', array('as'=> 'rotate.image', 'uses' => 'ImageController@rotateImage'));

Route::post('rotate-image-store', array('as'=> 'rotate.image.store', 'uses' => 'ImageController@rotateImageStore'));

Step 5 : 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

{

/**

* Rotate Image View

*

* @return void

*/

public function rotateImage(){

return view('index');

}

/**

* Rotate Image Preview

*

* @return void

*/

public function rotateImageStore(Request $request){

$image = $request->file('name');

$file_name = time().'_'.$image->getClientOriginalName();

$parts = pathinfo($file_name);

$extensions = $parts['extension'];

if($extensions == "jpeg"){

$degrees = 90;

$source = imagecreatefromjpeg($image);

$rotate = imagerotate($source, $degrees, 0);

imagejpeg($rotate, "myUpdateImage.jpeg");

return response()->file(public_path('myUpdateImage.jpeg'));

}elseif($extensions == "png"){

$degrees = 90;

$source = imagecreatefrompng($image);

$rotate = imagerotate($source, $degrees, 0);

imagepng($rotate, "myUpdateImage.png");

return response()->file(public_path('myUpdateImage.png'));

}else{

echo "Plz Select jpeg And Png File.";

}

}

}

Step 6 : Create blade file

resources/view/index.blade.php

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Laravel Rotate Image Example - Nicesnippets.com</title>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css">

</head>

<body>

<div class="container">

<div class="row justify-content-md-center mt-5">

<div class="col-md-8 border p-4 mt-5">

<h3>Laravel Rotate Image Example - Nicesnippets.com</h3>

{{ Form::open(array('route' => 'generate.image.text.store','method' => 'post','files' => true ,'class' => 'mt-4')) }}

<div class="form-group">

{{ Form::label('Enter Image:') }}

{{ Form::file('name',array('class' => 'form-control')) }}

</div>

{{ Form::submit('Submit',array('class' => 'btn btn-primary mt-2')); }}

{!! Form::close() !!}

</div>

</div>

</div>

</body>

</html>

Defualt Image:

Output:

I hope it can help you...

#Laravel