Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round image corners using intervention image and laravel 5.4

I need insert some images to another image using intervention image and laravel.

This is my main image:

main image

And these are my images to insert to main image:

r1 image

r2 image

And finally this image after insert:

final image sample

Well, I use this code to make this:

    $img        = Image::make(asset('images/cover.png' ) )->encode('jpg', 15);

    $token      = Session::get('_token');

    $imgWidth   = $img->width();
    $imgHeight  = $img->height();

    $coverImages = Storage::allFiles('public/' . $token . '/cover');

    $r1 = Image::make(asset('storage/' . $token . '/cover/r1.png') );
    $r2 = Image::make(asset('storage/' . $token . '/cover/r2.png') );

    $r1->resize(80, 180, function ($constraint){
        $constraint->aspectRatio();
    });

    $r2->resize(80, 180, function ($constraint){
        $constraint->aspectRatio();
    });

    $img->insert($r1, 'top-left', 190, 175);
    $img->insert($r2, 'top-left', 290, 175);

    $img->save( public_path("storage/{$token}/111111.png"));

Now I need to round r1.png and r2.png corners to fit main image.

Do you know how can I do this?

Thanks in Advance

NOTE :

Thanks to @Pascal Meunier, but

I need to round r1.png corners by itself, because I have to save rounded image in another place again for some reasons...

like image 637
Kiyarash Avatar asked Jun 16 '17 11:06

Kiyarash


2 Answers

I have not try this myself, but it could be done using mask method from the Intervention class. With this you would need another image of a white rounded rectangle with a black background for the mask.

it should work like this:

$img        = Image::make(asset('images/cover.png' ) )->encode('jpg', 15);

$token      = Session::get('_token');

$imgWidth   = $img->width();
$imgHeight  = $img->height();

$coverImages = Storage::allFiles('public/' . $token . '/cover');

$r1 = Image::make(asset('storage/' . $token . '/cover/r1.png') );
$r2 = Image::make(asset('storage/' . $token . '/cover/r2.png') );

$r1->resize(80, 180, function ($constraint){
    $constraint->aspectRatio();
})->mask('public/mask.png');

$r2->resize(80, 180, function ($constraint){
    $constraint->aspectRatio();
})->mask('public/mask.png');

$img->insert($r1, 'top-left', 190, 175);
$img->insert($r2, 'top-left', 290, 175);

$img->save( public_path("storage/{$token}/111111.png"));
like image 129
am05mhz Avatar answered Nov 11 '22 18:11

am05mhz


I found a way, by using a canvas and inserting r1 and r2 before the main image.

$cover = Image::make('main.png');
$r1 = Image::make('r1.png');
$r2 = Image::make('r2.png');

$r1->resize(80, 180, function ($constraint){
    $constraint->aspectRatio();
});

$r2->resize(80, 180, function ($constraint){
    $constraint->aspectRatio();
});

$canvas = Image::canvas(746, 738);

$canvas->insert($r1, 'top-left', 190, 175);
$canvas->insert($r2, 'top-left', 290, 175);
$canvas->insert($cover);

$canvas->save('final.png');

The result looks like this.

enter image description here

like image 37
Pascal Meunier Avatar answered Nov 11 '22 18:11

Pascal Meunier