Skip to content

Instantly share code, notes, and snippets.

@afiqiqmal
Created October 8, 2020 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afiqiqmal/34e599780b9443344a152abea783027e to your computer and use it in GitHub Desktop.
Save afiqiqmal/34e599780b9443344a152abea783027e to your computer and use it in GitHub Desktop.
Create an image from Text
<?php
class CreateImageFromText
{
/**
* @param string $text_string
* @param array $request
* @return string
*/
public static function createImageFromText(string $text_string, $request = [])
{
if (!$text_string) {
throw new \RuntimeException("Text cannot be null");
}
$config = [
'font_size' => isset($request['font_size']) ? (!$request['font_size'] || !is_numeric($request['font_size']) ? 20 : $request['font_size']) : 20,
'text_angle' => isset($request['text_angle']) ? (!$request['text_angle'] || !is_numeric($request['text_angle']) ? 0 : $request['text_angle']) : 0,
'text_padding' => isset($request['text_padding']) ? (!$request['text_padding'] || !is_numeric($request['text_padding']) ? 20 : $request['text_padding']) : 20,
];
if (isset($request['bg_rgb'])) {
$rgb = explode(',', $request['bg_rgb']);
if (count($rgb) != 3) {
throw new \RuntimeException("Invalid RGB");
}
}
if (isset($request['txt_rgb'])) {
$trgb = explode(',', $request['txt_rgb']);
if (count($trgb) != 3) {
throw new \RuntimeException("Invalid RGB");
}
}
$font_ttf = resource_path('assets/fonts/Poppins-Regular.ttf');
$the_box = self::calculateTextBox($text_string, $font_ttf, $config['font_size'], $config['text_angle']);
$imgWidth = $the_box["width"] + $config['text_padding'];
$imgHeight = $the_box["height"] + $config['text_padding'];
$image = imagecreate($imgWidth, $imgHeight);
imagefill($image,0,0, imagecolorallocate($image, $rgb[0] ?? 220,$rgb[1] ?? 220,$rgb[2] ?? 220));
$color = imagecolorallocate($image,$trgb[0] ?? 225,$trgb[1] ?? 0,$trgb[2] ?? 0);
imagettftext($image,
$config['font_size'],
$config['text_angle'],
$the_box["left"] + ($imgWidth / 2) - ($the_box["width"] / 2),
$the_box["top"] + ($imgHeight / 2) - ($the_box["height"] / 2),
$color,
$font_ttf,
$text_string);
ob_start();
imagejpeg($image);
$contents = ob_get_contents();
ob_end_clean();
imagedestroy($image);
return 'data:image/png;base64,'.base64_encode($contents);
}
/**
* @param $text
* @param $fontFile
* @param $fontSize
* @param $fontAngle
* @return array
*/
private static function calculateTextBox($text, $fontFile, $fontSize, $fontAngle)
{
$rect = imagettfbbox($fontSize,$fontAngle,$fontFile,$text);
$minX = min(array($rect[0],$rect[2],$rect[4],$rect[6]));
$maxX = max(array($rect[0],$rect[2],$rect[4],$rect[6]));
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));
return array(
"left" => abs($minX) - 1,
"top" => abs($minY) - 1,
"width" => $maxX - $minX,
"height" => $maxY - $minY,
"box" => $rect
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment