PHP imagecropauto - 300dpi image coverted to 96dpi

114 views Asked by At

I have a function taking a 10 character string and rendering a 300dpi PNG ready for print. It works great but when using the imagecropauto() function - the resolution of the original is lost and I endup with a file at 96dpi.

$string = "URBANWARFARE";

header('Content-type: image/png');
header("Cache-Control: no-store, no-cache");  
header('Content-Disposition: attachment; filename="name.png"');

$img = imagecreate(4200, 420); // Sets the size of my canvas
imageresolution($img, 300, 300); //  Sets the DPI to 300dpi on X and Y

imagealphablending($img, FALSE);
imagesavealpha($img, TRUE);

$transparent = imagecolorallocatealpha($img, 255, 255, 255, 127); // create transparency

imagefill($img,0,0,$transparent); // apply the transparency to  the image

//imagecolortransparent($img,$transparant);
$textColor = imagecolorallocate($img, 255, 255, 255); // White Text Colour
$font = "./Bloomsbury-Sans.ttf"; // Set the font

imagettftext($img, 380, 0, 0, 410, $textColor, $font, $string); // Draw the text
$cropped = imagecropauto($img,IMG_CROP_DEFAULT); // crop the dead space around the text 

imagepng($img); // 300dpi
imagepng($cropped); // 96dpi

imagedestroy($img);
imagedestroy($cropped);

Interestingly - if I set the file to be 72dpi - the file still comes out of the imagecropauto() as a 96dpi file. I cannot see any mention of this in the documentation - and it seems a very odd resolution to end up with?

1

There are 1 answers

0
UrbanwarfareStudios On BEST ANSWER

Upon writing the line "I cannot see any mention of this in the documentation - and it seems a very odd resolution to end up with?" - I checked again and although it doesn't mention 96dpi here, it does here - so I tried adding the following line after I crop it and low and behold that fixes the issue.

imageresolution($cropped, 300, 300); //  Sets the DPI to 300dpi on X and Y

So for anyone that would find it useful here you go:

$string = "URBANWARFARE";

//echo $string."<br/>";

header('Content-type: image/png');
header("Cache-Control: no-store, no-cache");  
header('Content-Disposition: attachment; filename="name.png"');

$img = imagecreate(4200, 420); // Sets the size of my canvas
imageresolution($img, 300, 300); //  Sets the DPI to 300dpi on X and Y

imagealphablending($img, FALSE);
imagesavealpha($img, TRUE);

$transparent = imagecolorallocatealpha($img, 255, 255, 255, 127); // create transparency
imagefill($img,0,0,$transparent); // apply the transparency to  the image

$textColor = imagecolorallocate($img, 255, 255, 255); // White Text Colour
$font = "./Bloomsbury-Sans.ttf"; // Set the font

imagettftext($img, 380, 0, 0, 410, $textColor, $font, $string); // Draw the text
$cropped = imagecropauto($img,IMG_CROP_DEFAULT); // crop the dead space around the text 
imageresolution($cropped, 300, 300); //  Sets the DPI to 300dpi on X and Y

imagepng($cropped); // 300dpi
//imagepng($cropped); // 96dpi

imagedestroy($img);
imagedestroy($cropped);

Out of interest does anyone know if any other PHP image functions have a similar effect on the resolution where you perhaps wouldn't expect it?