PHP load image imagecreatefrom_gif_jpeg_png Series Function Usage Analysis, imagecreatefromjpeg
This document analyzes the usage of PHP-loaded imagecreatefrom_gif_jpeg_png functions. We will share this with you for your reference. The details are as follows:
Imagecreatefrom functions are used to load an image from a file or URL.
Load Image
Imagecreatefrom functions are used to load an image from a file or URL. An image resource is returned successfully. If the image fails, an empty string is returned.
The series of functions include:
Imagecreatefromgif (): Creates a canvas and loads an image from a GIF file or URL.
Imagecreatefromjpeg (): Creates a canvas and loads an image from a JPEG file or URL.
Imagecreatefrompng (): Creates a canvas and loads an image from a PNG file or URL.
Imagecreatefromwbmp (): Creates a canvas and loads an image from a WBMP file or URL.
Imagecreatefromstring (): Creates a canvas and creates an image from the image stream in the string.
Syntax:
resource imagecreatefromgif( string filename )resource imagecreatefromjpeg( string filename )resource imagecreatefrompng( string filename )resource imagecreatefromwbmp( string filename )resource imagecreatefromstring( string image )
Example:
<? Header ("Content-type: image/jpeg"); // create and load an image $ im = @ imagecreatefromjpeg ("images/flower_1.jpg "); // error handling if (! $ Im) {$ im = imagecreatetruecolor (150, 30); $ bg = imagecolorallocate ($ im, 255,255,255); $ text_color = imagecolorallocate ($ im, 0, 0,255 ); // fill in the background color imagefilledrectangle ($ im, 0, 0,150, 30, $ bg); // output the error message imagestring ($ im, 3, 5, 5, "Error loading image", $ text_color);} else {// output this image imagejpeg ($ im) ;}?>
In this example, we load and output the source image. Since PHP does not provide friendly error prompts for image creation errors, we have customized error processing information.
Prompt
For PHP-generated images, to be displayed directly on a common webpage rather than output through the header, use the following method: