Home » Php » how to get DPI value of an uploaded image using GDI library – PHP

how to get DPI value of an uploaded image using GDI library – PHP

Posted by: admin February 22, 2020 Leave a comment

Q(Question):

Hey all,
I want to get the DPI value of uploaded image. Is there any way to get the dpi value of an uploaded image using GD Library.

Kindly help me out to sort out my problem as i will be very grateful to you guys.

kind regards,
Mohsin Rafique

A(Answer):

I do not believe you are able to get the DPI via GD.

Mark.

A(Answer):

I’m also not sure, which formats actually save a DPI value…

A(Answer):

Then is there any algorithm/formula which find the DPI value of an uploaded image? If so then kindly tell me as if it will not happen then i will be really in big trouble.

A(Answer):

the DPI value is something print devices care about. so only a very few formats (like .psd) save that value. I have not heard, that bitmap image formats (bmp, jpg, png, gif) save such data (why should they?).

A(Answer):

Then there are websites which tells the dpi value of an uploaded image. Then how can they do that? For reference here is a website which tellls the dpi value of an uploaded image instantly once we uplaod an image

Reference Website Link

A(Answer):

do you mean that second area ("Format auswählen”)? that has nothing to do with DPI, that’s the output size (and according to that, the DPI value is calculated)

I should try first… nevertheless, the result is the same, the DPI value (aka quality) is calculated by dividing the pixel dimensions by the size dimensions.

A(Answer):

No i am talking about Qualität: which comes once the image upload. It show that property in just under Auflösung: (Uploaded image area view section).

Just upload any picture and it will tells something about Auflösung, Qualität, Tonwert, Material, Format, Zuschnitt etc. In Qualität property it shows also the dpi value. I want to know how it calculate that dpi value.

A(Answer):

@neovantage

dpi = pixels / size

note:
if you uplaod the same picture with different format settings, you’ll see different dpi values.

q.e.d.

A(Answer):

One more thing which i want ti clear sir
pixel = width * Height;
and size is the given format on the website which is Format auswählen: or size is the actuall size of uploaded image which we get from getimagesize() function in php? Please ellaborate more the formula which you guide me. it will be really a great help from your end.

A(Answer):

@neovantage

wrong

In digital imaging, a pixel (or picture element) is the smallest item of information in an image.

@neovantage

obviously* the size comes from "Format auswählen” (since that is the only option providing you with a length unit).

* – basic scientific deduction gives:
DPI = Dots Per Inch,

Dots per inch (DPI) is a measure of spatial printing or video dot density, in particular the number of individual dots that can be placed within the span of one linear inch (2.54 cm). The DPI value tends to correlate with image resolution, but is related only indirectly.

– Dot is the smallest printable unit
– Inch is a size (length) dimension, can be converted into mm, cm, dm, m, km, ft, yd, mi (…)
– there is no definition of the size of a pixel (see above)

If you set Dot = Pixel (aka PPI)

PPI can also describe the resolution, in pixels, of an image to be printed within a specified space. […] Good quality photographs usually require 300 pixels per inch when printed.

you get (according to the unit) the formula:

DPI = pixels(Dots) /(Per) size(Inch)

Q.E.D

A(Answer):

Some one share code on youtube to get DPi value of an image.
Do you think it is good. here is the code


function get_dpi($filename){
// open the file and read first 20 bytes.
$a = fopen($filename,'r');
$string = fread($a,20);
fclose($a);
// get the value of byte 14th up to 18th
$data = bin2hex(substr($string,14,4));
$x = substr($data,0,4);
$y = substr($data,4,4);
return array(hexdec($x),hexdec($y));
}
// output the result:
print_r(get_dpi('filename.jpg'));

A(Answer):

this has clearly nothing to do with DPI.

the function returns 2 (!) values, internally named $x and $y (!) (image dimension). so, how is that the single DPI value?

A(Answer):

Hmm ok.
I have write php code for dpi code now as you guide me. So will please tell me that is it right or wrong.


//My uploaded image resolution is 850 x 567
$pixels = 481950;
// my select format is 30 x 20 cm
$inch_first = 30 / 2.54;
$inch_second = 20 / 2.54;
$inch = $inch_first * $inch_second;
$DPI = $pixels /$inch;

A(Answer):

this would be the squared DPI. if picture and format are similar (i.e. the width/height ratio is the same) you just need to take the square root. otherwise you have to calculate the vertical and horizontal DPI values separately.

A(Answer):

And how can i calculate the verticle and horizental DPI values seperately as the appliaction i am working on most of the people never give the picture as per predefined format say selected format is 30 x 20 cm.

A(Answer):

get the width & height of the image.
determine wether it is portrait or landscape.
DPI (horizontal) = image width / format width
DPI (vertical) = image height / format height

if picture and format are not similar, you have to crop the image (or print canvas)

A(Answer):

get the width & height of the image.
determine wether it is portrait or landscape.
DPI (horizontal) = image width / format width
DPI (vertical) = image height / format height

I have find out the DPI (horizontal) and DPI (vertical) but how can i actaully show one single dpi of an image?

A(Answer):

@neovantage

not at all.

DPI is related to printing not to images (as the wikipedia article points out).

even text has a DPI value when printed and what is a “single dpi” of the text?

A(Answer):

Hmm ok
But can you tell me how this website Link finding the dpi value of uploading images against selected "Format auswählen"?

As i need to do that in my client appliction and i am stuck with that. Please please help me out to sort out my problem

A(Answer):

@neovantage

They’re not finding the DPI of an image. They have set widths that you select from and, I assume, that value is then passed to a printer.

A(Answer):

Printer in a sense means? And if it connects to a printer then how it returns the dpi value of that particular uploaded image?

A(Answer):

don’t mistake PPI for DPI (like on that website).

A(Answer):

I didnt get you sir. Please ellaborate it in more details as i asked different question and what is that PPI?

A(Answer):

see post #11
_______________

A(Answer):

Thanks but how what’s the formula then to calculate the PPI value of an uploaded image and my 2nd question posterlia.de means finding the PPI value sof an uploaded image instead od DPI value am i right?

A(Answer):

@neovantage

see post #17
_____________

A(Answer):

@neovantage

the point is that hardly any user has ever heard off PPI, but DPI as a resolution value is known.

another example: if I were to explain the molecular structure of ammonia (NH3), I’d go for the VSEPR model, although I know that many aspects are incorrect. explaining the phosphane (PH3) structure (and comparing it with ammonia) I had to use the correct, but rather difficult MO theory.

A(Answer):

see post #17

I have done that already but it is giving me the DPI’s horizentally and vertically seperately. I need to show the DPi value of the image. Not specifically horizentally or vertically respectively.

Here is my code which i have done that seperately.


$sizes[0] = intval($sizes[1]); // Having height selected of Format auswählen
$sizes[1] = intval($sizes[0]); // Having width selected of Format auswählen
if($sizes[0]>$sizes[1]){ //
$DPIRandom_w = $o_wd / $sizes[0];
$DPIRandom_h = $o_ht / $sizes[1];
}else{
$DPIRandom_w = $o_wd / $sizes[1];
$DPIRandom_h = $o_ht / $sizes[0];
}
$DPIRandom_w = $o_wd / $sizes[0];

Here is the url of my application Link
Just upload an image n you will have the results. but i need to show 1 single value sir like posterlia.de.

A(Answer):

@neovantage

take ther smaller one. or insert canvas then you can take the larger one

A(Answer):

take ther smaller one.

Does it means smaller value from DPI (horizontal) or DPI (verticle)

or insert canvas then you can take the larger one

I didnt get that.

A(Answer):

@neovantage
*sigh* yes

@neovantage

what do you do in case the chosen format is 30×20 cm and the picture is 400×200 px?

A(Answer):

Sir
if my picture is of 400×200 and my selected format is 30x20cm then i get the values of DPI (horizontal) and DPI (verticle) by divding 400/30 and 200/20 respectively. So this is all i do and i get the DPI values of horizentally and vertically. This is all i do.

*note: if 30 greater than 20 then i divide 400 by 30 and 200 by 20 and vice versa

A(Answer):

Sir Dormilich,
where are you please help me out. I am badly stuck in it and i have less time. Pleaseeeeeeeee do something for me i will be really very grateful to you

A(Answer):

where are you stuck?

A(Answer):

Sir you asked question from me

what do you do in case the chosen format is 30×20 cm and the picture is 400×200 px?

And i answered that

Sir
if my picture is of 400×200 and my selected format is 30x20cm then i get the values of DPI (horizontal) and DPI (verticle) by divding 400/30 and 200/20 respectively. So this is all i do and i get the DPI values of horizentally and vertically. This is all i do.

*note: if 30 greater than 20 then i divide 400 by 30 and 200 by 20 and vice versa

But i am still unable to get the single DPI value as porterlia.de website calculating.

This is my website URL where i am acalculating DPi value but these are horizental and verticle as you suggested the formula. it gives 2 seperate values. I need one single value sir

A(Answer):

well. starting with posterlia.de. they do exactly calculate as expected. if I upload a picture (520×824) in 30 x 20 cm (vertical 69.8, horizontal 66.0), they correctly calculate a value of 70 ppi.
ranaposter.de however calculates 72 ppi (rounded to the next standard resolution, I suppose)
you calculate vertical 41.2, horizontal 17.33, that doesn’t even have the right width-height-ratio. I have no idea how you get those results.

EDIT: I have an idea. you forgot two things.determine whether it’s portrait or landscape centimeters are not inches

A(Answer):

Sir here is my code.


<?
if($sizes[0]>$sizes[1]){
//echo "Width of actauly uploaded image: ".$o_wd ;
//echo "height of actauly uploaded image: ".$o_ht;
//echo "Width of selected format image: ".$sizes[0];
//echo "height of selected format image: ".$sizes[1];
$DPIRandom_w = $o_wd / $sizes[0];
$DPIRandom_h = $o_ht / $sizes[1];
}else{
//echo "Width of actauly uploaded image: ".$sizes[1];
//echo "height of actauly uploaded image: ".$sizes[0];
//echo "height of selected format image: ".$sizes[0];
//echo "Width of selected format image: ".$sizes[1];
$DPIRandom_w = $o_wd / $sizes[1];
$DPIRandom_h = $o_ht / $sizes[0];
}
?>

A(Answer):

all I can say is that ranaposter fails to do the format detection (a landscape image is calculated correctly*, a portrait image is not)

* see previous post, EDIT section