Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pdellepiane's avatar

Crop Image Into Circle Using Image Intervention

Hello, I would like to know if anyone has ever done this or if anyone knows how to do it.

Thanks in advance!

0 likes
7 replies
mezie's avatar

You can crop the image using intervention then use CSS to make the cropped image circular.

.image {
    border-radius: 50%:
}
2 likes
pdellepiane's avatar

@gocanto I'm trying to get it done with that package too, but seems like is not posible.

I also found this: http://stackoverflow.com/questions/30094341/php-gd-how-to-circular-crop-3-square-images-and-merge-into-1-image-maintaining-t and the following function works great:

    function create_circle( $img_path ) {
    // Attribution: by NerdsOfTech

    // Step 1 - Start with image as layer 1 (canvas).
    if (! $img1 = $this->imageCreateFromAny( $img_path )) {
        return FALSE;
    }

    $x=imagesx($img1);
    $y=imagesy($img1);


    // Step 2 - Create a blank image.
    $img2 = imagecreatetruecolor($x, $y);

    $bg = imagecolorallocate($img2, 255,0,255, 127); // wierdo pink background
    // $bg = imagecolorallocate($img2, 0, 0, 0, 127 ); // white background

    imagefill($img2, 0, 0, $bg);
    imagecolortransparent($img2, $bg);

    // Step 3 - Create the ellipse OR circle mask.
    $e = imagecolorallocate($img2, 255, 255, 255); // black mask color

    // Draw a ellipse mask
    imagefilledellipse ($img2, ($x/2), ($y/2), $x, $y, $e);

    // OR
    // Draw a circle mask
    // $r = $x <= $y ? $x : $y; // use smallest side as radius & center shape
    // imagefilledellipse ($img2, ($x/2), ($y/2), $r, $r, $e);

    // Step 4 - Make shape color transparent
    imagecolortransparent($img2, $e);

    // Step 5 - Merge the mask into canvas with 100 percent opacity
    imagecopymerge($img1, $img2, 0, 0, 0, 0, $x, $y, 100);

    // Step 6 - Make outside border color around circle transparent
    imagecolortransparent($img1, $bg);

    /* Clean up memory */
    imagedestroy($img2);

    return $img1;
}

But still trying to make it work with Image Intervention.

pdellepiane's avatar

@gocanto I know the package works frin, its just that the functions it has are not enough to acomplish what I need.

1 like
mane_olawale's avatar

Wow this is old but I know what you want and in my opinion the code you posted is too complex...

You can do this in intervention image by masking the image with a circle shape:

Route::get('circle_image.png', function () {
    # Create you image
    $img = Image::make(storage_path('app/public/image.jpg'));

	// Apply a smart crop 
    $img->fit(1000);

    // create empty canvas with transparent background
    $canvas = Image::canvas(1000, 1000);

    // draw a black circle on it
    $canvas->circle(1000, 500, 500, function ($draw) {
        $draw->background('#000000');
    });

    // Mask your image with the shape
    $img->mask($canvas->encode('png', 75), true);

	// Response with the image or you can as well do whatever you like with it.
    return $img->response('png');
});

This should work well if you have the GD extention installed.

2 likes

Please or to participate in this conversation.