【问题标题】:PHP imagecropauto - 300dpi image coverted to 96dpiPHP imagecropauto - 300dpi 图像转换为 96dpi
【发布时间】:2022-12-30 22:09:48
【问题描述】:

我有一个函数接受一个 10 个字符的字符串并渲染一个 300dpi PNG 准备打印。 它工作得很好,但是当使用 imagecropauto() 函数时 - 原始分辨率丢失,我最终得到一个 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);

有趣的是 - 如果我将文件设置为 72dpi - 该文件仍然作为 96dpi 文件从 imagecropauto() 中出来。我在文档中看不到任何提及 - 这似乎是一个非常奇怪的解决方案?

【问题讨论】:

    标签: php image-resolution


    【解决方案1】:

    在写下“我在文档中看不到任何提及 - 这似乎是一个非常奇怪的解决方案?”这一行时- 我再次检查,虽然它没有 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 图像函数对您可能不期望的分辨率有类似的影响吗?

    【讨论】:

      猜你喜欢
      • 2012-05-20
      • 2019-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多