Question

I'm looking for a solution in php to convert my images from 300 DPI to 72 DPI. So that the script check first if img == 72 DPI do nothing else convert to 72 DPI. And after that check size and resize image.

Something like this.

if ($image(dpi) > 72 dpi) {
   convert to 72 DPI;
}
else {
}
Was it helpful?

Solution 2

To convert an image to a different resolution you need ImageMagick.

$img = new Imagick($imgname);
if ($img) {
  $width=$img->getImageWidth();
  $height=$img->getImageHeight();
  $res=$img->getImageResolution();
  $colorspace=$img->getImageColorspace();

  $resx=$res['x'];
  $resy=$res['y'];
  echo 'Image is '.$width.'x'.$height.' resolution: '.$resx.'x'.$resy.' colorspace='.$colorspace.'='.$colorspace_array[$colorspace];
  $cmw=($width/$resx)*2.54;
  $cmh=($height/$resy)*2.54;
  echo 'Image is '.$cmw.'cm x '.$cmh.'cm';

  // creating 72dpi version
  $w72=round($width*72/$resx);
  $h72=round($height*72/$resy);
  if ($w72>$width || $h72>$height) {
    $w72=$width;
    $h72=$height;
  }

  $img->resizeImage($w72,$h72,imagick::FILTER_QUADRATIC,1);
  $img->writeImage('newimage.png');

} else {
  die('Unknown image format');
}

OTHER TIPS

Use below line of code to convert image dpi from 300 to 72 dpi :

$filename = "Enter path of the image which you want to use";

$image = file_get_contents($filename);
$image =substr_replace($image, pack("cnn", 1, 72, 72), 13, 5); file_put_contents($filename,$image);

Try this:

$image = imagecreatefromjpeg($source);
$image = imagecreatefromgif($source);
$image = imagecreatefrompng($source);

imagejpeg($source_image, $destination_image, $quality); //i'd say keep quality up to 70. anything over that is too much for no reason

My suggestion is simply limit the upload size and your problem is solved.

But to answer your question, using ImageMagick:

$img = new Imagick();
$img->setResolution(25,25);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top