php 图片压缩 并进行 圆角 处理

图片进行压缩 然后进行圆角 处理 !  亲试可以哦!

function expand_image($srcImage, $maxwidth, $maxheight, $radius = 10)
    {
        list($width, $height, $type, $attr) = getimagesize($srcImage);
        switch ($type) {
            case 1:
                $img = imagecreatefromgif($srcImage);
                break;
            case 2:
                $img = imagecreatefromjpeg($srcImage);
                break;
            case 3:
                $img = imagecreatefrompng($srcImage);
                break;
            default:
                $img = imagecreatefrompng($srcImage);
                break;
        }
        $canvas = imagecreatetruecolor($maxwidth, $maxheight); // 创建一个真彩色图像 我把它理解为创建了一个画布
        $alpha = imagecolorallocatealpha($canvas, 0, 0, 0, 127);
        imagefill($canvas, 0, 0, $alpha);
        imagecopyresampled($canvas, $img, 0, 0, 0, 0, $maxwidth, $maxheight, $width, $height);
        $extend = explode(".", $srcImage);
        $attach_fileext = strtolower($extend[count($extend) - 1]);
        if (!in_array($attach_fileext, array('jpg', 'png', 'jpeg'))) {
            return '';
        }
        $sFileNameS = str_replace("." . $attach_fileext, "_" . $maxwidth . '_' . $maxheight . '.' . $attach_fileext, $srcImage);
        //是否存在
        if (file_exists($sFileNameS)) {
            return $sFileNameS;
        }
        imagejpeg($canvas, $sFileNameS, 100,$alpha);
        //圆角
        $canvas = $this->radius_img($sFileNameS, $radius);
        imagejpeg($canvas, $sFileNameS, 100,$alpha);
        return $sFileNameS;
    }


    /**
     * 把图片转换成圆角
     * @param string $imgpath
     * @param int $radius
     * @return resource
     */
   function radius_img($imgpath, $radius = 15, $width = '', $height = '')
    {
        $ext = pathinfo($imgpath);
        $src_img = null;
        switch ($ext['extension']) {
            case 'jpg':
                $src_img = imagecreatefromjpeg($imgpath);
                break;
            case 'png':
                $src_img = imagecreatefromjpeg($imgpath);
                break;
        }
        if (empty($width) || empty($height)) {
            $wh = getimagesize($imgpath);
            $width = $wh[0];
            $height = $wh[1];
        }
        $w = &$width;
        $h = &$height;
        $radius = $radius == 0 ? (min($w, $h) / 2) : $radius;
        $img = imagecreatetruecolor($w, $h);
        //这一句一定要有
        imagesavealpha($img, true);
        //拾取一个完全透明的颜色,最后一个参数127为全透明
        $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
        imagefill($img, 0, 0, $bg);
        $r = $radius; //圆 角半径
        for ($x = 0; $x < $w; $x++) {
            for ($y = 0; $y < $h; $y++) {
                $rgbColor = imagecolorat($src_img, $x, $y);
                if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
                    imagesetpixel($img, $x, $y, $rgbColor);
                }else{
                    imagesetpixel($img,$x,$y,$bg);
                }
            }
        }
        return $img;
    }


 $imag_path = './share/11.png';
 $this->expand_image($imag_path, 100, 100,10)
发布了52 篇原创文章 · 获赞 11 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_40816144/article/details/105112682
今日推荐