Php: how to add a circular logo icon to the background image. This article describes how to add a circular logo icon to the background image of php. We will share this with you for your reference. The details are as follows:
Next steps:
There are three steps in total:
1. compress the logo into a fixed-size square image
2. convert the logo into a circular logo
3. Merge the logo and background image
If you don't talk much about it, go directly to the code:
Load-> library ('curl');}/*** @ todo: this function is used to compress a square image and then cut it into a circle to form a logo * and merge it with the background image * @ return returns the url */public function index () {// Avatar $ headimgurl = 'a.jpg '; // background image $ bgurl = '. /aa.png '; $ imgs ['dst'] = $ bgurl; // The first step is to compress the image $ imggzip = $ this-> resize_img ($ headimgurl ); // Step 2 cut the image into a rounded corner $ imgs ['src'] = $ this-> test ($ imggzip ); // merge the image in step 3 $ dest = $ this-> mergerImg ($ imgs);} public function resize_img ($ url, $ path = '. /') {$ imgname = paipath.uniqid().'.jpg'; $ file = $ url; list ($ width, $ height) = getimagesize ($ file ); // Obtain the source image size $ percent = (110/$ width); // scale the size $ newwidth = $ width * $ percent; $ newheight = $ height * $ percent; $ src_im = imagecreatefromjpeg ($ file); $ dst_im = bytes ($ newwidth, $ newheight); imagecopyresized ($ dst_im, $ src_im, 0, 0, 0, 0, $ newwidth, $ newheight, $ width, $ height); imagejpeg ($ dst_im, $ imgname); // output the compressed image imagedestroy ($ dst_im); imagedestroy ($ src_im ); return $ imgname;} // The first step is to generate the public function test ($ url, $ path = '. /') {$ w = 110; $ h = 110; // original size $ original_path = $ url; $ dest_path = paipath.uniqid().'.png '; $ src = imagecreatefromstring (values ($ original_path); $ newpic = values ($ 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 merge Image public function mergerImg ($ imgs, $ path = '. /') {$ imgname = comment'; 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']); imagecopy ($ dests, $ src_im, 270,202, $ src_info [0], $ src_info [1]); imagedestroy ($ src_im); // var_dump ($ imgs); exit; // header ("Content-type: image/jpeg"); imagejpeg ($ dests, $ imgname); // unlink ($ imgs ['dst ']); unlink ($ imgs ['src']); return $ imgname ;}}
Result Display: