Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

DPI

Help
Anonymous
2011-05-28
2013-04-25

  • Anonymous
    2011-05-28

    Is there a way to set the DPI on the image created? Thanks for any help in this regard!

     
  • Oneag
    Oneag
    2012-02-13

    I was able to figure this out after going through the main code, and thought I should post here for any Googlers.

    Inside qrimage.php, the jpg and png function calls are using imagepng() and imagejpeg() PHP functions.
    http://www.php.net/manual/en/function.imagejpeg.php
    http://www.php.net/manual/en/function.imagepng.php
    ^^ if you are unaware how to use those functions, read more here.

    You can change the DPI by modifying this with a hardcoded default, or creating a dropdown to fill a value for quality ($q).

     
  • Oneag
    Oneag
    2012-02-13

    My final solution (for the jpg() function)

    public static function jpg($frame, $filename = false, $pixelPerPoint = 4, $outerFrame = 4,$saveandprint=FALSE)
            {
                $image = self::image($frame, $pixelPerPoint, $outerFrame);
               
                if ($filename === false) {
                    Header("Content-type: image/jpeg");
                    ImageJpeg($image);
                } else {
                    if($saveandprint===TRUE){
                        ImageJpeg($image, $filename, 100);
                        header("Content-type: image/jpeg");
                        ImageJpeg($image, NULL, 100);
                    }else{
                        ImageJpeg($image, $filename, 100);

      // Change DPI
      $dpi_x   = 300;
      $dpi_y   = 300;
     
      // Read the file
      $size    = filesize($filename);
      $imageContents   = file_get_contents($filename);

      // Update DPI information in the JPG header
      $imageContents  = chr(1);
      $imageContents  = chr(floor($dpi_x/255));
      $imageContents  = chr(      $dpi_x%255);
      $imageContents  = chr(floor($dpi_y/255));
      $imageContents  = chr(      $dpi_y%255);

      // Write the new JPG
      $f = fopen($filename, 'w');
      fwrite($f, $imageContents, $size);
      fclose($f);
                    }
                }
               
                ImageDestroy($image);
            }

    This generates a DPI of 301. :)