1. 程式人生 > >PHP 壓縮圖片 合併圖片和製作圓頭像利用gd庫

PHP 壓縮圖片 合併圖片和製作圓頭像利用gd庫

//按照寬高縮放圖片
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;
}