How to add a circular logo icon to the background image of PHP _php tips

Source: Internet
Author: User
Tags imagecopy imagejpeg

This article illustrates the method of adding a circular logo icon to the background map of PHP. Share to everyone for your reference, specific as follows:

Say the steps:

A total of 3 steps:

1. Compressed logo into a fixed size square picture
2. Turn the logo into a circular logo
3. Combine logo with background image

Nonsense not much to say, directly on the code:

<?php/** * Author: Friker * Development time: 20160516 * Function: Image processing * * */class Imagecontroller extends ci_controller{public functi
    On __construct () {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 a square image and then cut it into a circle to make a logo * with the background image merge * @return return URL/Public Function ind
    Ex () {//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 is to cut into fillet picture $imgs [' src '] = $this->test ($imggzip);
  Third step merge 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);
    Scale 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;
    ///First step to generate fillet picture public function test ($url, $path = './') {$w = =110 $h Original size $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 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:

For more information on PHP related content readers can view the site topics: "PHP graphics and pictures Operating skills summary", "Basic PHP Grammar Introductory Course", "PHP Operations and Operator Usage Summary", "PHP object-oriented Program Design Introductory Course", "PHP Network Programming Skills Summary", "PHP array (Array) Operations tips Encyclopedia, "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP Common database Operation tips Summary"

I hope this article will help you with the PHP program design.

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.