Home > Backend Development > PHP Problem > How to set the picture to a circular picture in php

How to set the picture to a circular picture in php

藏色散人
Release: 2023-03-13 17:48:01
Original
3007 people have browsed it

php method to set the picture to a circular picture: 1. Create a PHP sample file; 2. Create a transparent picture; 2. Through "function yuan_img($imgpath) {...}" The method is to process the image into a circle.

How to set the picture to a circular picture in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to set pictures in php For round pictures?

php Picture rounding processing:

The php gd library functions used are

imagecolorat
imagesetpixel
Copy after login

First, round the picture Processed into a circle:

The original picture is as follows:


The effect after processing:

Use the following formula to calculate

(x-a)*(x-a)+(y-b)*(y-b)<r2
Copy after login

If the formula is established, it means that the current x, y point is within the circle

x, y is the current coordinate

a, b is the center position of the circle

r is the radius

First create a transparent picture,

and then scan the original image line by line as shown in the figure where the pixels are within the circle Just keep the transparent color if the pixel is not there

function yuan_img($imgpath) {
        $ext     = pathinfo($imgpath);
        $src_img = null;
        switch ($ext[&#39;extension&#39;]) {
        case &#39;jpg&#39;:
            $src_img = imagecreatefromjpeg($imgpath);
            break;
        case &#39;png&#39;:
            $src_img = imagecreatefrompng($imgpath);
            break;
        }
        $wh  = getimagesize($imgpath);
        $w   = $wh[0];
        $h   = $wh[1];
        $w   = min($w, $h);
        $h   = $w;
        $img = imagecreatetruecolor($w, $h);
        //这一句一定要有
        imagesavealpha($img, true);
        //拾取一个完全透明的颜色,最后一个参数127为全透明
        $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
        imagefill($img, 0, 0, $bg);
        $r   = $w / 2; //圆半径
        $y_x = $r; //圆心X坐标
        $y_y = $r; //圆心Y坐标
        for ($x = 0; $x < $w; $x++) {
            for ($y = 0; $y < $h; $y++) {
                $rgbColor = imagecolorat($src_img, $x, $y);
                if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
                    imagesetpixel($img, $x, $y, $rgbColor);
                }
            }
        }
        return $img;
    }
Copy after login

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to set the picture to a circular picture in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template