A simple way to determine the image format in php

WBOY
Release: 2016-07-25 08:58:05
Original
1508 people have browsed it
This article introduces the method used to determine the image format in PHP programming. This is also the most basic method, which is achieved by determining the file extension. Friends in need can refer to it.

php determines the image format, the following is the simplest method:

<?php
/**
* 判断图片的格式
* edit by bbs.it-home.org
*/
$file= "test.jpg";
$filetype= strtolower(strrchr($file,"."));
$arrtype = array(".jpeg",".bmp",".gif",".jpg");
if(!in_array($filetype,$arrtype))
{
echo '不符合格式';
exit;
}
?>
Copy after login

In addition, determining the image format in PHP is mostly during the process of uploading images.

At this time, you can use: $_FILES['userfile']['type'] to judge before uploading the file.

In addition, there is a getimagesize function in php, which is used to obtain 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 the tag for 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 new IMAGETYPE constant added in PHP 4.3.0. Index 3 is a text string with the content "height="yyy" width="xxx" ", which can be used directly in the IMG tag.

Let’s introduce these, I hope it will be helpful to everyone.



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