Skip to content

Instantly share code, notes, and snippets.

@vgrish
Created December 3, 2015 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vgrish/5212d75feea58e96b08b to your computer and use it in GitHub Desktop.
Save vgrish/5212d75feea58e96b08b to your computer and use it in GitHub Desktop.
Генерация изображения с заданным текстом
<?php
define('MODX_API_MODE', true);
require_once dirname(dirname(__FILE__)) . '/core/config/config.inc.php';
require_once MODX_BASE_PATH . 'index.php';
if (empty($_GET['text'])) {
die('введите текст');
}
$opts = [
'background' => MODX_ASSETS_PATH . 'img/background.jpg',
'font' => MODX_BASE_PATH . 'img/arial.ttf',
'save' => MODX_ASSETS_PATH . 'img/g/',
'size' => 30,
'top' => 200,
'left' => 200,
'text' => $_GET['text']
];
$path = $opts['save'] . sha1($opts['text']) . '.jpg';
if (!file_exists($path)) {
$img = imagecreatefromjpeg($opts['background']);
$color = imagecolorallocate($img, 250, 0, 0);
/* выводим текст на изображение */
imagettftext(
$img,
$opts['size'],
0,
$opts['left'],
$opts['top'],
$color,
$opts['font'],
$opts['text']
);
imagejpeg($img, $path, 100);
imagedestroy($img);
}
echo '<img src="' . str_replace(MODX_BASE_PATH, MODX_BASE_URL, $path) . '">';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment