php imagettftext Getting blurry text

507 views Asked by At

In very simple explanation i am creating a QR code with a library and then add some text to this image The qrcode is getting fine but when i add the text whatever font i use i get blurry text which when i print it its nearly unreadable. Why is this getting blurry

Here is the function (i am experimenting so there can be some idiotic lines in it)

public function CreateLabel($code, $description)
{
    include(getcwd().DIRECTORY_SEPARATOR.'application'.DIRECTORY_SEPARATOR.'libraries'.DIRECTORY_SEPARATOR.'QrCode'.DIRECTORY_SEPARATOR.'qrlib.php');
    $qrName = preg_replace( '/[^a-z0-9]+/', '_', strtolower($code)).'.png';
    $src_image = D_QR.$qrName;
    QRcode::png($code, $src_image);

    //Extend Image
    list($height, $width) = getimagesize($src_image);
    $newWidth = $width + 253;
    $newHeight = $height + 83;
    $thumbnail = imagecreatetruecolor($newWidth, $newHeight);
    imageantialias($thumbnail,false);
    $white = imagecolorallocate($thumbnail, 255, 255, 255);
    imagefill($thumbnail, 0, 0, $white);
    $img_source = imagecreatefrompng($src_image);
    imagecopyresampled($thumbnail, $img_source, 0, 87, 0, 0, $width, $height, $width, $height);
    imagepng($thumbnail, $src_image, 9);
    imagedestroy($img_source);
    imagedestroy($thumbnail);

    //AddText
    $image = imagecreatefrompng($src_image);
    imageantialias($image,false);
    imagealphablending($image, true);
    $black = imagecolorallocate($image, 0, 0, 0);
    $tnr = getcwd().DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'fonts'.DIRECTORY_SEPARATOR.'Montserrat-Medium.otf';
    $nevis = getcwd().DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'fonts'.DIRECTORY_SEPARATOR.'Montserrat-ExtraBold.otf';
    imagettftext($image, 10, 0, 85,126, $black, $nevis, $code);
    $max_length = 45;
    if (strlen($description) > $max_length)
    {
        $newString = wordwrap($description, $max_length, "||");
        $parts = explode("||", $newString);
        $x = 10;
        $y = 20;
        for($i = 0; $i <= sizeof($parts); $i++)
        {
            imagettftext($image, 9, 0, $x, $y*($i+1), $black, $tnr, $parts[$i]);
            //imagestring($image, $f, $x, $y*($i+1), $parts[$i], $black*-1);
        }
    }
    else imagettftext($image, 9, 0, 12, 20, $black*-1, $tnr, $description);

    imageantialias($image,false);


    imagepng($image, $src_image, 9);
    imagedestroy($image);

    return $qrName;
}

And this is the image it produces: Produced Image

As you can see the description (long part) is not really focused and when you print it get blurry.

I tried many fonts and still get the same result.

I tried also using imagestring which is much better but the 1-5 latin font is not good for me and when i try to use imageloadfont and use a font of my own i get the smallest possible size of the font, and there is no way of changing the font size this way.

Why is this happening or maybe any suggestions?

Note: I am using PHP Version 7.0.33 GD Version: bundled (2.1.0 compatible) libPNG Version 1.6.28

0

There are 0 answers