PHP图像处理- 300dpi图像转换为96 dpi

zbdgwd5y  于 2023-01-01  发布在  PHP
关注(0)|答案(1)|浏览(239)

我有一个函数,取一个10个字符的字符串,渲染一个300dpi的PNG准备打印。它工作得很好,但当使用imagecropauto()函数时-原始的分辨率丢失了,我最终得到了一个96 dpi的文件。

$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);

有趣的是--如果我将文件设置为72 dpi--文件从imagecropauto()中出来时仍然是96 dpi的文件。我在文档中看不到任何提到这一点的内容--这似乎是一个非常奇怪的分辨率。

4ioopgfo

4ioopgfo1#

在写行“我看不到任何提到这在文档中-这似乎是一个非常奇怪的决议结束了吗?”-我再次检查,虽然它没有mention 96dpi hereit does here-所以我尝试添加以下行后,我裁剪它和低,看这修复了问题。

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

因此,对于任何人,会发现它有用的在这里你去:

$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);

出于兴趣,有人知道是否有任何其他PHP图像函数对分辨率有类似的影响,你可能不会期望它?

相关问题