微信的頭像現在要加水印到一張圖片上 弄成 圓形的 怎么裁剪呢
Imagick提供的有 gd庫 怎么做呢
因為要在微信中生成這個圖片發送給用戶 所以必須是生成這樣的圖片的 css的border-radius是只能顯示網頁中 怎么生成一個圖片呢
已找到方法
function resize_img($url,$path='./'){ $imgname = $path.uniqid().'.jpg'; $file = $url; list($width, $height) = getimagesize($file); //獲取原圖尺寸 $percent = (110/$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; } function test($url,$path='./'){ $w = 132; $h=132; // original size 微信默認頭像大小 高132,寬132 $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); imagepng($newpic, $dest_path); imagedestroy($newpic); imagedestroy($src); // unlink($url); return $dest_path; }