Find JPEG resolution with PHP

Calling all PHP gurus!

I understand that you can use getimagesize () to get the actual height and width of the image pixel in PHP. However, if you open the image in Photoshop and look at the image size dialog box, you will notice that there is a resolution value that determines the print size of the image.

Given an arbitrary jpg image file, I need to use PHP to determine this permission number. It seems that this information is stored in the jpg file somewhere, and how do I get to it?

One more requirement - I have only gdlib. I need to do this without using other php libraries (imagemagick, etc.)

Thanks for the help!

+4
source share
5 answers

You can simply read the JPEG file directly, specify in bytes 14-18:

  • byte 14: 01, X and Y density unit specifier (00: no, pixel ratio, 01: DPI, 02: DPC)
  • bytes 15-16: horizontal pixel density,
  • byte 16-18: vertical pixel size

Also see: http://www.obrador.com/essentialjpeg/headerinfo.htm

+6
source

There are two places where resolution can potentially be saved (i.e., the JPEG resolution for printing, also referred to in abbreviated form as DPI or dots per inch).

The first is in the JFIF JPEG header, which is often (but NOT always) right at the beginning of the JPEG.

The other is in EXIF ​​data.

, , , . . , . , JPEG (,), .

, JFIF, , APP0, . ( SOI.):

function read_JFIF_dpi($filename)
{
    $dpi = 0;
    $fp = @fopen($filename, r);
    if ($fp) {
        if (fseek($fp, 6) == 0) { // JFIF often (but not always) starts at offset 6.
            if (($bytes = fread($fp, 16)) !== false) { // JFIF header is 16 bytes.
                if (substr($bytes, 0, 4) == "JFIF") { // Make sure it is JFIF header.
                    $JFIF_density_unit = ord($bytes[7]);
                    $JFIF_X_density = ord($bytes[8])*256 + ord($bytes[9]); // Read big-endian unsigned short int.
                    $JFIF_Y_density = ord($bytes[10])*256 + ord($bytes[11]); // Read big-endian unsigned short int.
                    if ($JFIF_X_density == $JFIF_Y_density) { // Assuming we're only interested in JPEGs with square pixels.
                        if ($JFIF_density_unit == 1) $dpi = $JFIF_X_density; // Inches.
                        else if ($JFIF_density_unit == 2) $dpi = $JFIF_X_density * 2.54; // Centimeters.
                    }
                }
            }
        }
        fclose($fp);
    }
    return ($dpi);
}
+5

: PHP JPEG - : http://www.ozhiker.com/electronics/pjmt/

, , , jfif- jpeg. script, XDensity YDensity ( x y) jpg:

<?php

include_once("./JPEG.php");
include_once("./JFIF.php");

$image_header = get_jpeg_header_data("./myImage.jpg");
$image_info = get_JFIF($image_header);

print( "XDensity:" . $image_info['XDensity'] . "<br />");
print( "YDensity:" . $image_info['YDensity'] . "<br />");

?>
+3

. Pixels = printsize x resolution, - . , 300x300 , 1 "x1" 300 DPI, 2 "x2" 150 DPI 4 "x4" 75 DPI .. , .

? ( ? =])

+1

Depending on how the image is saved, EXIF ​​contains a metric load of information. Read more about this in the PHP manual . You may need to sort out / process the results a bit (e.g. flash information, or at least it was just a byte expressing different states).

0
source

All Articles