Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active November 15, 2023 03:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save code-boxx/f6d888cd0211b00d54f6da6ca0335bd5 to your computer and use it in GitHub Desktop.
Save code-boxx/f6d888cd0211b00d54f6da6ca0335bd5 to your computer and use it in GitHub Desktop.
PHP Write Text On Image

PHP WRITE TEXT ON IMAGE

https://code-boxx.com/php-add-text-image/

NOTES

  1. Make sure that extension=gd is enabled in php.ini
  2. In all the scripts, please change the font file $fontFile to your own.

IMAGES

demo

LICENSE

Copyright by Code Boxx

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
// (A) OPEN IMAGE
$img = imagecreatefrompng("demo.png");
// (B) WRITE TEXT
$txt = "BUBBLE TEA";
$fontFile = "C:\Windows\Fonts\arial.ttf"; // CHANGE TO YOUR OWN!
$fontSize = 24;
$fontColor = imagecolorallocate($img, 255, 0, 0);
$posX = 5;
$posY = 24;
$angle = 0;
imagettftext($img, $fontSize, $angle, $posX, $posY, $fontColor, $fontFile, $txt);
// (C) OUTPUT IMAGE
// (C1) DIRECTLY SHOW IMAGE
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
/* (C2) OR SAVE TO A FILE
$quality = 100; // 0 to 100
imagejpeg($img, "demo.jpg", $quality); */
<?php
// (A) OPEN IMAGE
$img = imagecreatefrompng("demo.png");
// (B) TEXT & FONT SETTINGS
$txt = "BUBBLE TEA";
$fontFile = "C:\Windows\Fonts\arial.ttf"; // CHANGE TO YOUR OWN!
$fontSize = 24;
$fontColor = imagecolorallocate($img, 255, 0, 0);
$angle = 0;
// (C) CALCULATE TEXT BOX POSITION
// (C1) GET IMAGE DIMENSIONS
$iWidth = imagesx($img);
$iHeight = imagesy($img);
// (C2) GET TEXT BOX DIMENSIONS
$tSize = imagettfbbox($fontSize, $angle, $fontFile, $txt);
$tWidth = max([$tSize[2], $tSize[4]]) - min([$tSize[0], $tSize[6]]);
$tHeight = max([$tSize[5], $tSize[7]]) - min([$tSize[1], $tSize[3]]);
// (C3) CENTER THE TEXT BLOCK
$centerX = ceil(($iWidth - $tWidth) / 2);
$centerX = $centerX<0 ? 0 : $centerX;
$centerY = ceil(($iHeight - $tHeight) / 2);
$centerY = $centerY<0 ? 0 : $centerY;
// (D) DRAW TEXT ON IMAGE
imagettftext($img, $fontSize, $angle, $centerX, $centerY, $fontColor, $fontFile, $txt);
// (E) OUTPUT IMAGE
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
<?php
// (A) OPEN IMAGE
$img = imagecreatefrompng("demo.png");
// (B) UTF-8 TEXT
// https://stackoverflow.com/questions/9458317/working-with-gd-imagettftext-and-utf-8-characters
$txt = "珍珠奶茶";
$txt = mb_convert_encoding($txt, "HTML-ENTITIES", "UTF-8");
$txt = preg_replace('~^(&([a-zA-Z0-9]);)~', htmlentities('${1}'), $txt);
// (C) WRITE TEXT TO IMAGE
$fontFile = "C:\Windows\Fonts\simsun.ttc"; // CHANGE TO YOUR OWN!
$fontSize = 24;
$fontColor = imagecolorallocate($img, 255, 0, 0);
$posX = 5;
$posY = 24;
$angle = 0;
// (D) WRITE UTF-8 TEXT
imagettftext($img, $fontSize, $angle, $posX, $posY, $fontColor, $fontFile, $txt);
// (E) OUTPUT IMAGE
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment