This article analyzes the usage of the imagecreatefrom_gif_jpeg_png series of functions for loading images in PHP. Share it with everyone for your reference, the details are as follows:
imagecreatefrom series of functions are used to load an image from a file or URL.
Load image
The imagecreatefrom series of functions are used to load an image from a file or URL, and return the image resource if successful, or an empty string if failed.
This series of functions include:
imagecreatefromgif(): Create a canvas and load an image from a GIF file or URL address
imagecreatefromjpeg(): Create a canvas and load it from Load an image from a JPEG file or URL
imagecreatefrompng(): Create a canvas and load an image from a PNG file or URL
imagecreatefromwbmp(): Create a canvas and load an image from a WBMP file or URL Address loads an image
imagecreatefromstring(): Create a canvas and create a new 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"); //创建并载入一幅图像 $im = @imagecreatefromjpeg("images/flower_1.jpg"); //错误处理 if(!$im){ $im = imagecreatetruecolor(150, 30); $bg = imagecolorallocate($im, 255, 255, 255); $text_color = imagecolorallocate($im, 0, 0, 255); //填充背景色 imagefilledrectangle($im, 0, 0, 150, 30, $bg); //以图像方式输出错误信息 imagestring($im, 3, 5, 5, "Error loading image", $text_color); } else { //输出该图像 imagejpeg($im); } ?>
In this example, we load and output the original image. Since PHP does not have friendly error prompts for image creation errors, we customized the error handling information.
Tips
For pictures generated by PHP, if you want to display them directly in a normal web page instead of outputting them through the header, you can call them in the following way:
<img src="pic.php" />
I hope this article will be helpful to everyone in PHP programming.
For more PHP loading image imagecreatefrom_gif_jpeg_png series function usage analysis and related articles, please pay attention to the PHP Chinese website!