PHP Load Image imagecreatefrom_gif_jpeg_png series function usage Analysis _php skills

Source: Internet
Author: User
Tags error handling

This paper analyzes the Imagecreatefrom_gif_jpeg_png series function usage of PHP loading image. Share to everyone for your reference, specific as follows:

The Imagecreatefrom series function is used to load an image from a file or URL.

Loading images

The Imagecreatefrom series function is used to load an image from a file or URL, return an image resource successfully, and return an empty string if it fails.

The series functions are:

Imagecreatefromgif (): Creates a canvas and loads an image from a GIF file or URL address
Imagecreatefromjpeg (): Creates a canvas and loads an image from a JPEG file or URL address
Imagecreatefrompng (): Creates a canvas and loads an image from a PNG file or URL address
Imagecreatefromwbmp (): Creates a canvas and loads an image from a WBMP file or URL address
Imagecreatefromstring (): Creates a canvas, and creates a new image from the image stream in the string

Grammar:

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 a picture
$im = @imagecreatefromjpeg ("images/flower_1.jpg");
Error handling
if (! $im) {
  $im = Imagecreatetruecolor (from);
  $BG = Imagecolorallocate ($im, 255, 255, 255);
  $text _color = imagecolorallocate ($im, 0, 0, 255);
  Fill background color
  imagefilledrectangle ($im, 0, 0, $BG);
  Output error messages in Images
  imagestring ($im, 3, 5, 5, "Error loading image", $text _color);
  Imagejpeg ($im);
>

In this example, we load and output the original image. Because PHP does not have friendly error prompts for image creation errors, we have customized error handling information.

Tips

For PHP-generated images, if you want to display them directly in the normal Web page rather than through the header output, you can invoke the following:



More about PHP Interested readers can view the site topics: "PHP graphics and pictures Operating skills summary", "PHP file Operation Summary", "PHP Array" operation Skills Encyclopedia, "PHP Basic Grammar Introductory Course", "PHP Operations and Operator Usage Summary", " Introduction to PHP object-oriented programming program, "PHP Network Programming Skills Summary", "PHP string (String) Usage Summary", "Php+mysql database Operation Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.