使用 PHP GD 在图像上制作圆角

问题描述 投票:0回答:0

我正在使用@Enyby 发布到图像圆角的功能,但它不起作用。 https://stackoverflow.com/a/52626818/14787718

谁能帮我修复@Enyby 的功能?

function roundCorners($source, $radius) {
    $ws = imagesx($source);
    $hs = imagesy($source);

    $corner = $radius + 2;
    $s = $corner*2;

    $src = imagecreatetruecolor($s, $s);
    imagecopy($src, $source, 0, 0, 0, 0, $corner, $corner);
    imagecopy($src, $source, $corner, 0, $ws - $corner, 0, $corner, $corner);
    imagecopy($src, $source, $corner, $corner, $ws - $corner, $hs - $corner, $corner, $corner);
    imagecopy($src, $source, 0, $corner, 0, $hs - $corner, $corner, $corner);

    $q = 8; # change this if you want
    $radius *= $q;

    # find unique color
    do {
        $r = rand(0, 255);
        $g = rand(0, 255);
        $b = rand(0, 255);
    } while (imagecolorexact($src, $r, $g, $b) < 0);

    $ns = $s * $q;

    $img = imagecreatetruecolor($ns, $ns);
    $alphacolor = imagecolorallocatealpha($img, $r, $g, $b, 127);
    imagealphablending($img, false);
    imagefilledrectangle($img, 0, 0, $ns, $ns, $alphacolor);

    imagefill($img, 0, 0, $alphacolor);
    imagecopyresampled($img, $src, 0, 0, 0, 0, $ns, $ns, $s, $s);
    imagedestroy($src);

    imagearc($img, $radius - 1, $radius - 1, $radius * 2, $radius * 2, 180, 270, $alphacolor);
    imagefilltoborder($img, 0, 0, $alphacolor, $alphacolor);
    imagearc($img, $ns - $radius, $radius - 1, $radius * 2, $radius * 2, 270, 0, $alphacolor);
    imagefilltoborder($img, $ns - 1, 0, $alphacolor, $alphacolor);
    imagearc($img, $radius - 1, $ns - $radius, $radius * 2, $radius * 2, 90, 180, $alphacolor);
    imagefilltoborder($img, 0, $ns - 1, $alphacolor, $alphacolor);
    imagearc($img, $ns - $radius, $ns - $radius, $radius * 2, $radius * 2, 0, 90, $alphacolor);
    imagefilltoborder($img, $ns - 1, $ns - 1, $alphacolor, $alphacolor);
    imagealphablending($img, true);
    imagecolortransparent($img, $alphacolor);

    # resize image down
    $dest = imagecreatetruecolor($s, $s);
    imagealphablending($dest, false);
    imagefilledrectangle($dest, 0, 0, $s, $s, $alphacolor);
    imagecopyresampled($dest, $img, 0, 0, 0, 0, $s, $s, $ns, $ns);
    imagedestroy($img);

    # output image
    imagealphablending($source, false);
    imagecopy($source, $dest, 0, 0, 0, 0, $corner, $corner);
    imagecopy($source, $dest, $ws - $corner, 0, $corner, 0, $corner, $corner);
    imagecopy($source, $dest, $ws - $corner, $hs - $corner, $corner, $corner, $corner, $corner);
    imagecopy($source, $dest, 0, $hs - $corner, 0, $corner, $corner, $corner);
    imagealphablending($source, true);
    imagedestroy($dest);

    return $source;
}

当我尝试时:

imagepng(roundCorners("images/test.jpg", 10));

显示错误:

Warning: imagesx() expects parameter 1 to be resource, string given in C:\xampp\htdocs\php\rounded-img.php on line 3

Warning: imagesy() expects parameter 1 to be resource, string given in C:\xampp\htdocs\php\rounded-img.php on line 4

Warning: imagecopy() expects parameter 2 to be resource, string given in C:\xampp\htdocs\php\rounded-img.php on line 10

Warning: imagecopy() expects parameter 2 to be resource, string given in C:\xampp\htdocs\php\rounded-img.php on line 11

Warning: imagecopy() expects parameter 2 to be resource, string given in C:\xampp\htdocs\php\rounded-img.php on line 12

Warning: imagecopy() expects parameter 2 to be resource, string given in C:\xampp\htdocs\php\rounded-img.php on line 13

Warning: imagealphablending() expects parameter 1 to be resource, string given in C:\xampp\htdocs\php\rounded-img.php on line 55

Warning: imagecopy() expects parameter 1 to be resource, string given in C:\xampp\htdocs\php\rounded-img.php on line 56

Warning: imagecopy() expects parameter 1 to be resource, string given in C:\xampp\htdocs\php\rounded-img.php on line 57

Warning: imagecopy() expects parameter 1 to be resource, string given in C:\xampp\htdocs\php\rounded-img.php on line 58

Warning: imagecopy() expects parameter 1 to be resource, string given in C:\xampp\htdocs\php\rounded-img.php on line 59

Warning: imagealphablending() expects parameter 1 to be resource, string given in C:\xampp\htdocs\php\rounded-img.php on line 60

@ Wh1T3h4Ck5 还有另一个工作功能:https://stackoverflow.com/a/5767092/20822540 但它占用了太多内存,这就是为什么我需要@Enyby 的功能,因为他说它可以减少内存负载。

谢谢!

php gd
© www.soinside.com 2019 - 2024. All rights reserved.