php保存远程图片改变尺寸,JPG转PNG,裁剪圆形

本文详细介绍如何使用PHP从远程URL下载图片,调整大小并将其转换为圆形头像。涉及的技术包括cURL下载图片、GD库调整图片尺寸及生成圆角和透明背景。

//$radius设置圆角弧度

function getyuan(){
$radius = 20;
$img = imagecreatetruecolor($radius, $radius);
$bgcolor = imagecolorallocate($img, 255, 255, 255);
$fgcolor = imagecolorallocate($img, 0, 0, 0);
imagefill($img, 0, 0, $bgcolor);
imagefilledarc($img, $radius, $radius, $radius*2, $radius*2,
180, 270, $fgcolor, IMG_ARC_PIE);
imagecolortransparent($img, $fgcolor);
return $img;
}

function put_file_from_url_content($url, $saveName, $path) {
// 设置运行时间为无限制
set_time_limit ( 0 );

$url = trim ( $url );
$curl = curl_init ();
// 设置你需要抓取的URL
curl_setopt ( $curl, CURLOPT_URL, $url );
// 设置header
curl_setopt ( $curl, CURLOPT_HEADER, 0 );
// 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
// 运行cURL,请求网页
$file = curl_exec ( $curl );
// 关闭URL请求
curl_close ( $curl );
// 将文件写入获得的数据
$filename = $path . $saveName;
$write = @fopen ( $filename, "w" );
if ($write == false) {
return false;
}
if (fwrite ( $write, $file ) == false) {
return false;
}
if (fclose ( $write ) == false) {
return false;
}
}

$url=“www.xxx.com”;

$file2=“headimg/xxx.png”;//第一次保存后路径

put_file_from_url_content($url, "xxx.png", "headimg/");//远程图片地址,保存图片的名字,保存文件夹
list($width, $height) = getimagesize($file2);
$new_width = 40;
$new_height = 40;
$image_wp = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($file2);
imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_wp, $file2, 75);
imagedestroy($image_wp);
/**
* 生成圆形图
*/

// load the source image
$src_image = imagecreatefromjpeg($file2);
if ($src_image === false) {
die('Sorry, can/t load the image');
}
$image_width = imagesx($src_image);
$image_height = imagesy($src_image);
if($image_width < $image_height){
$imageSize = $image_width;
}else{
$imageSize = $image_height;
}


// create a new image, with src_width, src_height, and fill it with transparent color
$image = imagecreatetruecolor($imageSize, $imageSize);
$trans_color = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $trans_color);

// then overwirte the source image to the new created image
imagecopymerge($image, $src_image, 0, 0, 0, 0, $imageSize, $imageSize, 100);

// then just copy all the rounded corner images to the 4 corners
$radius = 20;
// lt
$lt_corner = getyuan();
imagecopymerge($image, $lt_corner, 0, 0, 0, 0, $radius, $radius, 100);
// lb
$lb_corner = imagerotate($lt_corner, 90, $trans_color);
imagecopymerge($image, $lb_corner, 0, $imageSize - $radius, 0, 0,
$radius, $radius, 100);
// rb
$rb_corner = imagerotate($lt_corner, 180, $trans_color);
imagecopymerge($image, $rb_corner, $imageSize - $radius, $imageSize
- $radius, 0, 0, $radius, $radius, 100);
// rt
$rt_corner = imagerotate($lt_corner, 270, $trans_color);
imagecopymerge($image, $rt_corner, $imageSize - $radius, 0, 0, 0, $radius, $radius, 100);

// set the transparency
imagecolortransparent($image,$trans_color);


imagepng($image, $file2);
imagedestroy($image);

 

转载于:https://www.cnblogs.com/yangchong/p/5360314.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值