Rounded corners on Images


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. $image_file = $_GET['src'];
  3. $corner_radius = isset($_GET['radius']) ? $_GET['radius'] : 20; // The default corner radius is set to 20px
  4. $topleft = (isset($_GET['topleft']) and $_GET['topleft'] == "no") ? false : true; // Top-left rounded corner is shown by default
  5. $bottomleft = (isset($_GET['bottomleft']) and $_GET['bottomleft'] == "no") ? false : true; // Bottom-left rounded corner is shown by default
  6. $bottomright = (isset($_GET['bottomright']) and $_GET['bottomright'] == "no") ? false : true; // Bottom-right rounded corner is shown by default
  7. $topright = (isset($_GET['topright']) and $_GET['topright'] == "no") ? false : true; // Top-right rounded corner is shown by default
  8. $imagetype=strtolower($_GET['imagetype']);
  9. $backcolor=$_GET['backcolor'];
  10. $endsize=$corner_radius;
  11. $startsize=$endsize*3-1;
  12. $arcsize=$startsize*2+1;
  13.  
  14. if (($imagetype=='jpeg') or ($imagetype=='jpg')) {
  15. $image = imagecreatefromjpeg($image_file);
  16. } else {
  17. if (($imagetype=='GIF') or ($imagetype=='gif')) {
  18. $image = imagecreatefromgif($image_file);
  19. } else {
  20. $image = imagecreatefrompng($image_file);
  21. }
  22. }
  23.  
  24.  
  25. $size = getimagesize($image_file);
  26. // Top-left corner
  27. $background = imagecreatetruecolor($size[0],$size[1]);
  28. imagecopymerge($background, $image, 0, 0, 0, 0, $size[0], $size[1], 100);
  29. $startx=$size[0]*2-1;
  30. $starty=$size[1]*2-1;
  31. $im_temp = imagecreatetruecolor($startx,$starty);
  32. imagecopyresampled($im_temp, $background, 0, 0, 0, 0, $startx, $starty, $size[0], $size[1]);
  33. $bg = imagecolorallocate($im_temp, hexdec(substr($backcolor,0,2)),hexdec(substr($backcolor,2,2)),hexdec(substr($backcolor,4,2)));
  34. $fg = imagecolorallocate($im_temp, hexdec(substr($forecolor,0,2)),hexdec(substr($forecolor,2,2)),hexdec(substr($forecolor,4,2)));
  35.  
  36. if ($topleft == true) {
  37. imagearc($im_temp, $startsize, $startsize, $arcsize, $arcsize, 180,270,$bg);
  38. imagefilltoborder($im_temp,0,0,$bg,$bg);
  39. }
  40.  
  41. // Bottom-left corner
  42. if ($bottomleft == true) {
  43. imagearc($im_temp, $startsize, $starty-$startsize,$arcsize, $arcsize, 90,180,$bg);
  44. imagefilltoborder($im_temp,0,$starty,$bg,$bg);
  45. }
  46.  
  47. // Bottom-right corner
  48. if ($bottomright == true) {
  49. imagearc($im_temp, $startx-$startsize, $starty-$startsize,$arcsize, $arcsize, 0,90,$bg);
  50. imagefilltoborder($im_temp,$startx,$starty,$bg,$bg);
  51. }
  52.  
  53. // Top-right corner
  54. if ($topright == true) {
  55. imagearc($im_temp, $startx-$startsize, $startsize,$arcsize, $arcsize, 270,360,$bg);
  56. imagefilltoborder($im_temp,$startx,0,$bg,$bg);
  57. }
  58.  
  59.  
  60. $newimage = imagecreatetruecolor($size[0],$size[1]);
  61. imagecopyresampled($image, $im_temp, 0, 0, 0, 0, $size[0],$size[1],$startx, $starty);
  62.  
  63. // Output final image
  64. header("Content-type: image/png");
  65. imagepng($image);
  66. imagedestroy($image);
  67. imagedestroy($background);
  68. imagedestroy($im_temp);
  69. ?>

URL: http://it.toolbox.com/blogs/opensource-programming/rounded-corners-on-images-22705

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.