im using the function from @Enyby post to rounded corners on image but it does not work. https://stackoverflow.com/a/52626818/14787718
Can anybody help me fix @Enyby's function?
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;
}
when i try:
imagepng(roundCorners("images/test.jpg", 10));
got errors display:
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
There is another working function by @ Wh1T3h4Ck5 : https://stackoverflow.com/a/5767092/20822540 But it using to much memory that why i need @Enyby's function because he said it reduce memory load.
Thank you!
Wh1T3h4Ck5's answer actually explains how to use this function, you should use one of the imagecreatefrom
functions to load the image from the file path, then pass it to Enyby's function.
$src = imagecreatefromjpeg("images/test.jpg");
imagepng(roundCorners($src, 10));