0

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!

wowtom16
  • 23
  • 2
  • 1
    Why do you think these errors are caused by insufficient memory? – shingo Apr 24 '23 at 05:04
  • 2
    The only thing that seems to be wrong here is how you're using the code. Have you read the error messages? – Tangentially Perpendicular Apr 24 '23 at 05:11
  • Please go check https://stackoverflow.com/help/formatting, on how to _properly_ set links here. Both the links you put in your question, have the correct URL as link _text_, but what you actually linked those _to_, is just the StackOverflow start page. (Corrected now by me.) – CBroe Apr 24 '23 at 06:22
  • @shingo, The code i posted here is to help reduce memory load as @Enyby said. But when i try it, i got those error and i need help to fix it. The code from first link (i did not post) working but if i use big image, i will get this `Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 73728 bytes) in...` – wowtom16 Apr 24 '23 at 07:48
  • 1
    So your problem is not how to reduce memory, nor how to fix Enyby‘s function, but how to fix the problems you encounter when using this function. Please update your title and question itself to clarify this point, and include how you use this function. – shingo Apr 24 '23 at 08:17

1 Answers1

1

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));
shingo
  • 18,436
  • 5
  • 23
  • 42
  • It working now, but the corners do not look smooth like Wh1T3h4Ck5's function. Thank you so much for your help. – wowtom16 Apr 24 '23 at 13:49