


Create image thumbnails, add watermarks, and add fonts with PHP. PHP website production tutorial. PHP website background production tutorial. PHPCMS template making.
The following is a class I provided, which encapsulates these three functions:
<?php /** * */ class ImageHelper { private $imgSrc; private $info; private $type; private $image; private $showOrSaveFunc; public function __construct($imgSrc) { $this->imgSrc = $imgSrc; $this->init(); } /** * 初始化操作 */ private function init() { //获取图片信息(通过GD库提供的方法,得到你想要处理的图片的基本信息) $this->info = getimagesize($this->imgSrc); //通过图像的编号来获取图像的类型 $this->type = image_type_to_extension($this->info[2], false); //在内存中创建一个和我们图像类型一样的图像 $fun = "imagecreatefrom{$this->type}"; //把图片复制到内存中 $this->image = $fun($this->imgSrc); $this->showOrSaveFunc = "image{$this->type}"; } /** * 给图片添加字体 * @param $fontfile 字体文件路径 * @param $text 文字内容 * @param $red 红色分量 * @param $green 绿色分量 * @param $blue 蓝色分量 * @param $alpha 透明度 * @param $angle 偏移角度 * @param $size 字体大小 * @param $x 距离左边的距离 * @param $y 距离右边的距离 */ public function addFont($fontfile, $text, $red, $green, $blue, $alpha , $angle, $size, $x, $y) { $color = imagecolorallocatealpha($this->image, $red, $green, $blue, $alpha); imagettftext($this->image, $size, $angle, $x, $y, $color, $fontfile, $text); } /** * 浏览器输出图片类型 */ private function setOutputHeader() { header("Content-Type:".$this->info['mime']); } /** * 输出图片到浏览器 */ public function outputImageToBrowser() { $this->setOutputHeader(); $this->excShowOrSaveFunc($this->showOrSaveFunc); } /** * 输出或者保存图片 */ private function excShowOrSaveFunc($type, $file='') { if ($file == '') { $type($this->image); } else { $type($this->image, $file); } } /** * 保存文件到本地 * @param 本地路径 * @param 文件名 */ public function outputImageToStorage($filepath, $filename) { $this->setOutputHeader(); $this->excShowOrSaveFunc($this->showOrSaveFunc, $filepath.$filename.'.'.$this->type); } /** * 为图片添加水印 * @param $waterMarkPath 水印图片路径 * @param $dst_x 水印距离原图左侧的距离 * @param $dst_y 水印距离原图上侧的距离 * @param $pct 水印的透明度 */ public function addWaterMark($waterMarkPath, $dst_x, $dst_y, $pct) { $waterInfo = getimagesize($waterMarkPath); $waterType = image_type_to_extension($waterInfo[2], false); $waterFun = "imagecreatefrom{$waterType}"; $waterMarkImage = $waterFun($waterMarkPath); //imagecopymerge(dst_im, src_im, dst_x, dst_y, src_x, src_y, src_w, src_h, pct) //pct透明度 imagecopymerge($this->image, $waterMarkImage, $dst_x, $dst_y, 0, 0, $waterInfo[0], $waterInfo[1], $pct); imagedestroy($waterMarkImage); } /** * 生成图片缩略图 * @param $mWidth 想要压缩的宽度 */ public function createThumbnail($mWidth) { $width = $this->info[0]; $height = $this->info[1]; if ($mWidth >= $width) { return ; } $mHeight = $height * $mWidth / $width; //1.在内存中建立一个宽300、高200的真色彩图片 $imageThumb = imagecreatetruecolor($mWidth, $mHeight); //2.核心步骤、将原图复制到新建的正色彩图片上,并按照一定比例压缩 //imagecopyresampled(dst_image, src_image, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h) imagecopyresampled($imageThumb, $this->image, 0, 0, 0, 0, $mWidth, $mHeight, $width, $height); $this->image = $imageThumb; } /* * 销毁图片资源 */ public function destroy() { imagedestroy($this->image); } } ?>
The usage method is as follows:
<?php require_once 'ImageHelper.class.php'; $imageHelper = new ImageHelper('P60219-211631.jpg'); /*$imageHelper->addFont("consola.ttf", "raid", 0, 0, 0, 50 , 0, 45, 20, 20); $imageHelper->addWaterMark('pic2.jpg', 0, 0, 50);*/ $imageHelper->createThumbnail(300); // $imageHelper->outputImageToBrowser(); $imageHelper->outputImageToStorage('','testImageHelperAddFont'); $imageHelper->destroy(); ?>
The above introduces how to create image thumbnails, add watermarks, and add fonts with PHP, including image creation and PHP content. I hope it will be helpful to friends who are interested in PHP tutorials.

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

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

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

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,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

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

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

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.
