🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
php通用修改裁切、圆角、合并图片: ``` <?php namespace Home\Controller; use Think\Controller; class IndexController extends Controller { /** * @todo : 本函数用于 将方形的图片压缩后 * 再裁减成圆形 做成logo * 与背景图合并 * @return 返回url */ public function index1(){ //头像 $headimgurl = 'public/log.jpg'; //背景图 $bgurl = 'public/a.png'; $imgs['dst'] = $bgurl; //第一步 压缩图片 $imggzip = $this->resize_img($headimgurl); //第二步 裁减成圆角图片 $imgs['src'] = $this->test($imggzip); //第三步 合并图片 $dest = $this->mergerImg($imgs); } public function resize_img($url,$sizess){ $path='./'; $imgname = $path.uniqid().'.jpg'; $file = $url; list($width, $height) = getimagesize($file); //获取原图尺寸 $percent = ($sizess/$width); //缩放尺寸 $newwidth = $width * $percent; $newheight = $height * $percent; $src_im = imagecreatefromjpeg($file); $dst_im = imagecreatetruecolor($newwidth, $newheight); imagecopyresized($dst_im, $src_im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); imagejpeg($dst_im, $imgname); //输出压缩后的图片 imagedestroy($dst_im); imagedestroy($src_im); return $imgname; } //裁切图片 public function resize_jpg($filename,$sizess){ // $percent = 1.5; list($width, $height) = getimagesize($filename); $percent = ($sizess/$width); $new_width = $width * $percent; $new_height = $height * $percent; $image_p = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); $image_name = time() . '.jpg'; imagejpeg($image_p,$image_name); imagedestroy($image_p); imagedestroy($image); return $image_name; } //第一步生成圆角图片 public function test($url,$path='./'){ // $w = 110; $h=110; // original size list($w, $h) = getimagesize($url); //获取原图尺寸 $original_path= $url; $dest_path = $path.uniqid().'.png'; $src = imagecreatefromstring(file_get_contents($original_path)); $newpic = imagecreatetruecolor($w,$h); imagealphablending($newpic,false); $transparent = imagecolorallocatealpha($newpic, 0, 0, 0, 127); $r=$w/2; for($x=0;$x<$w;$x++) for($y=0;$y<$h;$y++){ $c = imagecolorat($src,$x,$y); $_x = $x - $w/2; $_y = $y - $h/2; if((($_x*$_x) + ($_y*$_y)) < ($r*$r)){ imagesetpixel($newpic,$x,$y,$c); }else{ imagesetpixel($newpic,$x,$y,$transparent); } } imagesavealpha($newpic, true); // header('Content-Type: image/png'); imagepng($newpic, $dest_path); imagedestroy($newpic); imagedestroy($src); // unlink($url); return $dest_path; } //php 合并图片 public function mergerImg($imgs,$path='./') { $imgname = $path.rand(1000,9999).time().'.jpg'; list($max_width, $max_height) = getimagesize($imgs['dst']); $dests = imagecreatetruecolor($max_width, $max_height); $dst_im = imagecreatefrompng($imgs['dst']); imagecopy($dests,$dst_im,0,0,0,0,$max_width,$max_height); imagedestroy($dst_im); $src_im = imagecreatefrompng($imgs['src']); $src_info = getimagesize($imgs['src']); print_r($imgname); //png合并必须使用imagecopy,否则透明部分无法透明 imagecopy($dests,$src_im,500,202,0,0,$src_info[0],$src_info[1]); imagedestroy($src_im); imagejpeg($dests,$imgname); // unlink($imgs['dst']); unlink($imgs['src']); return $imgname; } public function index(){ $imgname = './'.rand(1000,9999).time().'.jpg'; //头像 $src = 'public/log.jpg'; //背景图 $dst = 'data:image/jpeg;base64,/9j/4AAQSkZJRg';//图片二进制流文件 $dst = 'public/ceshi.jpg'; //得到原始图片信息 $dst_im = $this->imagecretefrom_type($dst); $dst_info = getimagesize($dst); //水印图像 $srcs = 'public/log.jpg'; $srcsa = $this->resize_jpg($srcs,660); $src = $this->yj_img($srcsa); $src_im = $this->imagecretefrom_type($src); $src_info = getimagesize($src); //水印透明度 $alpha = 100; imagealphablending($src_im, true); imagesavealpha($src_im, false); //合并水印图片 imagecopy($dst_im,$src_im,$dst_info[0]/2-$src_info[0]/2, $dst_info[1]/2-$src_info[1]/2,0,0,$src_info[0], $src_info[1] ); header('Content-Type: image/jpeg'); header('Content-Type: image/png'); //输出合并后水印图片 imagejpeg($dst_im,$imgname); $PSize = filesize($imgname); $picturedata = fread(fopen($imgname, "r"), $PSize); imagedestroy($dst_im); imagedestroy($src_im); unlink($src); unlink($srcsa); echo $picturedata; } public function imagecretefrom_type($imgpath){ $ext = pathinfo($imgpath); $src_img = null; switch ($ext['extension']) { case 'jpg': $src_img = imagecreatefromjpeg($imgpath); break; case 'png': $src_img = imagecreatefrompng($imgpath); break; } return $src_img; } public function yj_img($imgpath){ $imgname = rand(1000,9999).uniqid().'.png'; $ext = pathinfo($imgpath); $src_img = null; switch ($ext['extension']) { 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); } } } imagesavealpha($img, true); imagepng($img, $imgname); imagedestroy($img); imagedestroy($src_img); return $imgname; } public function curl_post_https($url,$data){ // 模拟提交数据函数 $curl = curl_init(); // 启动一个CURL会话 curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在 curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求 curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包 curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环 curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 $tmpInfo = curl_exec($curl); // 执行操作 if (curl_errno($curl)) { echo 'Errno'.curl_error($curl);//捕抓异常 } curl_close($curl); // 关闭CURL会话 return $tmpInfo; // 返回数据,json格式 } public function index2(){ $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=52_GwppN6xcxomP3F2vufbO-cQCSvH4_W901vq9mE8YrZ_R7Kv-wycOFUpUlAt2dtHtXQ7QChMsvasKJln8D3ob66n_nl7csTbVsfXrwe17duZ3TUcKB_1TL2rQXsr0ehdmQntqzv_S8n89RHd6JMDeAFAQTK'; $arr['page'] = 'pages/index/index'; $arr['scene'] = 'a=1'; $arr['check_path'] = true; $arr['env_version'] = 'release'; $datas = json_encode($arr); $data = $this->curl_post_https($url,$datas); print_r(json_encode($data)); } } ``` 运行以下代码时: ~~~lisp imagecreatefrompng('http://imgur.com/NUl4v.png'); ~~~ 我得到一个错误: > **PHP警告**:imagecreatefrompng()*\[function.imagecreatefrompng\]*:无法读取/home/test/...中的图像数据 PNG文件似乎很好-我可以用不同的编辑器打开它,并且Unix`file`命令报告它是: > `PNG image, 640 x 360, 8-bit/color RGB, non-interlaced` 我不知道为什么,但是以下方法可行: ~~~reasonml imagecreatefromstring(file_get_contents('http://imgur.com/NUl4v.png')); ~~~