Home > Backend Development > PHP Tutorial > PHP 5 text and picture mixed watermark and thumbnail

PHP 5 text and picture mixed watermark and thumbnail

巴扎黑
Release: 2023-03-03 12:00:01
Original
1125 people have browsed it

1. Watermark production

1. Watermark text

Watermarking text on images in PHP is mainly achieved through the imagettftext() function provided by the GD library.

The process is: Load the image => Adjust the color of the watermark text => Add watermark

<?php
$img = &#39;Desert.jpg&#39;;//图像的路径。这里以 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,&#39;msyh.ttf&#39;,"我的博客 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;    
}
?>
Copy after login

2. Watermark image

In PHP, watermarking images is achieved through the imagecopy() function.

<?php
$img = &#39;Desert.jpg&#39;;//图像的路径。这里以 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 格式将图像输出到浏览器
?>
Copy after login


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template