PHP simple way to determine image format

Source: Internet
Author: User
This article describes, In PHP programming to determine the format of the image method, which is also the most basic method, by judging the file extension to achieve. Please refer to the friends who are in need.

PHP to determine the image format, the following is the simplest way:

!--? php/*** to determine the format of the picture * edit by bbs.it-home.org*/$file = "test.jpg"; $filety Pe= Strtolower (STRRCHR ($file, ".")); $arrtype = Array (". jpeg", ". bmp", ". gif", ". jpg"), if (!in_array ($filetype, $arrtype)) {echo ' does not conform to format '; exit;}? --> 

In addition, in PHP to determine the format of the picture, mostly in the process of uploading pictures.

At this point, you can use: $_files[' userfile ' [' type '] to be judged before the file is uploaded.

In addition, PHP has a getimagesize function to get the image size. GetImageSize returns and parameters are as follows. Returns an array with four cells. Index 0 contains the pixel value of the image width, and index 1 contains the pixel value of the image height. Index 2 is a marker of the image type: 1 = gif,2 = jpg,3 = png,4 = swf,5 = psd,6 = bmp,7 = TIFF (Intel byte order), 8 = TIFF (Motorola byte order), 9 = jpc,10 = jp2,11 = jpx,12 = jb2,13 = swc,14 = iff,15 = wbmp,16 = XBM. These tags correspond to the newly added IMAGETYPE constants of PHP 4.3.0. Index 3 is a text string with the content "height=" yyy "width=" xxx ", which can be used directly with the IMG tag.

To introduce these, I hope to help you.

  • 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.