note 93705 deleted from function.imagecreate by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 11:17:05 +0000 |
| Subject: | note 93705 deleted from function.imagecreate by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-197072@lists.php.net to get a copy of this message | ||
Note Submitter: dan at wow dot com
----
if you want to create an image with a specific DPI, you can't control this directly from GD.
If you have access to gd.h (rarely on a shared hosting) you can modify GD_RESOLUTION and give it
your desired value.
The solution that I found was directly manipulating the jpeg header bytes.
I hope this sample code would helps someone because it took me 5 hours of digging to figure out i
have no other options.
<?php
header('Content-Disposition: attachment; filename="myimg.jpg"');
header('Cache-Control: private');
$dst = imagecreatetruecolor(300, 300); /*this creates a 300x300 pixels image*/
ob_start(); /*don't send the output to the browser since we'll need to manipulate it*/
ImageJpeg($dst);
$img = ob_get_contents();
ob_end_clean();
//byte #14 in jpeg sets the resolution type: 0=none, 1=pixels per inch, 2=pixels per cm
//bytes 15-16 sets X resolution
//bytes 17-18 sets Y resolution
$img = substr_replace($img, pack('cnn',1,300,300),13,5);
echo $img;
?>