PHP GD库生成图像的几个函数总结
这篇文章主要介绍了PHP GD库生成图像的几个函数总结,即imagegif、imagejpeg、imagepng、imagewbmp几个生成图片的函数,需要的朋友可以参考下
使用GD库中提供的函数动态绘制完成图像以后,就需要输出到浏览器或者将图像保存起来。在PHP中,可以将动态绘制完成的画布,直接生成GIF、JPEG、PNG和WBMP四种图像格式。可以通过调用下面四个函数生成这些格式的图像:
复制代码 代码如下:
bool imagegif(resource $image[,string $filename]) //以GIF格式将图像输出
bool imagejpeg(resource $image[,string $filename[,int $quality]]) //以JPEG格式将图像输出
bool imagepng(resource $image[,string $filename]) //以PNG格式将图像输出
bool imagewbmp(resource $image[,string $filename[,int $foreground]]) //以WBMP格式将图像输出
以上四个函数的使用类似,前两个参数的使用是相同的。第一个参数$image为必选项,是前面介绍的图像引用句柄。如果部位这些函数提供其他参数,访问时则直接将原图像流出,并在浏览器使用中显示动态输出的图像。但一定要在输出之前,,使用header()函数发送标头信息,用来通知浏览器使用正确的MIME类型对接收的内容进行解析,让它知道我们发送的是图片而不似乎文本的HTML。以下代码段通过自动检测GD库支持的图像类型,来写出移植性更好的PHP程序。如下所示:
复制代码 代码如下:
if(function_exists("imagegif")){ //判断生成GIF格式图像的函数是否存在
header("Content-type:image/gif"); //发送标头信息设置MIME类型为image/gif
imagegif($im); //以GIF格式将图像输出到浏览器
}elseif(function_exists("imageipeg")){
header("Content-type:image/jpeg");
imagejpeg($im,"",0.5);
}elseif(function_exists("imagepng")){
header("Content-type:image/png");
imagepng($im);
}elseif(function_exists("imagewbmp")){
header("Content-type:image/wbmp");
imagewbmp($im);
}else{
die("在PHP服务器中,不支持图像");
}
?>
如果希望将PHP动态绘制的图像保存在本地服务器上,则必须在第二个可选参数中指定一个文件名字符串。这样不仅不会将图像直接输出到浏览器,也不需要使用header()函数发送标头信息。如果使用imageJPEG()函数生成JPEG格式的图像,还可以通过第三个可选参数$quality指定JPEG格式图像的品质,该参数可以提供的值是从0(最差品质,但文件最小)到100(最高品质,文件也最大)的整数,默认值为75.也可以为函数imageWBMP()提供第三个可选参数$forground,指定图像的前景颜色,默认颜色值为黑色。

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 this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
