zoukankan      html  css  js  c++  java
  • 方角图片转换成圆角图片

    <?php
    $start_time = time();
    get_squre_circle_pic('logo.jpg');
    $end_time = time();
    echo date('i:s', $end_time - $start_time);

    function get_squre_circle_pic($url,$path='./') {
    $squre_url = resize_img($url, $path);
    $w = 100; $h=100; // 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);
    imagepng($newpic, $dest_path);
    imagedestroy($newpic);
    imagedestroy($src);
    // unlink($url);
    return $dest_path;
    }

    function resize_img($url,$path='./'){
    $square_url = $path.uniqid().'.jpg';
    $file = $url;
    list($width, $height) = getimagesize($file); //获取原图尺寸
    $percent = (100/$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, $square_url); //输出压缩后的图片
    imagedestroy($dst_im);
    imagedestroy($src_im);
    return $square_url;
    }
  • 相关阅读:
    Spring MVC笔记(二) Hello World实例
    Spring MVC笔记(一) Spring MVC概述
    正则表达式在python中的应用
    Spring与Ibatis整合入门
    Spark GraphX的函数源码分析及应用实例
    Python爬虫获取迅雷会员帐号
    Linux Shell编程学习笔记
    Hadoop在linux下无法启动DataNode解决方法
    Ubuntu中使用终端运行Hadoop程序
    Hadoop2.6.0在Ubuntu Kylin14.04上的配置
  • 原文地址:https://www.cnblogs.com/phonecom/p/7418773.html
Copyright © 2011-2022 走看看