Home  >  Article  >  Backend Development  >  How to realize rounded corner images using PHP and GD library

How to realize rounded corner images using PHP and GD library

WBOY
WBOYOriginal
2023-07-12 09:21:421808browse

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:

  1. Loading images
    First, we need to use the imagecreatefromXXX() function of the GD library to load images. XXX represents the format of the image, which can be JPEG, PNG or GIF, etc. For example, use the imagecreatefromjpeg() function to load a JPEG image.
  2. Create canvas
    To create a canvas, you can use the imagecreatetruecolor() function. This function accepts two parameters, the width and height of the canvas.
  3. Drawing a rounded rectangle
    Next, we need to use the functions of the GD library to create a rounded rectangle. First use the imagefilledrectangle() function to draw a solid rectangle, and then use the imagefilledellipse() function to draw four arcs to create a rounded corner effect.
  4. Crop the picture
    Next, we need to crop the picture to achieve the rounded corner effect. Use the imagecopy() or imagecopyresampled() function to crop the original image through the rounded rectangle in the canvas.
  5. Save the image
    Finally, we can use the imagejpeg(), imagepng() or imagegif() function to save the processed image to a specific folder.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn