How to Transparent Circular Crop using PHP GD
In this tutorial, we will learn How to Transparent Circular Crop using PHP GD. I will show how to crop an image to a transparent circular using the PHP GD Library. In this example, I will give you an example of How to Transparent Circular Crop using PHP GD. I will give a simple example of creating a PHP Script to crop an image to a transparent circular image:
Transparent Circular Crop
<?php
$upload_dir = 'upload-image/';
$uploaded_file = $upload_dir . 'example.jpg';
$image_s = imagecreatefromstring(file_get_contents($uploaded_file));
$width = imagesx($image_s);
$height = imagesy($image_s);
$newwidth = 460;
$newheight = 480;
$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);
imagecopymerge($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');
// Save the rounded image
$rounded_file = $upload_dir . 'rounded_example.png';
imagepng($image, $rounded_file);
// Free up memory
imagedestroy($image);
imagedestroy($mask);
// Optionally, you can delete the uploaded file if you no longer need it
// @unlink($uploaded_file);
?>