Home > Blockchain >  php imagejpeg() function allways set quality of 96 dpi
php imagejpeg() function allways set quality of 96 dpi

Time:07-26

after uploading an image I place it in a folder using imagejpeg()
documentation says that this function resamples the image to a given quality, depending on third param in the function:

imagejpeg($newimg, $path, 80);

problem - whatever value I set as quality (50 - 70 - 99 ...) the resulting image is always 96 dpi

so how to get an image having 72 dpi ?

CodePudding user response:

DPI and quality are two completely different things.

The DPI is a piece of metadata that tells software how to scale the number of pixels in the image into inches. (This is generally only used when printing onto paper and in image editing software such as Photoshop; rendering the image on a website will work in pixels not inches).

The quality determines how much compression to use.


The imageresolution function lets you adjust the DPI metadata.

imageresolution($image, 72);

This comment, on the page you linked to, explains how to adjust the DPI setting manually.

<?php

  imagejpeg($image, $file, 75);

  // Change DPI
  $dpi_x   = 150;
  $dpi_y   = 150;
 
  // Read the file
  $size    = filesize($file);
  $image   = file_get_contents($file);

  // Update DPI information in the JPG header
  $image[13] = chr(1);
  $image[14] = chr(floor($dpi_x/256));
  $image[15] = chr(      $dpi_x%6);
  $image[16] = chr(floor($dpi_y/256));
  $image[17] = chr(      $dpi_y%6);

  // Write the new JPG
  $f = fopen($file, 'w');
  fwrite($f, $msg, $size);
  fclose($f);

?>
  • Related