Get/Set Dpi with PHP Gd/Imagick

How can I set DPI/PPI through an GD resize?

It seems PHP GD extension has no way of specifying the DPI directly when writing an image file. You have two options though:

  • Change the #define GD_RESOLUTION 72 in GD source code to your desired value and recompile the GD extension on your own. Be careful, this will have effect on all images saved by GD on your system.
  • Manipulate the binary file (JPEG or PNG?) to change the DPI value in the file header. DPI is only a meta-value, it has no connection to the pixel data.

Which brings me to the question why are you resizing the image if you only need to change the DPI value? Or the other way round - why do you need to change the DPI when you're resizing the image anyway? Is the image used for print?

how to change the DPI of an image from 72DPI to 300DPI with Imagick

I think this is what you want:

I have setted resolution as pixel/inch with 300 dpi:

<?php
$im = new Imagick();
$im->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
$im->setImageResolution(300,300);
$im->readImage("test.png");
$im->setImageFormat("png");
header("Content-Type: image/png");
echo $im;
?>

Hope this will help you!

How to get Image DPI in PHP

You can go for some image libraries for that. Eg: Imagick, GD Library...

(OR)

You can use the following function,

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

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

return array(hexdec($x),hexdec($y));
}

Already solved this question here... :)

How to Check Photo DPI with PHP

It's too late for me to check now but I think you're looking for Imagick::getImageResolution() and Imagick::setImageResolution() if you need to change the DPIs.

I don't think this is possible with GD, I believe it "converts" all images to 72 DPIs.

How to change Image to jpg and set dpi to 300 using php?

After making some changes it worked for me.

public function pngtojpgAction()   
{
//Code to convert png to jpg image
$input = imagecreatefrompng($this->getRequest()->getParam('imagedata'));
$width=imagesx($input);
$height=imagesy($input);
$output = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($output, 255, 255, 255);
imagefilledrectangle($output, 0, 0, $width, $height, $white);
imagecopy($output, $input, 0, 0, 0, 0, $width, $height);

ob_start();
imagejpeg($output);
$contents = ob_get_contents();
//Converting Image DPI to 300DPI
$contents = substr_replace($contents, pack("cnn", 1, 300, 300), 13, 5);
ob_end_clean();
echo 'data:image/jpeg;base64,'.base64_encode($contents);

}

convert image 75 dpi to 300 dpi using Imagemagick PHP

If you look at the UK Government website for the Passport Office, it says that passport photos need to be at least 600px wide by 750px tall.

Let's start with a photo of adequate quality (if not content) for Mr Bean at 600x750:

Sample Image

If we now resize him down to the same as your image (160x200), then back up you will see the quality has suffered through trying to represent the image at 160x200 and you can't invent all those pixels you lost - they are gone for good. Look at his teeth and the highlights in his eyes:

convert bean.jpg -resize 160x200 -resize 600x750 result.jpg

Sample Image

So, all you can do in Imagick is:

Imagick::resizeImage ( int $columns , int $rows , int $filter , float $blur [, bool $bestfit = FALSE [, bool $legacy = FALSE ]] )

to go back up to 600x750 and experiment with setting the filter to Catrom or Lanczos. But you can't invent stuff that isn't there...



Related Topics



Leave a reply



Submit