php 图片合成

php 图片合成(例:背景图加二维码图)

hecheng('back.png','123.png','newaaa.png');


// 合成图片
function hecheng($backpc,$qr,$newpc)
{
    $qrcode = thumb($qr,150,150,'new_');
     
    $bigImg = imagecreatefromstring(file_get_contents($backpc));
    $qrcodeimg = imagecreatefromstring(file_get_contents($qrcode));
     
    list($qCodeWidth, $qCodeHight, $qCodeType) = getimagesize($qrcode);
    // imagecopymerge使用注解
    imagecopymerge($bigImg, $qrcodeimg, 133, 550, 0, 0, $qCodeWidth, $qCodeHight, 100);
     
    list($bigWidth, $bigHight, $bigType) = getimagesize($backpc);
     
     
    switch ($bigType) {
        case 1: //gif
            header('Content-Type:image/gif');
            imagegif($bigImg);
            break;
        case 2: //jpg
            header('Content-Type:image/jpg');
            imagejpeg($bigImg);
            break;
        case 3: //png
            // header('Content-Type:image/png');
            imagepng($bigImg,$newpc);
            break;
        default:
            # code...
            break;
    }
     
    imagedestroy($bigImg);
    imagedestroy($qrcodeimg);
    return true;
}

// 缩放图片
function thumb($file,$dw,$dh,$pre)
{
    $brr=pathinfo($file);
    $dir=$brr['dirname']; //图片保存路径
    $basename=$brr['basename'];//图片名字
    $dstname=$pre.$basename;  //目标图片名字
    $path=$dir.'/'.$dstname;//目标图片保存轮径
    $arr=getimagesize($file);  //获得图片信息
    $sw=$arr[0];  //原图宽
    $sh=$arr[1];  //原图的高
    $type=$arr[2];  //图片格式  1 = GIF,2 = JPG,3 = PNG
    $mime=$arr['mime'];  //MIME 类型
 
    switch ($type) {
        case 1:
            $imgcreate='imagecreatefromgif';
            $imgout='imagegif';
            break;
        case 2:
            $imgcreate='imagecreatefromjpeg';
            $imgout='imagejpeg';
            break;
        case 3:
            $imgcreate='imagecreatefrompng';
            $imgout='imagepng';
            break;
    }
    $src=$imgcreate($file);
    $b=$sw/$dw>$sh/$dh?$sw/$dw:$sh/$dh;
    $dw=floor($sw/$b);
    $dh=floor($sh/$b);
    $dst=imagecreatetruecolor($dw,$dh);
    $bool=imagecopyresampled($dst, $src, 0, 0 , 0 , 0, $dw , $dh , $sw, $sh);
    $imgout($dst,$path);
    return  $dstname;
}
 

你可能感兴趣的:(php 图片合成)