/*
       * @fun 图片转换成圆形png,传入源路径和转换后的路径,均用相对于当前程序文件的路径
       * @memo 对于非正方形的图片,以短边作为图片的直径
       * @param string $src 源路径
       * @param string $dst 转换后的路径
       */
      public function yuan_img($src) {
        //获取原图尺寸,并设置新图片的宽度和高度
        list($w, $h) = getimagesize($src);
        if( $w > $h ){
          $w = $h;
        }else{
          $h = $w;
        }
        $oimgSrc = imagecreatefromstring(file_get_contents($src));
        $oimgDst = imagecreatetruecolor($w, $h);
        imagealphablending($oimgDst,false);
        $transparent = imagecolorallocatealpha($oimgDst, 0, 0, 0, 127);
        $r=$w/2;
        for($x=0;$x<$w;$x++){
          for($y=0;$y<$h;$y++){
            $c = imagecolorat($oimgSrc,$x,$y);
            $_x = $x - $w/2;
            $_y = $y - $h/2;
            if((($_x*$_x) + ($_y*$_y)) < ($r*$r)){
              imagesetpixel($oimgDst,$x,$y,$c);
            }else{
              imagesetpixel($oimgDst,$x,$y,$transparent);
            }
          }
        }
        $str = uniqid(mt_rand(),1);
        $file='/upload/ai/code/user/wx_tx/'.date('Ymd',time()).'/';
        $file2=$_SERVER['DOCUMENT_ROOT'].$file;
        if(!is_dir($file2))//检测目录是否存在
        {
          mkdir($file2,0777,true);
        }
        imagesavealpha($oimgDst, true);
        imagepng($oimgDst, $file2.md5($str).'.png');
        imagedestroy($oimgDst);
        imagedestroy($oimgSrc);
         return $file.md5($str).'.png';
      }

快捷键:yuanimg

源:

sublime_text格式

<snippet>
    <content><![CDATA[
     /*
       * @fun 图片转换成圆形png,传入源路径和转换后的路径,均用相对于当前程序文件的路径
       * @memo 对于非正方形的图片,以短边作为图片的直径
       * @param string \$src 源路径
       * @param string \$dst 转换后的路径
       */
      public function yuan_img(\$src) {
        //获取原图尺寸,并设置新图片的宽度和高度
        list(\$w, \$h) = getimagesize(\$src);
        if( \$w > \$h ){
          \$w = \$h;
        }else{
          \$h = \$w;
        }
        \$oimgSrc = imagecreatefromstring(file_get_contents(\$src));
        \$oimgDst = imagecreatetruecolor(\$w, \$h);
        imagealphablending(\$oimgDst,false);
        \$transparent = imagecolorallocatealpha(\$oimgDst, 0, 0, 0, 127);
        \$r=\$w/2;
        for(\$x=0;\$x<\$w;\$x++){
          for(\$y=0;\$y<\$h;\$y++){
            \$c = imagecolorat(\$oimgSrc,\$x,\$y);
            \$_x = \$x - \$w/2;
            \$_y = \$y - \$h/2;
            if(((\$_x*\$_x) + (\$_y*\$_y)) < (\$r*\$r)){
              imagesetpixel(\$oimgDst,\$x,\$y,\$c);
            }else{
              imagesetpixel(\$oimgDst,\$x,\$y,\$transparent);
            }
          }
        }
        \$str = uniqid(mt_rand(),1);
        \$file='/upload/ai/code/user/wx_tx/'.date('Ymd',time()).'/';
        \$file2=\$_SERVER['DOCUMENT_ROOT'].\$file;
        if(!is_dir(\$file2))//检测目录是否存在
        {
          mkdir(\$file2,0777,true);
        }
        imagesavealpha(\$oimgDst, true);
        imagepng(\$oimgDst, \$file2.md5(\$str).'.png');
        imagedestroy(\$oimgDst);
        imagedestroy(\$oimgSrc);
         return \$file.md5(\$str).'.png';
      }
]]></content>
   <tabTrigger>yuanimg(图片转换成圆形png)</tabTrigger>
   <scope></scope>
</snippet>
点赞(1)

评论列表 共有 0 条评论

暂无评论
立即
投稿
发表
评论
返回
顶部