ImageDealHandler.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Handlers;
  3. //use Image;
  4. class ImageDealHandler
  5. {
  6. // protected $allowed_ext = ["mp3", "wma", "wav",'flac'];
  7. // 裁剪成圆形图片
  8. public function circleImg($imgpath)
  9. {
  10. // var_dump(getimagesize($imgpath));exit();
  11. // header("content-type:image/png");
  12. $ext = pathinfo($imgpath);
  13. $src_img = null;
  14. switch ($ext['extension']) {
  15. case 'jpeg':
  16. $src_img = imagecreatefromjpeg($imgpath);
  17. break;
  18. case 'jpg':
  19. $src_img = imagecreatefromjpeg($imgpath);
  20. break;
  21. case 'png':
  22. $src_img = imagecreatefrompng($imgpath);
  23. break;
  24. }
  25. $wh = @getimagesize($imgpath);
  26. $w = $wh[0];
  27. $h = $wh[1];
  28. $w = min($w, $h);
  29. $h = $w;
  30. $img = imagecreatetruecolor($w, $h);
  31. imagesavealpha($img, true);
  32. //拾取一个完全透明的颜色,最后一个参数127为全透明
  33. $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
  34. imagefill($img, 0, 0, $bg);
  35. $r = $w / 2; //圆半径
  36. $y_x = $r; //圆心X坐标
  37. $y_y = $r; //圆心Y坐标
  38. for ($x = 0; $x < $w; $x++) {
  39. for ($y = 0; $y < $h; $y++) {
  40. $rgbColor = imagecolorat($src_img, $x, $y);
  41. if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
  42. imagesetpixel($img, $x, $y, $rgbColor);
  43. }
  44. }
  45. }
  46. // imagepng($img, $imgpath);
  47. // imagejpeg($img, $imgpath);
  48. // imagedestroy($img);
  49. return $img;
  50. }
  51. }