search
HomeBackend DevelopmentPHP Tutorial利用php给图片添加文字水印-面向对象与面临过程俩种方法的实现

利用php给图片添加文字水印--面向对象与面向过程俩种方法的实现

1: 面向过程的编写方法

<span style="color: #008000;">//</span><span style="color: #008000;">指定图片路径</span><span style="color: #800080;">$src</span> = '001.png'<span style="color: #000000;">;</span><span style="color: #008000;">//</span><span style="color: #008000;">获取图片信息</span><span style="color: #800080;">$info</span> = <span style="color: #008080;">getimagesize</span>(<span style="color: #800080;">$src</span><span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;">获取图片扩展名</span><span style="color: #800080;">$type</span> = image_type_to_extension(<span style="color: #800080;">$info</span>[2],<span style="color: #0000ff;">false</span><span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;">动态的把图片导入内存中</span><span style="color: #800080;">$fun</span> =  "imagecreatefrom{<span style="color: #800080;">$type</span>}"<span style="color: #000000;">;</span><span style="color: #800080;">$image</span> = <span style="color: #800080;">$fun</span>('001.png'<span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;">指定字体颜色</span><span style="color: #800080;">$col</span> = imagecolorallocatealpha(<span style="color: #800080;">$image</span>,255,255,255,50<span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;">指定字体内容</span><span style="color: #800080;">$content</span> = 'helloworld'<span style="color: #000000;">;</span><span style="color: #008000;">//</span><span style="color: #008000;">给图片添加文字</span>imagestring(<span style="color: #800080;">$image</span>,5,20,30,<span style="color: #800080;">$content</span>,<span style="color: #800080;">$col</span><span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;">指定输入类型</span><span style="color: #008080;">header</span>('Content-type:'.<span style="color: #800080;">$info</span>['mime'<span style="color: #000000;">]);</span><span style="color: #008000;">//</span><span style="color: #008000;">动态的输出图片到浏览器中</span><span style="color: #800080;">$func</span> = "image{<span style="color: #800080;">$type</span>}"<span style="color: #000000;">;</span><span style="color: #800080;">$func</span>(<span style="color: #800080;">$image</span><span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;">销毁图片</span>imagedestroy(<span style="color: #800080;">$image</span>);

2:面向对象的实现方法

<span style="color: #0000ff;">class</span><span style="color: #000000;"> Image_class {    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$image</span><span style="color: #000000;">;    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$info</span><span style="color: #000000;">;    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*     * @param $src:图片路径     * 加载图片到内存中     </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$src</span><span style="color: #000000;">){        </span><span style="color: #800080;">$info</span> = <span style="color: #008080;">getimagesize</span>(<span style="color: #800080;">$src</span><span style="color: #000000;">);        </span><span style="color: #800080;">$type</span> = image_type_to_extension(<span style="color: #800080;">$info</span>[2],<span style="color: #0000ff;">false</span><span style="color: #000000;">);        </span><span style="color: #800080;">$this</span> -> info =<span style="color: #800080;">$info</span><span style="color: #000000;">;        </span><span style="color: #800080;">$this</span>->info['type'] = <span style="color: #800080;">$type</span><span style="color: #000000;">;        </span><span style="color: #800080;">$fun</span> = "imagecreatefrom" .<span style="color: #800080;">$type</span><span style="color: #000000;">;        </span><span style="color: #800080;">$this</span> -> image = <span style="color: #800080;">$fun</span>(<span style="color: #800080;">$src</span><span style="color: #000000;">);    }    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*     * @param $fontsize: 字体大小     * @param $x: 字体在图片中的x位置     * @param $y: 字体在图片中的y位置     * @param $color: 字体的颜色是一个包含rgba的数组     * @param $text: 想要添加的内容     * 操作内存中的图片,给图片添加文字水印     </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> fontMark(<span style="color: #800080;">$fontsize</span>,<span style="color: #800080;">$x</span>,<span style="color: #800080;">$y</span>,<span style="color: #800080;">$color</span>,<span style="color: #800080;">$text</span><span style="color: #000000;">){        </span><span style="color: #800080;">$col</span> = imagecolorallocatealpha(<span style="color: #800080;">$this</span>->image,<span style="color: #800080;">$color</span>[0],<span style="color: #800080;">$color</span>[1],<span style="color: #800080;">$color</span>[2],<span style="color: #800080;">$color</span>[3<span style="color: #000000;">]);        imagestring(</span><span style="color: #800080;">$this</span>->image,<span style="color: #800080;">$fontsize</span>,<span style="color: #800080;">$x</span>,<span style="color: #800080;">$y</span>,<span style="color: #800080;">$text</span>,<span style="color: #800080;">$col</span><span style="color: #000000;">);    }    </span><span style="color: #008000;">/*</span><span style="color: #008000;">     * 输出图片到浏览器中     </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> show(){       </span><span style="color: #008080;">header</span>('content-type:' . <span style="color: #800080;">$this</span> -> info['mime'<span style="color: #000000;">]);        </span><span style="color: #800080;">$fun</span>='image' . <span style="color: #800080;">$this</span>->info['type'<span style="color: #000000;">];        </span><span style="color: #800080;">$fun</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">image);    }    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*     * 销毁图片     </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">function</span><span style="color: #000000;"> __destruct(){        imagedestroy(</span><span style="color: #800080;">$this</span>-><span style="color: #000000;">image);    }}</span><span style="color: #008000;">//</span><span style="color: #008000;">对类的调用</span><span style="color: #800080;">$obj</span> = <span style="color: #0000ff;">new</span> Image_class('001.png'<span style="color: #000000;">);</span><span style="color: #800080;">$obj</span>->fontMark(20,20,30,<span style="color: #0000ff;">array</span>(255,255,255,60),'hello'<span style="color: #000000;">);</span><span style="color: #800080;">$obj</span>->show();

 

Statement
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
Go中Type关键字有哪些用法Go中Type关键字有哪些用法Sep 06, 2023 am 09:58 AM

Go中Type关键字的用法有定义新的类型别名或者创建新的结构体类型。详细介绍:1、类型别名,使用“type”关键字可以为已有的类型创建别名,这种别名不会创建新的类型,只是为已有的类型提供一个新的名称,类型别名可以提高代码的可读性,使代码更加清晰;2、结构体类型,使用“type”关键字可以创建新的结构体类型,结构体是一种复合类型,可以用于定义包含多个字段的自定义类型等等。

解决Ubuntu挂载移动硬盘错误:未知的文件系统类型exfat解决Ubuntu挂载移动硬盘错误:未知的文件系统类型exfatJan 05, 2024 pm 01:18 PM

ubuntu挂载移动硬盘出现错误:mount:unknownfilesystemtype'exfat'处理方法如下:Ubuntu13.10或安装exfat-fuse:sudoapt-getinstallexfat-fuseUbuntu13.04或以下sudoapt-add-repositoryppa:relan/exfatsudoapt-getupdatesudoapt-getinstallfuse-exfatCentOSLinux挂载exfat格式u盘错误的解决方法CentOS中加载extfa

如何免费使用Bing Image Creator如何免费使用Bing Image CreatorFeb 27, 2024 am 11:04 AM

本文将介绍七种利用免费的BingImageCreator获得高质量输出的方法。BingImageCreator(现称为MicrosoftDesigner的ImageCreator)是一个出色的在线人工智能艺术生成器之一。它能根据用户的提示生成高度逼真的视觉效果。提示越具体、清晰和创意,生成的效果也会更出色。BingImageCreator在创建高质量图像方面取得了重大进展。它现在使用Dall-E3培训模式,显示出更高水平的细节和现实主义。然而,它能否始终如一地生成高清结果取决于几个因素,包括快速

小米手机image怎么删除小米手机image怎么删除Mar 02, 2024 pm 05:34 PM

小米手机image怎么删除?在小米手机中是可以删除image,但是多数的用户不知道image如何的删除,接下来就是小编为用户带来的小米手机image删除方法教程,感兴趣的用户快来一起看看吧!小米手机image怎么删除1、首先打开小米手机中的【相册】功能;2、然后勾选不需要的图片,点击右下角的【删除】按钮;3、之后点击最顶部的【相册】进入到专区,选择【回收站】;4、接着直接点击下图所示的【清空回收站】;5、最后直接点击【永久删除】即可完成。

Imagemagic安装Centos及Image安装教程Imagemagic安装Centos及Image安装教程Feb 12, 2024 pm 05:27 PM

LINUX是一种开源的操作系统,它的灵活性和可定制性使得它成为了许多开发者和系统管理员的首选,在LINUX系统中,图像处理是一个非常重要的任务,而Imagemagick和Image是两个非常流行的图像处理工具,本文将为您介绍如何在Centos系统中安装Imagemagick和Image,并提供详细的安装教程。Imagemagic安装Centos教程Imagemagick是一个功能强大的图像处理工具集,它可以在命令行下执行各种图像操作,以下是在Centos系统上安装Imagemagick的步骤:1

Linux类型命令Linux类型命令Mar 20, 2024 pm 05:06 PM

在本指南中,我们将学习更多关于Linux中的”type”命令。前提条件:要执行本指南中演示的步骤,您需要以下组件:正确配置的Linux系统。查看如何创建LinuxVM用于测试和学习目的。对命令行界面的基本理解Linux中的Type命令与其他Linux专用命令(例如:ls、chmod、shut、vi、grep、pwd等)不同,”type”命令是一个内置的Bash函数,它显示作为参数提供的命令类型的信息。$type除了Bash,其他炮弹(Zsh、Ksh等)还附带

一篇搞懂this指向,赶超70%的前端人一篇搞懂this指向,赶超70%的前端人Sep 06, 2022 pm 05:03 PM

同事因为this指向的问题卡住的bug,vue2的this指向问题,使用了箭头函数,导致拿不到对应的props。当我给他介绍的时候他竟然不知道,随后也刻意的看了一下前端交流群,至今最起码还有70%以上的前端程序员搞不明白,今天给大家分享一下this指向,如果啥都没学会,请给我一个大嘴巴子。

注册Deepseek账号时,哪些信息是必填的?注册Deepseek账号时,哪些信息是必填的?Mar 12, 2025 pm 02:33 PM

必填信息:1、邮箱注册;2、手机号码注册;3、第三方社交平台注册。注册成功后通常还需要填写一些基本个人信息,如昵称、性别、生日等。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),