php面向对象与面向过程两种方法给图片添加文字水印_php实例
目前绝大多数PHP程序员使用面向过程的方式,因为解析WEB页面本身就非常“过程化”(从一个标签到另一个标签)。在HTML中嵌入过程处理代码是很直接自然的作法,所以PHP程序员通常使用这种方式。
如果你是刚接触PHP,用面向过程的风格来书写代码很可能是你唯一的选择。但是如果你经常上PHP论坛和新闻组的话,你应该会看到有关“对象”的文章。你也可能看到过如何书写面向对象的PHP代码的教程。或者你也可能下载过一些现成的类库,并尝试着去实例化其中的对象和使用类方法--尽管你可能没有真正理解这些类为什么可以工作,或者为什么需要使用面向对象的方法来实现功能。
应该使用“面向对象”的风格还是“面向过程”的风格?双方各有支持者。像“对象是低效的”或“对象非常棒”这样的议论也时有耳闻。本文不尝试轻易判定两种方法的哪种具有绝对的优势,而是要找出每种方法的优缺点。
1:面向对象的实现利用php给图片添加水印方法
class Image_class { private $image; private $info; /** * @param $src:图片路径 * 加载图片到内存中 */ function __construct($src){ $info = getimagesize($src); $type = image_type_to_extension($info[2],false); $this -> info =$info; $this->info['type'] = $type; $fun = "imagecreatefrom" .$type; $this -> image = $fun($src); } /** * @param $fontsize: 字体大小 * @param $x: 字体在图片中的x位置 * @param $y: 字体在图片中的y位置 * @param $color: 字体的颜色是一个包含rgba的数组 * @param $text: 想要添加的内容 * 操作内存中的图片,给图片添加文字水印 */ public function fontMark($fontsize,$x,$y,$color,$text){ $col = imagecolorallocatealpha($this->image,$color[0],$color[1],$color[2],$color[3]); imagestring($this->image,$fontsize,$x,$y,$text,$col); } /* * 输出图片到浏览器中 */ public function show(){ header('content-type:' . $this -> info['mime']); $fun='image' . $this->info['type']; $fun($this->image); } /** * 销毁图片 */ function __destruct(){ imagedestroy($this->image); } } //对类的调用 $obj = new Image_class('001.png'); $obj->fontMark(20,20,30,array(255,255,255,60),'hello'); $obj->show();
2: 面向过程的编写利用php给图片添加水印方法:
//指定图片路径 $src = '001.png'; //获取图片信息 $info = getimagesize($src); //获取图片扩展名 $type = image_type_to_extension($info[2],false); //动态的把图片导入内存中 $fun = "imagecreatefrom{$type}"; $image = $fun('001.png'); //指定字体颜色 $col = imagecolorallocatealpha($image,255,255,255,50); //指定字体内容 $content = 'helloworld'; //给图片添加文字 imagestring($image,5,20,30,$content,$col); //指定输入类型 header('Content-type:'.$info['mime']); //动态的输出图片到浏览器中 $func = "image{$type}"; $func($image); //销毁图片 imagedestroy($image);
以上代码示例是介绍php面向对象与面向过程两种方法给图片添加文字水印,希望大家喜欢。

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



In-depth interpretation of PHP object-oriented encapsulation Encapsulation is one of the three major characteristics of object-oriented programming. It refers to encapsulating data and operations on data in a class, hiding specific implementation details from external programs, and providing external interfaces. In PHP, the concept of encapsulation is implemented by using access modifiers (public, protected, private) to control the accessibility of properties and methods. First, let’s take a look at the role of access modifiers: public (public): Public properties and methods can

How to implement object version control and management through the PHP object-oriented simple factory model. When developing large and complex PHP projects, version control and management are very important. Through appropriate design patterns, we can better manage and control the creation and use of objects, thereby improving the maintainability and scalability of the code. This article will introduce how to use the PHP object-oriented simple factory pattern to implement object version control and management. The simple factory pattern is a design pattern for creating classes that instantiates specified objects through a factory class

How to create flexible object instances using the PHP object-oriented simple factory pattern. The simple factory pattern is a common design pattern that creates object instances without exposing object creation logic. This mode can improve the flexibility and maintainability of the code, and is especially suitable for scenarios where different objects need to be dynamically created based on input conditions. In PHP, we can use the characteristics of object-oriented programming to implement the simple factory pattern. Let's look at an example below. Suppose we need to create a graphing calculator that can

With the development of the Internet, PHP has gradually become one of the most popular programming languages in web development. However, with the rapid development of PHP, object-oriented programming has become one of the necessary skills in PHP development. In this article, we will discuss how to master object-oriented programming skills in PHP development. Understanding the Concepts of Object-Oriented Programming Object-oriented programming is a programming paradigm that organizes code and data through the use of objects (classes, properties, and methods). In object-oriented programming, code is organized into reusable modules, thereby improving the program's

How to achieve seamless switching and replacement of objects through PHP's object-oriented simple factory pattern Introduction: In PHP development, object-oriented programming (Object-oriented Programming, referred to as OOP) is a very common programming paradigm. The object-oriented design pattern can further improve the maintainability and scalability of the code. This article will focus on the simple factory pattern in PHP to achieve seamless switching and replacement of objects. What is the simple factory pattern? Simple factory pattern (Simple

Understanding the Object-Oriented Inheritance Mechanism of PHP Inheritance is an important concept in object-oriented programming, which allows the creation of new classes that include the features and functionality of the old classes. In PHP, inheritance can be achieved through the keyword extends. Through inheritance, subclasses can inherit the properties and methods of the parent class, and can add new properties and methods, or override inherited methods. Let us understand the object-oriented inheritance mechanism of PHP through an example. classAnimal{public$name

PHP language has become a very popular web development language because it is easy to learn and use. Object-oriented programming is one of the most important programming paradigms in the PHP language. However, object-oriented programming is not something that is easy to master, so some common problems often arise. In this article, we will provide a detailed analysis of common problems with object-oriented programming in PHP. Question 1: How to create an object? In PHP, you can use the new keyword to create an object. For example: classMyClass{/

With the continuous development of Internet technology, PHP has become one of our common website development languages, and PHP object-oriented programming has also become a knowledge point that must be learned. Object-oriented programming (OOP) is a programming paradigm whose core concept is to combine data and behavior into an object to improve code reusability, readability, and maintainability. This article will explore how to use PHP to implement object-oriented programming and improve the readability and maintainability of your code. Basic Concepts of Object-Oriented Programming In object-oriented programming, each object has a set of properties.
