/**
* 剪切图片为圆形
* @param $picture 图片数据流 比如file_get_contents(imageurl)返回的东东
* @return 图片数据流
*/
private function circular_image($picture) {
$src_img = imagecreatefromstring($picture);
$w = imagesx($src_img);
$h = imagesy($src_img);
$w = min($w, $h);
$h = $w;
$img = imagecreatetruecolor($w, $h);
//这一句一定要有
imagesavealpha($img, true);
//拾取一个完全透明的颜色,最后一个参数127为全透明
$bg = imagecolorallocatealpha($img, 0, 0, 0, 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);
}
}
}
/**
* 如果想要直接输出图片,应该先设header。header("Content-Type: image/png; charset=utf-8");
* 并且去掉缓存区函数
*/
//获取输出缓存,否则imagepng会把图片输出到浏览器
ob_start();
imagepng ( $img );
imagedestroy($img);
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}