Home > Article > Backend Development > How to realize rounded corner images using PHP and GD library
How to implement rounded corner images using PHP and GD libraries
Introduction
In web design, sometimes it is necessary to use rounded corner images to beautify the appearance of the page. This article will introduce how to use PHP and GD library to implement rounded images.
The GD library is one of the PHP extension libraries and provides a series of functions for image processing. By using the GD library, we can crop, resize, add filters, etc. to images. To achieve rounded images, we need to use some functions in the GD library for image processing.
Steps
The following are the specific steps to implement rounded corner images:
Code example
The following is a sample code to implement rounded images through PHP and GD library:
// Loading images
$source = imagecreatefromjpeg('source.jpg');
// Get the image size
$source_width = imagesx($source);
$source_height = imagesy($source);
// Create canvas
$canvas = imagecreatetruecolor($source_width, $source_height);
// Create background color
$background = imagecolorallocate($canvas, 255, 255 , 255);
// Fill the background color
imagefill($canvas, 0, 0, $background);
// Create a rounded rectangle
$radius = 50 ; // Radius of the fillet
$corner_width = $radius * 2;
$corner_height = $radius * 2;
// Upper left corner
imagefilledellipse($canvas, $radius, $radius, $corner_width, $corner_height, $background);
// Upper right corner
imagefilledellipse($canvas, $source_width - $radius, $radius, $corner_width, $corner_height, $background);
// Lower left corner
imagefilledellipse($canvas, $radius, $source_height - $radius, $corner_width, $corner_height, $background);
// Lower right corner
imagefilledellipse($canvas, $source_width - $ radius, $source_height - $radius, $corner_width, $corner_height, $background);
// Crop the image
imagecopy($canvas, $source, $radius, 0, $radius, 0, $ source_width - $corner_width, $source_height); // Top
imagecopy($canvas, $source, 0, $radius, 0, $radius, $source_width, $source_height - $corner_height); // Left side
imagecopy($canvas, $source, $source_width - $corner_width, $radius, $source_width - $corner_width, $radius, $corner_width, $source_height - $corner_height); // Right side
imagecopy($canvas, $source , $radius, $source_height - $corner_height, $radius, $source_height - $corner_height, $source_width - $corner_width, $corner_height); // Bottom
// Save image
imagejpeg($canvas, 'output.jpg');
// Release memory
imagedestroy($source);
imagedestroy($canvas);
?>
Summary
By implementing the above steps, we can use PHP and GD libraries to create rounded images. By using the functions of the GD library, we can load images, create canvases, draw rounded rectangles, crop images and finally save the results. This method can be used to create various rounded corner images, adding more beauty to web design.
The above is the detailed content of How to realize rounded corner images using PHP and GD library. For more information, please follow other related articles on the PHP Chinese website!