search
HomeBackend DevelopmentPHP TutorialPHP脚本的10个技巧(4)

动态创建图象
在安装了某些第三方函数库之后,结合你的图形处理技能,你就可以用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
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

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

Video Face Swap

Video Face Swap

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

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

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.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor