你所在的位置:首 页 >> 前台技术 >> 详细新闻页面


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

作者:zhirong1230 创建时间:2018-03-19 阅读次数:286


//$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);

 



    关键词(keywords):php保存远程图片改变尺寸,JPG转PNG,裁剪圆形

分享到: 更多


前一篇: 通过PHP实现PNG转JPG            后一篇:jquery获取kineditor中内容

phpchina   php爱好者   php100    中国网管联盟   LAMP兄弟连   河北联合大学   胜芳趣团网   rss 联系我们 问题反馈
版权所有@:ABCMS新闻发布系统!
建议使用ie6、ie8和 ff 浏览器进行浏览 | 建议分辨率:1024x768
地址:唐山市路北区高新技术产业园区龙华道128号 | 邮编:63000| 邮箱:zhirong1230@yeah.net