AgjGd::linebreaksfortext Example: Before and After

The opening of A Tale of Two Cities is rendered twice for comparison. On the left it goes straight to imagettftext and runs off the edge of the image on a single line; on the right the same text is passed through this method first, which inserts line breaks so that it wraps to the width of the image.

Source Code: examples/linebreaksfortext/01-before-and-after.php

<?php

/**
 * AgjGd Example (linebreaksfortext: Before and After)
 *
 * Copyright (c) 2018–2026 Andrew G. Johnson <andrew@andrewgjohnson.com>
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * PHP version 8
 *
 * @category  AndrewGJohnson
 * @package   AgjGd
 * @author    Andrew G. Johnson <andrew@andrewgjohnson.com>
 * @copyright 2018–2026 Andrew G. Johnson <andrew@andrewgjohnson.com>
 * @license   https://opensource.org/licenses/mit/ The MIT License
 * @link      https://github.com/andrewgjohnson/AgjGd
 */

use AndrewGJohnson\AgjGd;

// Load the AgjGd class directly from source, or fall back to the Composer autoloader
if (file_exists('../../source/AndrewGJohnson/AgjGd.php')) {
    require_once '../../source/AndrewGJohnson/AgjGd.php';
} elseif (file_exists('../../vendor/autoload.php')) {
    require_once '../../vendor/autoload.php';
} elseif (!class_exists('AndrewGJohnson\AgjGd')) {
    die('AndrewGJohnson\AgjGd class not found');
}

// Set the parameters for our images
$fontPath     = dirname(__DIR__) . '/NotoSans-Regular.ttf';
$fontSize     = 10;
$fontAngle    = 0;
$textPadding  = 10;
$imageWidth   = 300;
$imageHeight  = 600;
$imagePadding = 25;

$text = file_get_contents(dirname(__DIR__) . '/a-tale-of-two-cities.txt');

if ($text === false) {
    die('Could not read the text');
}

// Generate the before image, whose text overflows horizontally because it has no line breaks
$beforeIm = imagecreatetruecolor($imageWidth, $imageHeight);

if ($beforeIm === false) {
    die('Could not create the image');
}

imagefill($beforeIm, 0, 0, (int)imagecolorallocate($beforeIm, 0xFF, 0xDD, 0xDD));

imagettftext(
    $beforeIm,
    $fontSize,
    $fontAngle,
    $textPadding,
    $textPadding + $fontSize,
    (int)imagecolorallocate($beforeIm, 0x66, 0x00, 0x00),
    $fontPath,
    $text
);

// Generate the after image, whose text is wrapped to the image’s width
$afterIm = imagecreatetruecolor($imageWidth, $imageHeight);

if ($afterIm === false) {
    die('Could not create the image');
}

imagefill($afterIm, 0, 0, (int)imagecolorallocate($afterIm, 0xDD, 0xFF, 0xDD));

imagettftext(
    $afterIm,
    $fontSize,
    $fontAngle,
    $textPadding,
    $textPadding + $fontSize,
    (int)imagecolorallocate($afterIm, 0x00, 0x66, 0x00),
    $fontPath,
    AgjGd::linebreaksfortext(
        $fontSize,
        $fontAngle,
        $fontPath,
        $text,
        imagesx($afterIm) - ($textPadding * 2)
    )
);

// Generate a single larger image to house both the before and after images
$im = imagecreatetruecolor(
    ($imagePadding * 3) + ($imageWidth * 2),
    ($imagePadding * 2) + $imageHeight
);

if ($im === false) {
    die('Could not create the image');
}

imagefill($im, 0, 0, (int)imagecolorallocate($im, 0xEE, 0xEE, 0xEE));

// Add the before image on the left
imagecopy($im, $beforeIm, $imagePadding, $imagePadding, 0, 0, $imageWidth, $imageHeight);

// Add the after image on the right
imagecopy($im, $afterIm, ($imagePadding * 2) + $imageWidth, $imagePadding, 0, 0, $imageWidth, $imageHeight);

// Display our image
header('Content-Type: image/png');
imagepng($im);

Expected Output: examples/linebreaksfortext/01-before-and-after.png

Example: Before and After