Add a circle logo icon to the PHP implementation background map

Source: Internet
Author: User
Tags imagecopy imagejpeg
This article mainly introduces the PHP implementation of the background map to add a circular logo icon method, combined with the case of a more detailed analysis of the PHP background map to add logo icon operation and specific implementation skills, the need for friends can refer to the next

Say the steps:

A total of 3 steps:

1. Compress the logo into a fixed-size square image
2. Turn the logo into a circular logo
3. Merge the logo with the background map

Nonsense not much to say, directly on the code:

<?php/** * Author: Friker * Development time: 20160516 * Function: Image processing * */class Imagecontroller extends ci_controller{public function __cons    Truct () {parent::__construct ();    Date_default_timezone_set (' Asia/shanghai ');    Error_reporting (e_all&~e_notice&~e_warning);  $this->load->library (' curl ');    }/** * @todo: This function is used to compress the square picture * and then cut into a circle to make the logo * and the background image merge * @return return URL */Public Function index () {    Avatar $headimgurl = ' a.jpg ';    Background image $bgurl = './aa.png ';    $imgs [' dst '] = $bgurl;    The first step compresses the picture $imggzip = $this->resize_img ($headimgurl);    The second step cuts into rounded pictures $imgs [' src '] = $this->test ($imggzip);  The third step merges the picture $dest = $this->mergerimg ($IMGS); The Public Function resize_img ($url, $path = './') {$imgname = $path. Uniqid ().    JPG ';    $file = $url; List ($width, $height) = getimagesize ($file);    Get the original size $percent = (110/$width);    Scaled size $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);    Output compressed picture Imagedestroy ($dst _im);    Imagedestroy ($src _im);  return $imgname; $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 Merge Picture public function mergerimg ($imgs, $path = './') {$imgname = $path. Rand (1000,9999). Uniqid ().    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 ']);    Imagecopy ($dests, $src _im,270,202,0,0, $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; }}

Results show:

The above is the whole content of this article, I hope that everyone's study has helped.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.