I found solutions here and here using Imagick but I really do not want go though Imagick extension due security issues.
Currently, I'm doing the resize as follow:
list($width, $height) = getimagesize($file_path);
$src = imagecreatefrompng($file);
$dst = imagecreatetruecolor($newwidth, $newheight);
//prepare the transparency
imagealphablending($dst, false);
imagesavealpha( $dst , true );
$transparent = imagecolorallocatealpha($dst, 255, 255, 255, 127);
imagefilledrectangle($dst, 0, 0, $newwidth, $newheight, $transparent);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
But it are setting always 72 DPI (overriding even the original file DPI).
There have some way to set DPI using GD?
It seems PHP GD extension has no way of specifying the DPI directly when writing an image file. You have two options though:
#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.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?