Регистр файлов
This commit is contained in:
parent
4fd0187ea6
commit
c8958cbee0
83 changed files with 25 additions and 53 deletions
206
src/Tools/TemplateImage.php
Normal file
206
src/Tools/TemplateImage.php
Normal file
|
|
@ -0,0 +1,206 @@
|
|||
<?php
|
||||
|
||||
require_once 'core/tools/drawing.php';
|
||||
|
||||
/**
|
||||
* Формат для композиции изображений
|
||||
*/
|
||||
class TemplateImage
|
||||
{
|
||||
static $listfiles = array('jpg' => 'jpeg', 'gif' => 'gif', 'png' => 'png', 'bmp' => 'wbmp');
|
||||
static $listfonts = array(
|
||||
'georgia' => 'georgia.ttf',
|
||||
'georgiabd' => 'georgiab.ttf',
|
||||
'georgiaz' => 'georgiaz.ttf',
|
||||
'times' => 'times.ttf',
|
||||
'timesbd' => 'timesbd.ttf',
|
||||
'arial' => 'arial.ttf',
|
||||
'arialbd' => 'arialbd.ttf',
|
||||
'tahoma' => 'tahoma.ttf',
|
||||
'calibri' => 'calibri.ttf',
|
||||
'calibribd' => 'calibrib.ttf',
|
||||
'calibrii' => 'calibrii.ttf',
|
||||
'' => 'arial.ttf',
|
||||
'dejavu' => 'DejaVuCondensedSerif.ttf',
|
||||
'dejavubd' => 'DejaVuCondensedSerifBold.ttf',
|
||||
'dejavuz' =>'DejaVuCondensedSerifBoldItalic.ttf',
|
||||
'dejavui' => 'DejaVuCondensedSerifItalic.ttf',
|
||||
'miriad' => 'MyriadPro-Cond.ttf',
|
||||
'miriadbd' => 'MyriadPro-BoldCond.ttf'
|
||||
|
||||
);
|
||||
|
||||
protected $src;
|
||||
protected $context = array();
|
||||
protected $data = array();
|
||||
protected $base = "c:\\windows\\fonts\\";
|
||||
protected $image;
|
||||
protected $prepare = true;
|
||||
public $debug = false;
|
||||
|
||||
function __construct ($template = false)
|
||||
{
|
||||
// assert(is_string($src));
|
||||
if($template) {
|
||||
$this->data = $template;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Путь к изображению
|
||||
*/
|
||||
function resourcePath($path)
|
||||
{
|
||||
assert(is_string($path));
|
||||
|
||||
$this->resource = $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Путь у шрифтам
|
||||
*/
|
||||
function fontPath($path)
|
||||
{
|
||||
assert(is_string($path));
|
||||
|
||||
$this->base = $path;
|
||||
}
|
||||
|
||||
function set($name, $value)
|
||||
{
|
||||
assert(is_string($name));
|
||||
|
||||
$this->context['['.$name.']'] = $this->encode($value);
|
||||
}
|
||||
|
||||
function setImage($name)
|
||||
{
|
||||
$this->filename = $name;
|
||||
$this->image = $this->imagefromfile($name);
|
||||
}
|
||||
|
||||
function setEmptyImage($width, $height)
|
||||
{
|
||||
$this->image = imagecreatetruecolor($width, $height);
|
||||
}
|
||||
|
||||
/**
|
||||
* Создает изображение из файла
|
||||
*/
|
||||
function imagefromfile($file)
|
||||
{
|
||||
assert(is_string($file));
|
||||
|
||||
$suffix = pathinfo($file, PATHINFO_EXTENSION);
|
||||
if (array_key_exists($suffix, self::$listfiles)) {
|
||||
return call_user_func('imagecreatefrom' . self::$listfiles[$suffix], $file);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function getFontFile($name)
|
||||
{
|
||||
assert(is_string($name));
|
||||
|
||||
if(array_key_exists(strtolower($name), self::$listfonts)) {
|
||||
return $this->base . self::$listfonts[$name];
|
||||
}
|
||||
return $this->base . 'arial.ttf';
|
||||
}
|
||||
|
||||
function fontSuffix($style)
|
||||
{
|
||||
if($style[0] && $style[1]) return "z";
|
||||
|
||||
if($style[0]) return "bd";
|
||||
if($style[1]) return "i";
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
function imageText($text, $value)
|
||||
{
|
||||
assert(is_string($text));
|
||||
|
||||
$text = strtr($text, $this->context);
|
||||
$size = $value->fontSize;
|
||||
fb('font-style');
|
||||
fb($value->fontStyle);
|
||||
$fontfile = $this->getFontFile($value->fontFamily . $this->fontSuffix($value->fontStyle));
|
||||
|
||||
$color = intval(substr($value->color, 1), 16);
|
||||
if ($value->align[0]) {
|
||||
$align = Drawing::ALIGN_LEFT;
|
||||
} elseif ($value->align[2]) {
|
||||
$align = Drawing::ALIGN_RIGHT;
|
||||
} else {
|
||||
$align = Drawing::ALIGN_CENTER;
|
||||
}
|
||||
|
||||
if ($value->valign[0]) {
|
||||
$valign = Drawing::ALIGN_TOP;
|
||||
} elseif ($value->valign[1]) {
|
||||
$valign = Drawing::ALIGN_CENTER;
|
||||
} else {
|
||||
$valign = Drawing::ALIGN_BOTTOM;
|
||||
}
|
||||
|
||||
Drawing::imagettftextbox($this->image, $size, 0, $value->left, $value->top, $color, $fontfile, $text,
|
||||
$value->width, $value->height,
|
||||
$align, $valign);
|
||||
}
|
||||
|
||||
/**
|
||||
* Перекодировка текста
|
||||
*/
|
||||
function encode($text)
|
||||
{
|
||||
assert(is_string($text));
|
||||
return iconv("WINDOWS-1251", "UTF-8", $text);
|
||||
}
|
||||
|
||||
function setSize($new_width, $new_height)
|
||||
{
|
||||
$width = imagesx($this->image);
|
||||
$height = imagesy($this->image);
|
||||
if($new_height == false) {
|
||||
$new_height = ceil($height * $new_width / $width);
|
||||
}
|
||||
|
||||
// Resample
|
||||
$image_p = imagecreatetruecolor($new_width, $new_height);
|
||||
imagecopyresampled($image_p, $this->image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
|
||||
// imagecopyresized($image_p, $this->image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
|
||||
$this->image = $image_p;
|
||||
}
|
||||
|
||||
function prepare() {
|
||||
if($this->prepare) {
|
||||
$this->prepare = false;
|
||||
foreach ($this->data as $value) {
|
||||
$this->imageText($value->text, $value); // break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Генерирует изображение из шаблона
|
||||
*/
|
||||
function render($file = null)
|
||||
{
|
||||
assert(is_string($file) || is_null($file));
|
||||
|
||||
$this->prepare();
|
||||
|
||||
if ($file == null) {
|
||||
ob_start();
|
||||
imagejpeg($this->image, $file, 100);
|
||||
$result = ob_get_contents();
|
||||
ob_clean();
|
||||
return $result;
|
||||
} else {
|
||||
return imagejpeg($this->image, $file, 100);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue