In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to output pictures in PHP", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "how to output pictures in PHP"!
Image processing is a common thing in PHP work. We encounter many scenes that need to process images, such as image Captcha, image watermark and so on. In fact, the focus of learning image processing is actually learning PHP GD system functions, PHP image processing needs GD library support, then we first look at the GD library related knowledge bar.
GD library
GD library can be understood as a function library for creating graphics images. GD library is actually written in C language and can be used in PHP. In addition to PHP, it can also be used in Perl and other languages. GD library provides many interfaces for processing images.
GD library can be used in PHP page drawing a variety of graphic images, statistics, and image Captcha can also be used to achieve GD library.
Examples are as follows:
After input, if two options appear in the output as shown below, it indicates that the current machine supports processing images with GD:
Or you can use:
After input, if the output result is as shown in the figure below, it also means that the current machine supports GD to process images.
If it is not installed properly, it can also be done by simply deleting the comment in front of "extension=php_gd2.dll" in the php.ini configuration file under Windows.
In PHP, the operation of processing images through GD libraries is generally divided into four basic steps:
create a canvas
drawn image
output image
release resources
Through these four steps we have completed the basic operation of the image, you next let's take a look at how to operate it.
create a canvas
All of our drawing and image processing needs to be done on a background image, that is, canvas, which can be understood as an image. We have a temporary area in memory as a canvas to store image information. Later image operations will be carried out on this area, that is, canvas.
imagecreate() and imagecreatetruecolor() are used to create a canvas, and their syntax is as follows:
imagecreate(int $width, int $height)imagecreatetruecolor(int $width, int $height)
where $width represents the width of the canvas created, and $height represents the height of the canvas created, which is the height of the image
Both of these functions can be used to create a canvas, but the difference is that they can accommodate different color ranges.
Examples are as follows:
Since only a canvas is created and nothing is on it, the browser will not output the canvas, but you can get the width and height of the image by imagesx() and imagesy():
Output:
This completes the creation of a canvas.
In addition to using the above two functions to create a canvas, you can also create an image from a file or url by:
imagecreatefromgif(): Create an image from a GIF file or URL
imagecreatefromjpeg(): Create an image from JPEG file or UR
imagecreatefrompng(): Create an image from PNG file or UR L
imagecreatefromwbmp(): Create an image from a WBMP file or URL
output picture
In the above example, we have successfully created the canvas. In PHP, different formats of images also require different functions to output. The different functions are as follows:
imagegif(): Output a GIF format image to a browser or file
imagejpeg(): outputs a JPEG image to a browser or file
imagepng(): outputs a PNG image to a browser or file
Their syntax is as follows:
imagegif(resource $image[, string $filename])imagejpeg(resource $image[, string $filename[, int $quality]])imagepng(resource $image[, string $filename])
Where $image is an image resource created;$filename is a parameter used to set the path where the file needs to be saved.
Examples are as follows:
First of all, I have saved an image file called php.png in my root directory
Output:
The imagecreatefrompng() function and imagepng() successfully output a png image.
Release image resources
The above example has successfully output the picture, after the use of the picture resources, usually need to release the image processing occupied memory, then how to operate?
In PHP we release image resources through the imagestro () function, which has the following syntax:
imagedestroy(resource $image)
where $image represents an image that needs to be released
Examples are as follows:
Because the operation of releasing image resources has no output content, the output results are not listed.
At this point, I believe that we have a deeper understanding of "how to output pictures in PHP," may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.