function GetTextRowsFromText($fontSize, $font, $text, $maxWidth)
{
    $text = str_replace("\n", "\n ", $text);
    $text = str_replace("\\n", "\n ", $text);
    $words = explode(" ", $text);
    $rows = array();
    $tmpRow = "";
    for ($i = 0; $i < count($words); $i++) {
        //last word
        if ($i == count($words) - 1) {
            $rows[] = $tmpRow . $words[$i];
            break;
        }
        if (GetTextWidth($fontSize, $font, $tmpRow . $words[$i]) > $maxWidth) {
            $rows[] = $tmpRow;
            $tmpRow = "";
        } else {
            if (StringEndsWith($tmpRow, "\n ")) {
                $tmpRow = str_replace("\n ", "", $tmpRow);
                $rows[] = $tmpRow;
                $tmpRow = "";
            }
        }
        //add new word to row
        $tmpRow .= $words[$i] . " ";
    }
    return $rows;
}
Exemplo n.º 2
0
$watermark_im = imagecreatefrompng($watrmarkPath);
$imageSize = getimagesize($backgroundPath);
$width = $imageSize[0];
$height = $imageSize[1];
//get textRows
$textRows = GetTextRowsFromText($fontSize, $font, $text, $width - $padding * 2);
//colors
$colorWhite = imagecolorallocate($im, 255, 255, 255);
$colorBlack = imagecolorallocate($im, 0, 0, 0);
$colorGrey = imagecolorallocate($im, 130, 130, 130);
//border
//imagerectangle($im, 0, 0, $width - 1, $height - 1, $colorGrey);
for ($i = 0; $i < count($textRows); $i++) {
    //text size
    $line_box = imagettfbbox($fontSize, 0, $font, $textRows[$i]);
    $text_width = GetTextWidth($fontSize, $font, $textRows[$i]);
    $text_height = GetMaxTextHeight($fontSize, $font, $textRows) * 2.3;
    //align: center
    $position_center = ceil(($width - $text_width) / 2);
    //valign: middle
    $test = count($textRows) - $i - ceil(count($textRows) / 2);
    $position_middle = ceil(($height - $text_height * $test) / 2);
    imagettfstroketext($im, $fontSize, 0, $position_center, $position_middle, $colorBlack, $colorWhite, $font, $textRows[$i], 0);
}
// Inserting watermark at the end of text
$x = ceil(($width - 200) / 2);
$y = $position_middle + 10;
imagecopymerge($im, $watermark_im, $x, $y, 0, 0, 200, 37, 100);
//Image File Name
$file_name = "../../../Quotes/" . $ID . ".png";
imagepng($im, $file_name);