How to read EXIF information of photos in PHP, _php tutorial

Source: Internet
Author: User
Tags phpinfo zts

How to read EXIF information from a photo in PHP,


First to understand what is the EXIF information of the picture

EXIF is an image file format whose data storage is identical to the JPEG format. In fact, the EXIF format is the insertion of digital photos in the head of the JPEG format, including aperture, shutter, white balance, ISO, focal length, date and time, and shooting conditions as well as camera brand, model, color coding, recorded sound during shooting and global Positioning System (GPS), thumbnails, etc. To put it simply, exif=jpeg+ the shooting parameters. As a result, you can browse the EXIF format with any viewing software that can view JPEG files, but not all graphics programs can handle EXIF information.

The above quoted from Baidu Encyclopedia.

EXIF reading photos is not necessary in many cases, but compared to some of the sites that explore photography technology, so the EXIF information to read photos is particularly important, such as the photography forum Hummingbird.

From the Hummingbird Forum, the Red Circle Information section is the program to read the EXIF information of the photos. We download the picture to the local, use the Light and shadow magic hand to open the picture to see its EXIF information, when BG, in addition to light and shadow there are many tools can see the image of the EXIF value.

In addition to the EXIF information in the lens value can not be read out, the rest of the values are read correctly.

Open PHP Module

By default, PHP reads the image EXIF information module is not open, we need to first open this module.

To open the EXIF module requires mbstring support, so first to install mbstring, the following is the Linux environment for example, the other environment similar.

Installing the Mbstring module

First find the PHP source package location, directly into the ext/mbstring, execute the following command installation, the specific parameters to see their environment.

The

copy Code code is as follows:
[Root@lee ext]# cd/data0/software/php/ext/mbstring
[Root@lee mbstring]#/ Usr/local/webserver/php/bin/phpize
Configuring for:
PHP API version:20090626
Zend Module API no:20090626
Zend Extension Api no:220090626
[Root@lee exif]#./configure--with-php-config=/usr/local/webserver/php/bin/ Php-config
[Root@lee mbstring]# make && make install
Installing shared extensions:/usr/local/webserver/ php/lib/php/extensions/no-debug-non-zts-20090626/
Installing header files:/usr/local/webserver/php/include/ php/
[Root@lee mbstring]#

After the installation, we can go into the extensions directory to see if the module exists, the presence indicates that the installation was successful.
Copy the Code code as follows:
[Root@lee mbstring]# cd/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626/
[Root@lee no-debug-non-zts-20090626]# LL
Total dosage 1880
-rwxr-xr-x. 1 root root 414405 June eaccelerator.so
-rwxr-xr-x. 1 root root 1091242 September imagick.so
-rwxr-xr-x. 1 root root 5285 February 15:07 mbstring.so
-rwxr-xr-x. 1 root root 246752 September memcache.so
-rwxr-xr-x. 1 root root 154252 September pdo_mysql.so

Installing the EXIF module

Similar to installing the Mbstring module, first find the source location and CD in and configure the installation, the specific parameters to see their own environment.
Copy the Code code as follows:
[Root@lee exif]# Cd/data0/software/php-5.3.13/ext/exif
[Root@lee exif]#./configure--with-php-config=/usr/local/webserver/php/bin/php-config
[Root@lee exif]# make && make install
Installing Shared extensions:/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626/
[Root@lee exif]#

Enter the extensions directory to verify if the installation was successful

Copy the Code code as follows:
[Root@lee exif]# cd/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626/
[Root@lee no-debug-non-zts-20090626]# LL
Total dosage 2036
-rwxr-xr-x. 1 root root 414405 June eaccelerator.so
-rwxr-xr-x. 1 root root 158554 February 15:25 exif.so
-rwxr-xr-x. 1 root root 1091242 September imagick.so
-rwxr-xr-x. 1 root root 5285 February 15:07 mbstring.so
-rwxr-xr-x. 1 root root 246752 September memcache.so
-rwxr-xr-x. 1 root root 154252 September pdo_mysql.so
[Root@lee no-debug-non-zts-20090626]#

The exif.so module already exists.

adding modules in php.ini
Open PHP.ini Add the following two lines
Copy the Code code as follows: Extension = "mbstring.so"
Extension = "exif.so"

and confirm that your Extension_dir value is the same as the installing shared extensions value that you prompted when you installed the module, such as when I install the module, I am prompted to extensions location is
Copy the Code code as follows:/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626/
So the extension_dir in your php.ini is going to point to the right directory.
Copy the Code code as follows: Extension_dir= "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626/"
Save php.ini and restart webserver.
Open Phpinfo () find the appropriate property to see if it is working correctly

Under normal circumstances you will see the following two modules of information

Use Exif_read_data () to read EXIF information for a picture

The type of picture that supports reading EXIF information is already stated in Phpinfo, which can only be JPEG or TIFF type, where JPEG is a common type, which is sufficient.
Let's take a look at the user's manual for the Exif_read_data () function
Copy the Code code as follows:
Array Exif_read_data (String $filename [, String $sections = NULL [, bool $arrays = False [, bool $thumbnail = false]]] )

Parameters:

FileName: To read the picture path of EXIF information, this cannot be a URL
Sections: is a comma-delimited list of extents that need to exist in the file to produce an array of results. If the requested segment is not found, the return value is FALSE.

FILE FileName, FileSize, FileDateTime, Sectionsfound
COMPUTED Html,width,height,iscolor, there may be more. Height and Width are computed in the same way as getimagesize (), so their values cannot be part of any returned header information. In addition, HTML is a height/width text string that can be used in normal HTML.
Any_tag Any information that contains tags, such as ifd0,exif, ...
IFD0 Tag data for all IFD0. In the standard image file, this includes the image size and other.
THUMBNAIL If there is a second IFD, the file should contain a thumbnail image. All tag information about embedded thumbnails is stored in this area.
COMMENT The comment header information for the JPEG image.
Exif The EXIF section is a sub-region of the IFDO that contains more detailed information about the image. Most of the content is related to digital cameras.

Arrays: Specifies whether each section becomes an array. Sections Computed,thumbnail and Comment sections always become arrays because they contain names that conflict with other sections.

Thumbnail: When set to TRUE, reads the thumbnail itself. Otherwise, only the tag data is read.

Let's try to read the EXIF information of a picture.
Copy the Code code as follows:
<?php
$exif = getexif (' a.jpg ');
Echo '

';
Print_r ($EXIF);
Echo '
';

Execution Result:
Copy CodeThe code is as follows:
Array
(
[FileName] = a.jpg
[FileDateTime] = 1361340032
[FileSize] = 69170
[FileType] = 2
[MimeType] = Image/jpeg
[Sectionsfound] = Any_tag, IFD0, THUMBNAIL, EXIF, GPS, INTEROP
[COMPUTED] = = Array
(
[HTML] = width= "height=" 397 "
[Height] = 397
[Width] = 600
[IsColor] = 1
[Byteordermotorola] = 1
[Aperturefnumber] = f/13.0
[Focusdistance] = 3.76m
[Usercomment] =
[Usercommentencoding] = ASCII
[Copyright] =
[Thumbnail.filetype] = 2
[Thumbnail.mimetype] = Image/jpeg
)
[ImageWidth] = 4928
[Imagelength] = 3264
[BitsPerSample] = = Array
(
[0] = 8
[1] = 8
[2] = 8
)
[Photometricinterpretation] = 2
[Make] = NIKON CORPORATION
[Model] = NIKON D7000
[Orientation] = 1
[Samplesperpixel] = 3
[XResolution] = 3000000/10000
[YResolution] = 3000000/10000
[Resolutionunit] = 2
[Software] = Adobe Photoshop CS5 Windows
[DateTime] = 2013:02:18 20:50:46
[Whitepoint] = = Array
(
[0] = 313/1000
[1] = 329/1000
)
[Primarychromaticities] = = Array
(
[0] = 64/100
[1] = 33/100
[2] = 21/100
[3] = 71/100
[4] = 15/100
[5] = 6/100
)
[Ycbcrcoefficients] = = Array
(
[0] = 299/1000
[1] = 587/1000
[2] = 114/1000
)
[Ycbcrpositioning] = 2
[Copyright] =
[Exif_ifd_pointer] = 500
[Gps_ifd_pointer] = 1248
[THUMBNAIL] = = Array
(
[Compression] = 6
[XResolution] = 72/1
[YResolution] = 72/1
[Resolutionunit] = 2
[Jpeginterchangeformat] = 1362
[Jpeginterchangeformatlength] = 4784
)
[Exposuretime] = 40/10
[Fnumber] = 130/10
[Exposureprogram] = 1
[Isospeedratings] = 1000
[undefinedtag:0x8830] = 2
[Exifversion] = 0230
[Datetimeoriginal] = 2013:02:14 21:12:08
[datetimedigitized] = 2013:02:14 21:12:08
[Componentsconfiguration] =
[Compressedbitsperpixel] = 4/1
[Shutterspeedvalue] = -2/1
[Aperturevalue] = 7400879/1000000
[Exposurebiasvalue] = 2/6
[Maxaperturevalue] = 36/10
[Subjectdistance] = 376/100
[Meteringmode] = 3
[LightSource] = 0
[Flash] = 16
[FocalLength] = 180/10
[Usercomment] = ASCII
[Subsectime] = 10
[Subsectimeoriginal] = 10
[Subsectimedigitized] = 10
[Flashpixversion] = 0100
[ColorSpace] = 65535
[Exifimagewidth] = 600
[Exifimagelength] = 397
[Interoperabilityoffset] = 1216
[Sensingmethod] = 2
[Filesource] =
[Scenetype] =
[Cfapattern] =
[Customrendered] = 0
[Exposuremode] = 1
[Whitebalance] = 0
[Digitalzoomratio] = 1/1
[Focallengthin35mmfilm] = 27
[Scenecapturetype] = 0
[Gaincontrol] = 2
[Contrast] = 0
[Saturation] = 0
[Sharpness] = 0
[Subjectdistancerange] = 0
[undefinedtag:0xa500] = 22/10
[Gpsversion] =
[Interoperabilityindex] = R03
[Interoperabilityversion] = 0100
)

If prompted:
Copy the Code code as follows:
Fatal error:call to undefined function exif_read_data () in/data0/htdocs/www/exif/index.php on line 2

It means that the module is not open, and it may be that you configure which piece is not configured, and reconfigure it just fine.

Extracting useful information from EXIF information read results

From the above implementation results we found that the picture Exif a lot, we just need to filter out the junk information from the rest of the useful just fine. This example writes a PHP function with the usual parameters as a precondition. Common parameters include shutter, device name, aperture, sensitivity, focal length:

Copy the Code code as follows:
<?php
/**
* Read EXIF information for JPEG images
* $img as Picture path
*
* Jones Taiwan Blog
*/

function Getexif ($img) {

$exif = Exif_read_data ($img, ' IFD0 ');

Return Array (
' filename ' = ' $exif [' filename '],
' Equipment brand ' and $exif [' Make '],
' Equipment ' = $exif [' Model '],
' Shutter ' = $exif [' Exposuretime '],
' Aperture ' = $exif [' Fnumber '],
' focal length ' = $exif [' FocalLength '],
' ISO ' = $exif [' isospeedratings ']
);

}

Reading photos

Copy the Code code as follows:
<?php
$exifInfo = getexif (' a.jpg ');
Echo '

';
Print_r ($exifInfo);
Echo '
';

Execution Result:
Copy CodeThe code is as follows:
Array
(
[File name] = 25556306.jpg
[Equipment Brand] = NIKON CORPORATION
[Equipment] = NIKON D3100
[Shutter] = 10/32000
[Iris] = 18/10
[Focal length] = 350/10
[ISO] = 100
)

Other instructions

The EXIF value of the image can be modified by the corresponding tool, so the use of the program to read the image of the EXIF value can only be used as a reference, not to do the real basis.

Interested friends can also visit the online Read EXIF information website http://exif.cn play

EXIF information read through the PHP module is occasionally wrong, or incomplete, in which case we can use third-party tools. Then use PHP to execute the system Linux command read




http://www.bkjia.com/PHPjc/866676.html www.bkjia.com true http://www.bkjia.com/PHPjc/866676.html techarticle The method of reading EXIF information in PHP, first to understand what is the EXIF information of the picture Exif is an image file format, and its data storage is identical to the JPEG format. Actually Exi ...

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