(codeIgniter框架为基础)
/**
* 获取网络图内容
* @param $url 网络图片url
* @return bool
*/
function http_get_data($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
ob_start();
curl_exec($ch);
$return_content = ob_get_contents();
ob_end_clean();
$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return $return_content;
}
/**
* 获取网络图片类型
* @param $url 网络图片url,支持不带后缀名url
* @return bool
*/
private function getNetworkImgType($url)
{
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL, $url); //设置需要获取的URL
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);//设置超时
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //支持https
curl_exec($ch);//执行curl会话
$http_code = curl_getinfo($ch);//获取curl连接资源句柄信息
curl_close($ch);//关闭资源连接
if ($http_code['http_code'] == 200) {
$theImgType = explode('/', $http_code['content_type']);
if ($theImgType[0] == 'image') {
return $theImgType[1];
} else {
return false;
}
} else {
return false;
}
}
/**
* 生成图片文件流
* @param $file 网络图片地址
* @return bool
*/
function createImageFromFile($file)
{
if (preg_match('/http(s)?:\/\//', $file)) {
$fileSuffix = $this->getNetworkImgType($file);
} else {
$fileSuffix = pathinfo($file, PATHINFO_EXTENSION);
}
if (!$fileSuffix) return false;
switch ($fileSuffix) {
case 'jpeg':
if (!strpos($file, 'qnimg.ruwii.com') && !strpos($file, 'upload')) {
// imagecreatefromstring:从字符串中的图像流新建一图像, 返回一个图像标识符,其表达了从给定字符串得来的图像
$theImage = @imagecreatefromstring($this->http_get_data($file));
} else {
$theImage = @imagecreatefromjpeg($file);
if (!$theImage) {
$theImage = @imagecreatefromstring($this->http_get_data($file));
}
}
break;
case 'jpg':
if (!strpos($file, 'qnimg.ruwii.com') && !strpos($file, 'upload')) {
$theImage = @imagecreatefromstring($this->http_get_data($file));
} else {
$theImage = @imagecreatefromjpeg($file);
if (!$theImage) {
$theImage = @imagecreatefromstring($this->http_get_data($file));
}
}
break;
case 'png':
if (!strpos($file, 'qnimg.ruwii.com') && !strpos($file, 'upload')) {
$theImage = @imagecreatefromstring($this->http_get_data($file));
} else {
$theImage = @imagecreatefrompng($file);
if (!$theImage) {
$theImage = @imagecreatefromstring($this->http_get_data($file));
}
}
break;
case 'gif':
if (!strpos($file, 'qnimg.ruwii.com') && !strpos($file, 'upload')) {
$theImage = @imagecreatefromstring($this->http_get_data($file));
} else {
$theImage = @imagecreatefromgif($file);
if (!$theImage) {
$theImage = @imagecreatefromstring($this->http_get_data($file));
}
}
break;
default:
$theImage = @imagecreatefromstring($this->http_get_data($file));
break;
}
return $theImage;
}
/**
* 将图片切割为圆形
* @param imgpath 图片地址(可以为网络地址,也可以为本地图片)
* @return bool
*/
function changeCircularImg($imgpath)
{
$src_img = null;
//如果$imgpath为网络地址
$src_img = $this->createImageFromFile($imgpath);
//如果如果$imgpath为本地图片地址
//$src_img = @imagecreatefromstring(file_get_contents($imgpath));(已经测试可用)
$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);
imagesavealpha($img , true);
$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);
//根据数学公式圆的计算方式 算的 (x-r)(x-r)+(y-r)(y-r)=r*r (x,y坐标点 r半径)
if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
}
}
return $img;
}
php中将图片裁剪为圆形
于 2022-02-10 16:15:30 首次发布