0

I have a question about to resize an image in Laravel with intervention.io (Link: http://image.intervention.io/

My question is that I don't understand how to input in my code, can somebody help me with showing the example? Thank you in advance!

MY CODE:

account.blade.php:

@extends('layouts.master')

@section('title')
    Account
@endsection

@section('content')
    <section class="row new-post">
        <div class="col-md-6 col-md-offset-3">
            <header><h3>Your Account</h3></header>
            <form action="{{ route('account.save') }}" method="post" enctype="multipart/form-data">
                <div class="form-group">
                    <label for="first_name">First Name</label>
                    <input type="text" name="first_name" class="form-control" value="{{ $user->first_name }}" id="first_name">
                </div>
                <div class="form-group">
                    <label for="image">Image (only .jpg)</label>
                    <input type="file" name="image" class="form-control" id="image">
                </div>
                <button type="submit" class="btn btn-primary">Save Account</button>
                <input type="hidden" value="{{ Session::token() }}" name="_token">
            </form>
        </div>
    </section>


    @if (Storage::disk('local')->has($user->first_name . '-' . $user->id . '.jpg'))
        <section class="row new-post">
            <div class="col-md-6 col-md-offset-3">
                <img src="{{ route('account.image', ['filename' => $user->first_name . '-' . $user->id . '.jpg']) }}" alt="" class="img-responsive">
            </div>
        </section>

    @endif
@endsection

User.php:

<?php

namespace App;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;

class User extends Model implements Authenticatable
{
    use \Illuminate\Auth\Authenticatable;

    public function posts()
    {
        return $this->hasMany('App\Post');
    }

    public function likes()
    {
        return $this->hasMany('App\Like');
    }
}
Lakhwinder Singh
  • 5,536
  • 5
  • 27
  • 52
Jennifer
  • 1
  • 1
  • 1

2 Answers2

0

Have you written necessary functions in controller for handling your image files ? If answer is yes, then you just install the Intervention Image package on your laravel project and add the code for image resizing in that controller functions. After uploading logic just add necessary lines to resize image in your controller function .
It's something like this

public function resizeImagePost(Request $request)
{
    $this->validate($request, [
        'title' => 'required',
        'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);


    $image = $request->file('image');
    $input['imagename'] = time().'.'.$image->getClientOriginalExtension();


    $destinationPath = public_path('/thumbnail');
    $img = Image::make($image->getRealPath());
    $img->resize(150, 150, function ($constraint) {
        $constraint->aspectRatio();
    })->save($destinationPath.'/'.$input['imagename']);


    $destinationPath = public_path('/images');
    $image->move($destinationPath, $input['imagename']);


    $this->postImage->add($input);


    return back()
        ->with('success','Image Upload successful')
        ->with('imageName',$input['imagename']);
}
Ragib Huda
  • 127
  • 5
0

Here is the intervention installation using composer (Auto detect version)

composer require intervention/image

And about resizing image

$image = $manager->make('public/foo.jpg')->resize(300, 200);

Example : This is in my controller method

/* TOP OF THE FILE */
use Request;
use Intervention\Image\ImageManagerStatic as Image;

public someFunctionname(Request $request)
{
    if ($request->hasFile('image')) {
      $extension = '';
      $files = $request->file('image');

      $mime = Image::make($files->getRealPath())->mime();
      if ($mime == 'image/jpeg') {
          $extension = '.jpg';
      } elseif ($mime == 'image/png') {
          $extension = '.png';
      } elseif ($mime == 'image/jpg') {
          $extension = '.jpg';
      } else {
          $extension = '';
      }

      $originalName = pathinfo($files->getClientOriginalName(), PATHINFO_FILENAME);
      $filename = time() . '_' . $originalName . $extension;
      $storagePath = public_path('uploads/profileImages/');

      if (!File::exists($storagePath)) File::makeDirectory($storagePath, 775);
      Image::make($files->getRealPath())->save($storagePath . '/' . $filename, 100)->resize(500, 500);

      $savePathInDB = asset('uploads/profileImages/' . $filename);

    } else {

      $savePathInDB = asset('uploads/demoImages/demoImage.jpg');

    }
}

Hope That Works. Happy Coding