Home > Backend Development > PHP Tutorial > PHP loading image imagecreatefrom_gif_jpeg_png series function usage analysis

PHP loading image imagecreatefrom_gif_jpeg_png series function usage analysis

高洛峰
Release: 2023-03-04 09:44:01
Original
1731 people have browsed it

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 )
Copy after login

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);
}
?>
Copy after login

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" />
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template