How can image properties be retrieved in PHP

The Eduladder is a community of students, teachers, and programmers just interested to make you pass any exams. So we solve previous year question papers for you.
In eduladder you can Ask,Answer,Listen,Earn and Download Questions and Question papers.
Watch related videos of your favorite subject.
Connect with students from different parts of the world.
Apply or Post Jobs, Courses ,Internships and Volunteering opportunity. For FREE
See Our team
Wondering how we keep quality?
Got unsolved questions? Ask Questions

PHP-->View question

How can image properties be retrieved in PHP?

its about How can image properties be retrieved in PHP


Asked On2019-09-06 15:46:35 by:Annaliya

Taged users:


Likes:
Be first to like this question

Dislikes:
Be first to dislike this question
Talk about this  Like  Dislike
View all qusetions
Answers

The data or Information that are associated with an image is called as metadata of the images. For example, image type, image width, and height, attributes, created date, last modified date and etc.

This information are not obvious to the user on simply viewing the images. In this article, we have to see how to get metadata of images using PHP script.

PHP provides various functions and DLL to extract image properties from an image. These functions are,

  1. imagesx() and imagesy()
  2. getimagesize()
  3. exif_read_data()

Answerd on:2019-09-09 Answerd By:Ananth

Likes:
|Ananth

Dislikes:
Be first to dislike this answer
Talk about this  Like  Dislike

You might like this video:HTML Basics for Beginners part-2
Watch more here

Watch more videos from this user Here

Learn how to upload a video and start earning here

PHP provides various functions and DLL to extract image properties from an image. These functions are,

  1. imagesx() and imagesy()
  2. getimagesize()
  3. exif_read_data()

The corresponding DLL to be enabled for using these functions are php_mbstring.dll, php_exif.dll. For that, we should search for these all names among php.ini, and could be found as,

;extension=php_mbstring.dll
;extension=php_exif.dll

And then, enable this DLL by removing semicolon(;) at the beginning of each line. And the order should be as shown above to enable mbstring before exif.

imagesx() and imagesy()

The imagesx() and imagesy() are used to extract the width and height of the images respectively. For that, it accepts the resource type of data which will be returned on creating new images dynamically using PHP script. For example, PHP captcha code is created dynamically as images to ensure that the input of web application is entered by the human. If we want to check the width and height of the captcha we have created, then, imagesx() and imagesy() could be used appropriately.

php_get_image_properties

getimagesize()

This PHP method that returns an array of image properties like width, height, image type, mime type and etc. This method will return a limited amount of image data. But, there is no need to send resource data of the image as the argument of this function. Rather, we should specify the path of the image file, which can be either relative or absolute path.

The following PHP program is used to extract the properties of an image. For that, we need to access HTML form data on submitting the selected image file.

First, create HTML content to select the image file for which we need to extract properties. As we have seen, to upload files through HTML form, we need to specify the enctype attribute to the form. But this attribute can be used, if the form method is posted, as like as the following content.

<html>
<head>
<title>Getting Image Properties</title>
</head>
<body>
<form name="frmImage" action="" method="post" enctype="multipart/form-data">
<input type="file" name="myImage" /> 
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

After that, we need to access these from data from a PHP script to be embedded on top of the above content. And the PHP script is,

<?php
if(isset($_POST["submit"])) {
if(is_array($_FILES)) {
$image_properties = getimagesize($_FILES['myImage']['tmp_name']);
print "<PRE>";
print_r($image_properties);
print "</PRE>";
}
}
?>

This script will be executed on submitting the form, and the image file is added to PHP global array, that is, $_FILES. After ensuring that the $_FILES is not empty, then, we should specify the name of the file to getimagesize() as shown above. Finally, image properties are returned as an array and displayed to the browser, in human readable format, by using PHP print statement inside pre tags.

Array
(
    [0] => 1024
    [1] => 768
    [2] => 2
    [3] => width="1024" height="768"
    [bits] => 8
    [channels] => 3
    [mime] => image/jpeg
)

exif_read_data()

Since getimagesize() functions will return limited set of properties, exif_read_data() is used to get more information further associated with images. So, added to the width, height information, it will return a huge list of additional information such as image created date, last modified date, File name, size, orientation, resolution and etc.

This function will be used to extract properties of digital images in which Exif data is stored in its header. Exif is a standard format, that can be expanded as Exchangeable Image Format.

Image types are of 16 totally, which varies based on the devices used to capture images. Image types are returned as numbers that are associated with the available types of images like gif, png and etc. For example, if image type returned as 2 denotes that, it is JPEG image.

Not only the type of the images but also the entire list of image properties returned by this function, will also be varied depends on devices.

In the above program, replace the line which invokes getimagesize() function, that is,

$image_properties = getimagesize($_FILES['myImage']['tmp_name']);

by the following line for getting image properties in EXIF format.

$image_properties = exif_read_data($_FILES['myImage']['tmp_name']);

Answerd on:2019-09-09 Answerd By:Tanisha-Garg

Likes:
Be first to like this answer

Dislikes:
Be first to dislike this answer
Talk about this  Like  Dislike

You might like this video:Data Flow on the Internet
Watch more here

Watch more videos from this user Here

Learn how to upload a video and start earning here



Lets together make the web is a better place

We made eduladder by keeping the ideology of building a supermarket of all the educational material available under one roof. We are doing it with the help of individual contributors like you, interns and employees. So the resources you are looking for can be easily available and accessible also with the freedom of remix reuse and reshare our content under the terms of creative commons license with attribution required close.

You can also contribute to our vision of "Helping student to pass any exams" with these.
Answer a question: You can answer the questions not yet answered in eduladder.How to answer a question
Career: Work or do your internship with us.Work with us
Create a video: You can teach anything and everything each video should be less than five minutes should cover the idea less than five min.How to upload a video on eduladder