php教程
php手册
PHP implements techniques for adding text and image watermarks when uploading images,
PHP implements techniques for adding text and image watermarks when uploading images,
PHP implements techniques for adding text and image watermarks when uploading images.
The functions implemented in this article are particularly suitable for some shopping malls and photo sites. It shares the ability to add text and image watermarks when uploading images. Picture watermarking techniques are for your reference. The details are as follows
1. water.class.php
<?php
header('Content-Type:text/html;charset=utf-8');
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//给图片添加水印
Class Water{
//开启水印
private $watermark_on = '1';
public $water_img;
//水印位置
public $pos = 1;
//压缩比
public $pct = 80;
//透明度
public $quality = 80;
public $text = '乐趣网zlblog.sinaapp.com';
public $size = 12;
public $color = '#000000';
public $font = 'font.ttf';
public function watermark( $img,$pos='',$out_img='',$water_img='',$text='' ){
if(!$this->check($img) || !$this->watermark_on) return false;
$water_img = $water_img ? $water_img : $this->water_img;
//水印的开启状态
$waterimg_on = $this->check($water_img) ? 1 : 0;
//判断是否在原图上操作
$out_img = $out_img ? $out_img : $img;
//判断水印的位置
$pos = $pos ? $pos : $this->pos;
//水印文字
$text = $text ? $text : $this->text;
$img_info = getimagesize($img);
$img_w = $img_info[0];
$img_h = $img_info[1];
//判断水印图片的类型
if( $waterimg_on ){
$w_info = getimagesize($water_img);
$w_w = $w_info[0];
$w_h = $w_info[1];
if ( $img_w < $w_w || $img_h < $w_h ) return false;
switch ( $w_info[2] ){
case 1:
$w_img = imagecreatefromgif($water_img);
break;
case 2:
$w_img = imagecreatefromjpeg($water_img);
break;
case 3:
$w_img = imagecreatefrompng($water_img);
break;
}
}else{
if( empty($text) || strlen($this->color)!=7 ) return FALSE;
$text_info = imagettfbbox($this->size, 0, $this->font, $text);
$w_w = $text_info[2] - $text_info[6];
$w_h = $text_info[3] - $text_info[7];
}
//建立原图资源
switch ( $img_info[2] ){
case 1:
$res_img = imagecreatefromgif($img);
break;
case 2:
$res_img = imagecreatefromjpeg($img);
break;
case 3:
$res_img = imagecreatefrompng($img);
break;
}
//确定水印的位置
switch ( $pos ){
case 1:
$x = $y =25;
break;
case 2:
$x = ($img_w - $w_w)/2;
$y = 25;
break;
case 3:
$x = $img_w - $w_w;
$y = 25;
break;
case 4:
$x = 25;
$y = ($img_h - $w_h)/2;
break;
case 5:
$x = ($img_w - $w_w)/2;
$y = ($img_h - $w_h)/2;
break;
case 6:
$x = $img_w - $w_w;
$y = ($img_h - $w_h)/2;
break;
case 7:
$x = 25;
$y = $img_h - $w_h;
break;
case 8:
$x = ($img_w - $w_w)/2;
$y = $img_h - $w_h;
break;
case 9:
$x = $img_w - $w_w;
$y = $img_h - $w_h;
break;
default :
$x = mt_rand(25, $img_w - $w_w);
$y = mt_rand(25, $img_h - $w_h);
}
//写入图片资源
if( $waterimg_on ){
imagecopymerge($res_img, $w_img, $x, $y, 0, 0, $w_w, $w_h, $this->pct);
}else{
$r = hexdec(substr($this->color, 1,2));
$g = hexdec(substr($this->color, 3,2));
$b = hexdec(substr($this->color, 5,2));
$color = imagecolorallocate($res_img, $r, $g, $b);
imagettftext($res_img, $this->size, 0, $x, $y, $color, $this->font, $text);
}
//生成图片类型
switch ( $img_info[2] ){
case 1:
imagecreatefromgif($res_img,$out_img);
break;
case 2:
//imagecreatefromjpeg($res_img,$out_img);
imagejpeg($res_img,$out_img);
break;
case 3:
imagejpeg($res_img,$out_img);
break;
}
if(isset($res_img)) imagedestroy ($res_img);
if(isset($w_img)) imagedestroy($w_img);
return TRUE;
}
//验证图片是否存在
private function check($img){
$type = array('.jpg','.jpeg','.png','.gif');
$img_type = strtolower(strrchr($img, '.'));
return extension_loaded('gd') && file_exists($img) && in_array($img_type, $type);
}
}2. test1.php
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//header('Content-Type:text/html;charset=utf-8');
include 'water.class.php';
$image = new Water();
$image->watermark('12.jpg',5);
//$image->watermark('12.jpg',1);
3. Rendering
The above is the entire content of this article. I hope it will be helpful to everyone in learning PHP programming.
Hot AI Tools
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undress AI Tool
Undress images for free
Clothoff.io
AI clothes remover
AI Hentai Generator
Generate AI Hentai for free.
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
1377
52
PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian
Dec 24, 2024 pm 04:42 PM
PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati
Discuss CakePHP
Sep 10, 2024 pm 05:28 PM
CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu
CakePHP File upload
Sep 10, 2024 pm 05:27 PM
To work on file upload we are going to use the form helper. Here, is an example for file upload.
How To Set Up Visual Studio Code (VS Code) for PHP Development
Dec 20, 2024 am 11:31 AM
Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
How do you parse and process HTML/XML in PHP?
Feb 07, 2025 am 11:57 AM
This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
CakePHP Quick Guide
Sep 10, 2024 pm 05:27 PM
CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
PHP Program to Count Vowels in a String
Feb 07, 2025 pm 12:12 PM
A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total
Explain JSON Web Tokens (JWT) and their use case in PHP APIs.
Apr 05, 2025 am 12:04 AM
JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,


