PHP图片加水印文字及图片合成缩放

<?php
//图片添加文字水印
/*$bigImgPath = 'background.png';
$img = imagecreatefromstring(file_get_contents($bigImgPath));
 
$font = 'MSYH.TTF';//字体,字体文件需保存到相应文件夹下
$black = imagecolorallocate($img, 0, 0, 0);//字体颜色 RGB
 
$fontSize = 12;   //字体大小
$circleSize = 60; //旋转角度
$left = 50;      //左边距
$top = 120;       //顶边距
 
imagefttext($img, $fontSize, $circleSize, $left, $top, $black, $font, 'zhix.net| 智昕网络');
 
list($bgWidth, $bgHight, $bgType) = getimagesize($bigImgPath);
switch ($bgType) {
    case 1: //gif
        header('Content-Type:image/gif');
        imagegif($img);
        break;
    case 2: //jpg
        header('Content-Type:image/jpg');
        imagejpeg($img);
        break;
    case 3: //png
        header('Content-Type:image/png');
        //imagepng($img,"images/circle.png");  //在 images 目录下就会生成一个 circle.png 文件,上面也可设置相应的保存目录及文件名。
        imagepng($img); 
        break;
    default:
        break;
}
imagedestroy($img);
*/

//图片合成
/*$bigImgPath = 'background.png';
$qCodePath = 'code.png';
 
$bigImg = imagecreatefromstring(file_get_contents($bigImgPath));
$qCodeImg = imagecreatefromstring(file_get_contents($qCodePath));
imagesavealpha($bigImg,true);//假如是透明PNG图片,这里很重要 意思是不要丢了图像的透明<code class="php spaces"></code>
list($qCodeWidth, $qCodeHight, $qCodeType) = getimagesize($qCodePath);
// imagecopymerge使用注解
imagecopymerge($bigImg, $qCodeImg, 50, 50, 0, 0, $qCodeWidth, $qCodeHight, 100);
list($bigWidth, $bigHight, $bigType) = getimagesize($bigImgPath);
 
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: //jpg
        header('Content-Type:image/png');
        //imagepng($bigImg,"images/circle.png");  //在 images 目录下就会生成一个 circle.png 文件,上面也可设置相应的保存目录及文件名。
        imagepng($bigImg);
        break;
    default:
        # code...
        break;
}
imagedestroy($bigImg);
imagedestroy($qcodeImg);
*/

//如果感觉上面的二维码太大  那么可以先生成一张小的二维码图片,下面是处理的方法
//等比例生成图片
//$filepath图片路径,$percent缩放百分比
function imagepress($filepath,$percent='0.5'){
    // 图片类型,这里的二维码是PNG的   所以使用PNG类型
    header('Content-Type: image/png');
    // 获得新的图片大小
    list($width, $height) = getimagesize($filepath);  //取图片信息
    $new_width = $width * $percent;
    $new_height = $height * $percent;
    // 重新取样
    $image_p = imagecreatetruecolor($new_width, $new_height);
    $image = imagecreatefrompng($filepath);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    // 输出
    return imagejpeg($image_p, 'code1.png', 100);  //这里的名命可根据需要设置
}

$bigImgPath = 'background.png';
$qCodePath = 'code.png';
imagepress($qCodePath);
$qCodePath = 'code1.png';
$bigImg = imagecreatefromstring(file_get_contents($bigImgPath));
$qCodeImg = imagecreatefromstring(file_get_contents($qCodePath));
 
list($qCodeWidth, $qCodeHight, $qCodeType) = getimagesize($qCodePath);
// imagecopymerge使用注解
imagecopymerge($bigImg, $qCodeImg, 90, 90, 0, 0, $qCodeWidth, $qCodeHight, 100);
list($bigWidth, $bigHight, $bigType) = getimagesize($bigImgPath);
 
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: //jpg
        header('Content-Type:image/png');
        //imagepng($bigImg,"images/circle.png");  //在 images 目录下就会生成一个 circle.png 文件,上面也可设置相应的保存目录及文件名。
        imagepng($bigImg);
        break;
    default:
        # code...
        break;
}
imagedestroy($bigImg);
imagedestroy($qcodeImg);

猜你喜欢

转载自www.cnblogs.com/hnhycnlc888/p/10155107.html