Get/set DPI with PHP GD/Imagick?

南楼画角 提交于 2019-11-26 17:14:46

问题


I'm building a Web application that will handle image files that will ultimately be printed, large format.

As part of this, I need to get (i.e. read) and set (i.e. change) the DPI of an image file.

Is this possible through PHP GD or PHP Imagick?

Thanks,

BK


Edit:

The DPI of a image can be accessed through iMagick's getImageResolution method:

public function getDPI() {

    $imgDPI = $this->image->getImageResolution();
    return $imgDPI;

}

and the DPI of an image can be set through iMagick's setImageResolution method:

public function setDPI($DPIX, $DPIY) {

    $this->image->setImageResolution($DPIX,$DPIY);

}

回答1:


in order to use "getImageResolution();" you must be sure that resolution in "PixelsPerInch"...sometimes it can be "PixelsPerCentimeter"

use following code to get image info:

$imagick = new Imagick($filename);
$data = $imagick->identifyimage();
var_dump($data);

result (when PixelsPerInch):

array(11) {
              ["imageName"]=> string(11) "/jpg300.jpg"
              ["format"]=> string(51) "JPEG (Joint Photographic Experts Group JFIF format)"
              ["units"]=> string(13) "PixelsPerInch"
              ["type"]=> string(9) "TrueColor"
              ["colorSpace"]=> string(3) "RGB"
              ["compression"] => string(4) "JPEG"
              ["fileSize"] => string(6) "8.72mb"
              ["mimetype"] => string(10) "image/jpeg"
              ["geometry"] => array(2) {
                        ["width"]  => int(11812)
                        ["height"] => int(7876)
              }
              ["resolution"]=> array(2) {
                    ["x"]=> float(300)
                    ["y"]=> float(300)
              }
              ["signature"]=> string(64) "7fc387ea465ec716e9fd6e233bb1d3740cb509f5667ed2a4df0199e6e664590e"
            }

or (when PixelsPerCentimeter):

    array(11) {
      ["imageName"]=> string(8) "/psm.jpg"
      ["format"]=> string(51) "JPEG (Joint Photographic Experts Group JFIF format)"
      ["units"]=> string(19) "PixelsPerCentimeter"
      ["type"]=> string(9) "TrueColor"
      ["colorSpace"]=> string(3) "RGB"
      ["compression"]=> string(4) "JPEG"
      ["fileSize"]=> string(7) "25.01mb"
      ["mimetype"]=> string(10) "image/jpeg"
      ["geometry"]=>
      array(2) {
        ["width"]=> int(11812)
        ["height"]=> int(7876)
      }
      ["resolution"]=>
      array(2) {
        ["x"]=> float(118.11)
        ["y"]=> float(118.11)
      }
      ["signature"]=> string(64) "b491e059624e79a4dee62d9cc7646019927b2222bfed9ac8dd4342185e648eaf"
    }



回答2:


In a primitive bitmap format like those that GD outputs, the dpi setting is merely a meta information that the processing application can use to convert the pixel size into a physical unit.

As far as I know, it is not possible to manipulate metadata directly in GD. You'd have to use an external library for that.

That said, I don't think it is really necessary. Just generate the image in whatever pixel dimensions you need (the number of pixels is the really relevant information!), and tell the printing process what dpi setting to use.




回答3:


This is my work solution at Joox.io

/**
 * @param $filename
 * @return array
 */
function getImageDPI($filename)
{
    $resolutions = null;

    if (class_exists('Imagick')) {

        $image = new Imagick($filename);
        $resolutions = $image->getImageResolution();

    } else {

        $a = fopen($filename, 'r');
        $string = fread($a, 20);
        fclose($a);

        $data = bin2hex(substr($string, 14, 4));
        $x = substr($data, 0, 4);
        $y = substr($data, 4, 4);

        $resolutions = array('x' => hexdec($x), 'y' => hexdec($y));

    }

    return $resolutions;
}


来源:https://stackoverflow.com/questions/4076936/get-set-dpi-with-php-gd-imagick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!