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 = 150;       //顶边距

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 文件,上面也可设置相应的保存目录及文件名。
        break;
    default:
        break;
}
imagedestroy($img);

 效果如下:

 

二、合成二维码

<?php
$bigImgPath = 'background.png';
$qCodePath = 'code.png';

$bigImg = imagecreatefromstring(file_get_contents($bigImgPath));
$qCodeImg = imagecreatefromstring(file_get_contents($qCodePath));
imagesavealpha($bigImg,true);//假如是透明PNG图片,这里很重要 意思是不要丢了图像的透明
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 文件,上面也可设置相应的保存目录及文件名。
        break;
    default:
        # code...
        break;
}
imagedestroy($bigImg);
imagedestroy($qcodeImg);

合成的效果如下:

 

如果感觉上面的二维码太大  那么可以先生成一张小的二维码图片,下面是处理的方法

<?php
$bigImgPath = 'background.png';
$qCodePath = 'code.png';
imagepress($qCodePath);
$qCodePath = 'code1.png';
$bigImg = imagecreatefromstring(file_get_contents($bigImgPath));
$qCodeImg = imagecreatefromstring(file_get_contents($qCodePath));
//$qCodeImg = myImageResize($qCodeImg, 200, 200); //最大宽高

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 文件,上面也可设置相应的保存目录及文件名。
        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);  //这里的名命可根据需要设置
}

效果如下:

 

 

imagecopymerge语法:
imagecopymerge( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y,
 int src_w, int src_h, int pct )

imagecopymerge() 函数用于拷贝并合并图像的一部分,成功返回 TRUE ,否则返回 FALSE

参数说明
dst_im 目标图像
src_im 被拷贝的源图像
dst_x 目标图像开始 x 坐标
dst_y 目标图像开始 y 坐标,x,y同为 0 则从左上角开始
src_x 拷贝图像开始 x 坐标
src_y 拷贝图像开始 y 坐标,x,y同为 0 则从左上角开始拷贝
src_w (从 src_x 开始)拷贝的宽度
src_h (从 src_y 开始)拷贝的高度
pct 图像合并程度,取值 0-100 ,当 pct=0 时,实际上什么也没做,反之完全合并。

 当为 pct = 100 时对于调色板图像本函数和 imagecopy() 完全一样,参考阅读:

imagecopy():拷贝图像或图像的一部分。

该函数的一个典型应用就是将图像打上水印标识:

<?
header("Content-type: image/jpeg");

//原始图像
$dst = "images/flower_1.jpg";

//得到原始图片信息
$dst_im = imagecreatefromjpeg($dst);
$dst_info = getimagesize($dst);

//水印图像
$src = "images/logo.gif";
$src_im = imagecreatefromgif($src);
$src_info = getimagesize($src);	

//水印透明度
$alpha = 30;

//合并水印图片
imagecopymerge($dst_im,$src_im,$dst_info[0]-$src_info[0],$dst_info[1]-$src_info[1],0,0,$src_info[0],
$src_info[1],$alpha);

//输出合并后水印图片
imagejpeg($dst_im);
imagedestroy($dst_im);
imagedestroy($src_im);
?>

  

posted @ 2017-07-21 14:45  智昕  阅读(4239)  评论(2编辑  收藏  举报