PHP 5 文字图片混合水印与缩略图

一、水印制作

   1.水印文字

    PHP 中为图片打上水印文字主要是通过 GD 库提供的 imagettftext() 函数来实现的。

 

    其过程为:载入图片 =》 调好水印文字的颜色 =》 打上水印

 

<?php
$img = 'Desert.jpg';//图像的路径。这里以 Windows 7 自带的一幅沙漠的图片为例
$img_info = getimagesize($img);

//载入图像到PHP,转成 PHP 可识别的编码
switch($img_info[2]) {
  case 1:
    $res = imagecreatefromgif($img);//返回一图像标识符,代表了从给定的文件名取得的图像。 
    break;
  case 2:
    $res = imagecreatefromjpeg($img);
    break;
  case 3:
    $res = imagecreatefrompng($img);
    break;    
}

// 为一幅图像分配颜色(相当于 PhotoShop 的调色板)
// imagecolorallocate ( resource image, int red, int green, int blue )  返回一个标识符,代表了由给定的 RGB 成分组成的颜色。
$te = imagecolorallocate($res,225,225,225);

//rand(0,10)倾斜度。msyh.ttf 是微软雅黑字体,可在 C:\Windows\Fonts 找到。然后拷贝到该文件的目录下
imagettftext($res,12,rand(0,10),20,80,$te,'msyh.ttf',"我的博客 www.woqilin.net");

switch($img_info[2]) {
  case 1:
    header("Content-type: image/gif");
    imagegif($res);//以 GIF 格式将图像输出到浏览器
    break;
  case 2:
    header("Content-type: image/jpeg");
    imagejpeg($res);
    break;
  case 3:
    header("Content-type: image/png");
    imagepng($res);
    break;    
}
?>​

 

  2.水印图片

    PHP 中为图片打上图片水印是通过 imagecopy() 函数来实现的。

<?php
$img = 'Desert.jpg';//图像的路径。这里以 Windows 7 下一幅沙漠的图片为例,像素为 1024 X 768
$img_info = getimagesize($img);

//载入图像到PHP
switch($img_info[2]) {
  case 1:
    $res = imagecreatefromgif($img);//返回一图像标识符,代表了从给定的文件名取得的图像。 
    break;
  case 2:
    $res = imagecreatefromjpeg($img);
    break;
  case 3:
    $res = imagecreatefrompng($img);
    break;    
}

//新建一个真彩色图像
$new = imagecreatetruecolor(400,400);
//bool imagecopyresized ( resource dst_image, resource src_image, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h )
//将一幅图像中的一块正方形区域拷贝到另一个图像中。dst_image 和 src_image 分别是目标图像和源图像的标识符。如果源和目标的宽度和高度不同,则会进行相应的图像收缩和拉伸。坐标指的是左上角。本函数可用来在同一幅图内部拷贝(如果 dst_image 和 src_image 相同的话)区域,但如果区域交迭的话则结果不可预知。
imagecopyresized($new,$res,0,0,0,0,400,400,$img_info[0],$img_info[1]);

header("Content-type: image/png");
imagepng($new);// 以 PNG 格式将图像输出到浏览器
?>​

原文链接:http://woqilin.blogspot.com/2012/11/php-5.html

猜你喜欢

转载自wangzq-phper.iteye.com/blog/2294209