search
HomeBackend DevelopmentPHP TutorialPHP中创建并处理图象_PHP

PHP中创建并处理图象_PHP

Jun 01, 2016 pm 12:33 PM
functioncreateimagedeal withInstall

原文地址:http://www.zdnet.com.cn/developer/code/story/0,2000081534,39246703,00.htm

 

在安装了一些第三方函数库之后,结合图形处理技能,你就可以迅速使用PHP创建和处理图像了。事实上,你也不需要很多几何学知识——因为我在中学的时候这门功课曾经不及格而现在却能使用PHP创建图像!

在使用基本的图像创建函数之前,需要安装GD库。要使用JPEG相关的图像创建函数还需要安装jpeg-6b。在图像中使用Type 1字体的时候还必须安装t1lib。asdf

在这里,你还需要对你的系统设置进行进一步地调整。首先安装t1lib并结束,然后是jpeg-6b。第三步安装GD函数库。确保以上三部分按顺序安装,原因是你需要编译GD库才能使用jpeg-6b库。如果首先安装jpeg-6b,编译就会出错,这会让你不知所措够上一段时间。

在三函数库之后,重新配置PHP。这是在轻松安装PHP的DSO版本时的典型方法。然后执行make clean,命令,并在当前配置提示中加入以下代码:

 

--with-gd=[/path/to/gd]

--with-jpeg-dir=[/path/to/jpeg-6b]

--with-t1lib=[/path/to/t1lib]

最后执行make、make install完成配置。重新启动 Apache,并运行phpinfo()函数以检查新功能是否正常运行,然后就可以开始了。

取决于安装的GD库版本,你可能具有创建GIF或者PNG图像的能力。关键是如果你已经安装了gd-1.6或者早期版本,则可处理GIF文件,但不能处理PNG文件;如果安装了gd-1.6或者以后版本,你可以处理PNG文件却又不能处理GIF文件。

创建一个简单的图像需要几个函数。我将按步骤演示如下。

输出包含你所创建图像MIME类型的文件头,本例中为PNG。

header ("Content-type: image/png");

使用ImageCreate()创建一变量存放空白图像。该函数需要一个图片像素尺寸。格式为ImageCreate(x_size, y_size),对250*250像素的图片,如下:

$newImg = ImageCreate(250,250);

由于此时你的图像还是空白,所以需要用某些色彩填满它。但首先需要用ImageColorAllocate()函数按照颜色的RGB值为每种颜色确定名字。函数的格式为ImageColorAllocate([image], [red], [green], [blue])。如是天蓝色,应使用:

$skyblue = ImageColorAllocate($newImg,136,193,255);

 

接着,用ImageFill()函数为图像填充以上颜色。实际上ImageFill()函数有多个版本,如ImageFillRectangle(), ImageFillPolygon()等等。为简单起见,采用ImageFill()函数进行颜色填充,格式如下:

ImageFill([image], [start x point], [start y point], [color])

ImageFill($newImg,0,0,$skyblue);

最后,创建最终图像并破坏图像流以释放内存并在结束后整理系统:

 

ImagePNG($newImg);

ImageDestroy($newImg);

你的代码应该看起来像这样:

 

header ("Content-type: image/png");

$newImg = ImageCreate(250,250);

$skyblue = ImageColorAllocate($newImg,136,193,255);

ImageFill($newImg,0,0,$skyblue);

ImagePNG($newImg);

ImageDestroy($newImg);

?>

如果调用脚本skyblue.php 并用自己的浏览器访问它,就会看到一个250*250像素的蓝色PNG图像。

还可以用图像创建函数处理图像,如对大图像创建的缩略图等。

假设你打算为某图片制作一个35*35像素大小的缩略图。所要做的即创建一个新的35*35像素大小图像;并产生一个包含原始图像内容的图像流;然后改变原始图像的大小,并将其放到新的空白图像中去。

达到此目的的关键函数是ImageCopyResized(),,格式如下所示:ImageCopyResized([new image handle],[original image handle],[new image X],[new Image Y],[original image X],[original image Y], [new image X], [new image Y],[original image X],[original image Y]);

注释在代码下面:

 

/* 发送文件头以使浏览器知道文件类型*/

header("Content-type: image/png");

/* 设置变量保存新图片长宽*/

$newWidth = 35;

$newHeight = 35;

/* 以给定长和宽创建空白新图片 */

$newImg = ImageCreate($newWidth,$newHeight);

/* 从原始大图获取数据 */

$origImg = ImageCreateFromPNG("test.png");

/* 复制更改尺寸后的图片。使用函数 ImageSX() 及 ImageSY 获取原始图片的x及x尺寸 */

ImageCopyResized($newImg,$origImg,0,0,0,0,$newWidth,$newHeight,ImageSX($origImg),ImageSY($origImg));

/* 创建最终图片并清空内存 */

ImagePNG($newImg);

ImageDestroy($newImg); ?>

如果调用脚本resized.php并使用浏览器访问此文件,可以看到一个35*35像素的缩略PNG图片。

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
PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 14, 2025 am 12:13 AM

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.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP's Current Status: A Look at Web Development TrendsPHP's Current Status: A Look at Web Development TrendsApr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A ComparisonPHP vs. Other Languages: A ComparisonApr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and FunctionalityPHP vs. Python: Core Features and FunctionalityApr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: A Key Language for Web DevelopmentPHP: A Key Language for Web DevelopmentApr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

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

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

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.