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

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

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

  1. $string = "URBANWARFARE";
  2. header('Content-type: image/png');
  3. header("Cache-Control: no-store, no-cache");
  4. header('Content-Disposition: attachment; filename="name.png"');
  5. $img = imagecreate(4200, 420); // Sets the size of my canvas
  6. imageresolution($img, 300, 300); // Sets the DPI to 300dpi on X and Y
  7. imagealphablending($img, FALSE);
  8. imagesavealpha($img, TRUE);
  9. $transparent = imagecolorallocatealpha($img, 255, 255, 255, 127); // create transparency
  10. imagefill($img,0,0,$transparent); // apply the transparency to the image
  11. //imagecolortransparent($img,$transparant);
  12. $textColor = imagecolorallocate($img, 255, 255, 255); // White Text Colour
  13. $font = "./Bloomsbury-Sans.ttf"; // Set the font
  14. imagettftext($img, 380, 0, 0, 410, $textColor, $font, $string); // Draw the text
  15. $cropped = imagecropauto($img,IMG_CROP_DEFAULT); // crop the dead space around the text
  16. imagepng($img); // 300dpi
  17. imagepng($cropped); // 96dpi
  18. imagedestroy($img);
  19. imagedestroy($cropped);

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

4ioopgfo

4ioopgfo1#

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

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

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

  1. $string = "URBANWARFARE";
  2. //echo $string."<br/>";
  3. header('Content-type: image/png');
  4. header("Cache-Control: no-store, no-cache");
  5. header('Content-Disposition: attachment; filename="name.png"');
  6. $img = imagecreate(4200, 420); // Sets the size of my canvas
  7. imageresolution($img, 300, 300); // Sets the DPI to 300dpi on X and Y
  8. imagealphablending($img, FALSE);
  9. imagesavealpha($img, TRUE);
  10. $transparent = imagecolorallocatealpha($img, 255, 255, 255, 127); // create transparency
  11. imagefill($img,0,0,$transparent); // apply the transparency to the image
  12. $textColor = imagecolorallocate($img, 255, 255, 255); // White Text Colour
  13. $font = "./Bloomsbury-Sans.ttf"; // Set the font
  14. imagettftext($img, 380, 0, 0, 410, $textColor, $font, $string); // Draw the text
  15. $cropped = imagecropauto($img,IMG_CROP_DEFAULT); // crop the dead space around the text
  16. imageresolution($cropped, 300, 300); // Sets the DPI to 300dpi on X and Y
  17. imagepng($cropped); // 300dpi
  18. //imagepng($cropped); // 96dpi
  19. imagedestroy($img);
  20. imagedestroy($cropped);

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

展开查看全部

相关问题