Circle crop image using PHP GD Library

0
179

PHP GD (Graphics Draw) is a library in PHP that allows you to create and manipulate images using various drawing functions. It is commonly used for tasks such as creating thumbnails, adding watermarks, processing images, and more. GD provides a set of functions to work with different image formats, including JPEG, PNG, GIF, and others.

Using PHP GD, you can perform a wide range of image processing tasks. It’s essential to be mindful of memory usage, especially when working with large images, and to consider the impact on performance for complex image manipulations. Additionally, newer versions of PHP may provide additional features or improvements related to image processing.

Circle Crop Image using PHP GD

Follow this video for complete guidance:

Source Code :

<?php

$filename = "images/1.jpg";
$image_s = imagecreatefromstring(file_get_contents($filename));
$width = imagesx($image_s);
$height = imagesy($image_s);

$newwidth = 285;
$newheight = 285;

$image = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($image, true);
imagecopyresampled($image, $image_s, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

//create masking
$mask = imagecreatetruecolor($newwidth,$newheight);

$transparent = imagecolorallocate($mask, 255, 0, 0);
imagecolortransparent($mask,$transparent);

imagefilledellipse($mask, $newwidth/2, $newheight/2, $newwidth, $newheight, $transparent);

$red = imagecolorallocate($mask, 0, 0, 0);
imagecopyresampled($image, $mask, 0, 0, 0, 0, $newwidth,$newheight,100);
imagecolortransparent($image,$red);
imagefill($image,0,0,$red);

//output, save and free memory
header('Content-Type: image/png');
imagepng($image);
imagepng($image,'output.png');
imagedestroy($image);
imagedestroy($mask);

 

 

 

Comments are closed.