Round image corners using intervention image and laravel 5.4
Asked Answered
T

2

12

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...

Tahmosh answered 16/6, 2017 at 11:14 Comment(3)
i think This can help you.. #30094841Mendez
@DCoder Thanks for your comment but I prefer do this using intervention class not using GDTahmosh
Did you try to use circle function .check this linkSaloon
R
6

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

Rubberize answered 2/7, 2017 at 19:15 Comment(2)
Thanks for your answer... It's good trick but I need to round r1.png by iteself... Is there a way to do this???Tahmosh
please check out my question again... I did add a note to thisTahmosh
T
2

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"));
Trapshooting answered 4/7, 2017 at 3:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.