PHP 压缩图片 合并图片和制作圆头像利用gd库

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/php_lzr/article/details/86073730
//按照宽高缩放图片
public static function zoom($filename,$nw,$nh){
    if(!file_exists(EXT.'qrcode/zoom/')){
        mkdir(EXT.'qrcode/zoom/',0777,true);
    }
    $ing_path = $filename;
    $ext = getimagesize($filename);
    $src_img = null;
    list($width, $height) = getimagesize($filename);
    $new=imagecreatetruecolor($nw, $nh);
    switch ($ext['mime']) {
        case 'image/jpeg':
            $img = imagecreatefromjpeg($filename);
            $path = EXT.'qrcode/zoom/'.rand(0,100000).'.jpg';
            imagecopyresized($new, $img,0, 0,0, 0,$nw, $nh, $width, $height);
            imagejpeg($new,$path,100);
            break;
        case 'image/png':
            $img = imagecreatefrompng($filename);
            $path = EXT.'qrcode/zoom/'.rand(0,100000).'.png';
            imagecopyresized($new, $img,0, 0,0, 0,$nw, $nh, $width, $height);
            imagepng($new,$path,2);
            break;
    }
    if(file_exists($filename)) unlink($ing_path);
    return $path;
}
//整成袁大头
public static function yuan_img($imgpath) {
    if(!file_exists(EXT.'qrcode/zoom/')){
        mkdir(EXT.'qrcode/zoom/',0777,true);
    }
    $src_img = null;
    $src_img = imagecreatefromjpeg($imgpath);
    $wh  = getimagesize($imgpath);
    $w   = $wh[0];
    $h   = $wh[1];
    $w   = min($w, $h);
    $h   = $w;
    $img = imagecreatetruecolor($w, $h);
    //这一句一定要有
    imagesavealpha($img, true);
    //拾取一个完全透明的颜色,最后一个参数127为全透明
    $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
    imagefill($img, 0, 0, $bg);
    $r   = $w / 2; //圆半径
    $y_x = $r; //圆心X坐标
    $y_y = $r; //圆心Y坐标
    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);
            }
        }
    }

    $path = EXT.'qrcode/zoom/'.rand(0,100000).'.jpg';
    imagejpeg($img,$path,100);
    imagedestroy($img);
    if(file_exists($imgpath)) unlink($imgpath);
    return $path;
}
//合并图片
public static function exclusive_photo($back,$code){
    if(!file_exists(EXT.'qrcode/qrcodepic/')){
        mkdir(EXT.'qrcode/qrcodepic/',0777,true);
    }

    $code_path = $code;
    //获取水印图片的宽度和高度
    list($c_w,$c_h) = getimagesize($code);//二维码
    //创建背景图片的资源
    $back = imagecreatefrompng($back);//背景图
    //创建水印图片的资源
    $code = imagecreatefromjpeg($code);
    $c_x = 177;
    $c_y = 581;
    //合并二维码
    imagecopy($back, $code, $c_x, $c_y, 0, 0, $c_w, $c_h);

    //保存带有水印图片的背景图片
    $outfile = EXT.'qrcode/qrcodepic/'.time().rand(0,9999).'.jpg';
    imagejpeg($back,$outfile,100);
    //销毁资源
    imagedestroy($back);
    imagedestroy($code);
    if(file_exists($code_path)) unlink($code_path);
    return $outfile;
}

猜你喜欢

转载自blog.csdn.net/php_lzr/article/details/86073730