202 lines
5.4 KiB
PHP
202 lines
5.4 KiB
PHP
<?php
|
||
|
||
/**
|
||
* Формат для композиции изображений
|
||
*/
|
||
class Tools_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;
|
||
$fontfile = $this->getFontFile($value->fontFamily . $this->fontSuffix($value->fontStyle));
|
||
|
||
$color = intval(substr($value->color, 1), 16);
|
||
if ($value->align[0]) {
|
||
$align = Tools_Drawing::ALIGN_LEFT;
|
||
} elseif ($value->align[2]) {
|
||
$align = Tools_Drawing::ALIGN_RIGHT;
|
||
} else {
|
||
$align = Tools_Drawing::ALIGN_CENTER;
|
||
}
|
||
|
||
if ($value->valign[0]) {
|
||
$valign = Drawing::ALIGN_TOP;
|
||
} elseif ($value->valign[1]) {
|
||
$valign = Tools_Drawing::ALIGN_CENTER;
|
||
} else {
|
||
$valign = Tools_Drawing::ALIGN_BOTTOM;
|
||
}
|
||
|
||
Tools_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 $text; //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);
|
||
}
|
||
}
|
||
}
|
||
|