Home Backend Development PHP Tutorial PHP脚本的10个技巧(4)

PHP脚本的10个技巧(4)

Jun 01, 2016 pm 02:30 PM

动态创建图象
在安装了某些第三方函数库之后,结合你的图形处理技能,你就可以用php创建和处理图像了。事实上,你也不需要太高的几何学知识。我在中学的时候这门功课总是不及格,现在不也照样会用PHP创建图像!

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

在这里,你还需要对你的系统进行进一步地调整。首先,你必须安装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-X-250像素的图像而言,用法如下:

$newImg = ImageCreate(250,250);

因为你的图像现在还是空白,所以你还要设法用某些色彩填满它,但是,首先你需要按照颜色的RGB值为每种颜色分配名字,这要用到ImageColorAllocate()函数。函数的格式是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-X-250像素大的蓝色PNG图像。

你还可以用图像创建函数处理图像,比如创建大型图像的缩微图等。

假设你打算为某个图片制作一个35-X-35像素大小的缩微图。你要做到就是创建一个新的35 X 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]);

以下是代码注释。

/* send a header so that the browser knows the content-type of the file */
header("Content-type: image/png");

/* set up variables to hold the height and width of your new image */
$newWidth = 35;
$newHeight = 35;

/* create a blank, new image of the given new height and width */
$newImg = ImageCreate($newWidth,$newHeight);

/* get the data from the original, large image */
$origImg = ImageCreateFromPNG("test.png");

/* copy the resized image. Use the ImageSX() and ImageSY functions to get the x and y sizes of the orginal image. */
ImageCopyResized($newImg,$origImg,0,0,0,0,$newWidth,$newHeight,ImageSX($origImg),ImageSY($origImg));

/* create final image and free up the memory */
ImagePNG($newImg);
ImageDestroy($newImg); ?>

如果你调用了以上脚本resized.php 并用自己的浏览器访问它,你应该能看到一个35-X-35像素大小的缩微PNG图。

Statement of this Website
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

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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. 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,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

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.

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

See all articles