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?
My Answer:
"ABCDEFGHIJ";
// Create a 300dpi image
$image = imagecreatetruecolor(300, 300);
// Set background color to white
$white = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $white);
// Set text color to black
$black = imagecolorallocate($image, 0, 0, 0);
// Add text to image
imagettftext($image, 12, 0, 10, 30, $black, 'arial.ttf', $string);
// Crop image automatically
$cropped_image = imagecropauto($image, IMG_CROP_DEFAULT);
// Save cropped image as PNG
imagepng($cropped_image, 'cropped_image.png');
// Free up memory
imagedestroy($image);
imagedestroy($cropped_image);
Rate this post
5 of 5 based on 6559 votesComments