首页 > 开发 > PHP > 正文

php生成圆角图片的方法

2024-05-04 21:56:05
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了php生成圆角图片的方法,涉及php操作图片的技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了php生成圆角图片的方法。分享给大家供大家参考。具体如下:

  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. if (($imagetype=='jpeg') or ($imagetype=='jpg')) { 
  14. $image = imagecreatefromjpeg($image_file); 
  15. } else { 
  16. if (($imagetype=='GIF') or ($imagetype=='gif')) { 
  17. $image = imagecreatefromgif($image_file); 
  18. } else { 
  19. $image = imagecreatefrompng($image_file); 
  20. } 
  21. } 
  22. $size = getimagesize($image_file); 
  23. // Top-left corner 
  24. $background = imagecreatetruecolor($size[0],$size[1]); 
  25. imagecopymerge($background,$image,0,0,0,0,$size[0],$size[1],100); 
  26. $startx=$size[0]*2-1; 
  27. $starty=$size[1]*2-1; 
  28. $im_temp = imagecreatetruecolor($startx,$starty); 
  29. imagecopyresampled($im_temp, $background, 0, 0, 0, 0, $startx, $starty, $size[0], $size[1]); 
  30. $bg = imagecolorallocate($im_temp, hexdec(substr($backcolor,0,2)),hexdec(substr($backcolor,2,2)),hexdec(substr($backcolor,4,2))); 
  31. $fg = imagecolorallocate($im_temp, hexdec(substr($forecolor,0,2)),hexdec(substr($forecolor,2,2)),hexdec(substr($forecolor,4,2))); 
  32. if ($topleft == true) { 
  33. imagearc($im_temp, $startsize, $startsize, $arcsize, $arcsize, 180,270,$bg); 
  34. imagefilltoborder($im_temp,0,0,$bg,$bg); 
  35. } 
  36. // Bottom-left corner 
  37. if ($bottomleft == true) { 
  38. imagearc($im_temp,$startsize,$starty-$startsize,$arcsize,$arcsize,90,180,$bg); 
  39. imagefilltoborder($im_temp,0,$starty,$bg,$bg); 
  40. } 
  41. // Bottom-right corner 
  42. if ($bottomright == true) { 
  43. imagearc($im_temp, $startx-$startsize, $starty-$startsize,$arcsize, $arcsize, 0,90,$bg); 
  44. imagefilltoborder($im_temp,$startx,$starty,$bg,$bg); 
  45. } 
  46. // Top-right corner 
  47. if ($topright == true) { 
  48. imagearc($im_temp, $startx-$startsize, $startsize,$arcsize, $arcsize, 270,360,$bg); 
  49. imagefilltoborder($im_temp,$startx,0,$bg,$bg); 
  50. } 
  51. $newimage = imagecreatetruecolor($size[0],$size[1]); 
  52. imagecopyresampled($image,$im_temp,0,0,0,0,$size[0],$size[1],$startx,$starty); 
  53. // Output final image 
  54. header("Content-type: image/png"); 
  55. imagepng($image); 
  56. imagedestroy($image); 
  57. imagedestroy($background); 
  58. imagedestroy($im_temp); 
  59. ?> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表