1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- <?php
- namespace App\Handlers;
- //use Image;
- class ImageDealHandler
- {
- // protected $allowed_ext = ["mp3", "wma", "wav",'flac'];
- // 裁剪成圆形图片
- public function circleImg($imgpath)
- {
- // var_dump(getimagesize($imgpath));exit();
- // header("content-type:image/png");
- $ext = pathinfo($imgpath);
- $src_img = null;
- switch ($ext['extension']) {
- case 'jpeg':
- $src_img = imagecreatefromjpeg($imgpath);
- break;
- case 'jpg':
- $src_img = imagecreatefromjpeg($imgpath);
- break;
- case 'png':
- $src_img = imagecreatefrompng($imgpath);
- break;
- }
- $wh = @getimagesize($imgpath);
- $w = $wh[0];
- $h = $wh[1];
- $w = min($w, $h);
- $h = $w;
- $img = imagecreatetruecolor($w, $h);
- imagesavealpha($img, true);
- //拾取一个完全透明的颜色,最后一个参数127为全透明
- $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
- imagefill($img, 0, 0, $bg);
- $r = $w / 2; //圆半径
- $y_x = $r; //圆心X坐标
- $y_y = $r; //圆心Y坐标
- for ($x = 0; $x < $w; $x++) {
- for ($y = 0; $y < $h; $y++) {
- $rgbColor = imagecolorat($src_img, $x, $y);
- if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
- imagesetpixel($img, $x, $y, $rgbColor);
- }
- }
- }
- // imagepng($img, $imgpath);
- // imagejpeg($img, $imgpath);
- // imagedestroy($img);
- return $img;
- }
- }
|