It could be done with CorelDraw, but this way we excluded any possible mistakes in list of names, omitted time and money consuming printing. Also there are some modules with some image processing but nothing suitable for me.
Prototype of this UDT was successfully used to generate pictures (0.5MB and 8.5MP) over 1500 times during one week.
Only thing i could not do was to send picture to browser without saving it to HDD.
I cal this UDT "invitation_card". I believe parameters are clearly documented at the top of UDT.
Code: Select all
<?php
/*$params['filename']*/ /*mandatory*//*image-template path relative to root directory of site*/
/*$params['font_path']*/ /*mandatory*//*path to "ttf" font, relative to root directory of site*/
/*$params['output_file']*/ /*output file path relative to site root. If output path is not specified - original image path will be apended with OUTPUT_EXTENTION */
/*$params['text']*/ /*text to be written on image template. Mandatory line breaks can be marked as ENDS_OF_LINE and END_OF_LINE*/
/*$params['text_size']= TEXT_SIZE*/ /*text size in px (e.g. '10' ) or persentages ('10%'), default is "5%"*/
/*$params['text_x']=0*/ /*text left side position from left edge of template image in px (e.g '5'), or in percents of image width (e.g. '5%'), or 'center'*/
/*$params['text_y']=0*/ /*text top position from top edge in px (e.g. '5'), or percents of image height (e.g. '5%'), or 'middle'*/
/*$params['wrap']=WRAP_WORD*/ /*'word', 'char', 'false'*/
/*$params['alpha']=ALPHA*/ /*transparent = 0, opaque=127*/
/*$params['text_color']=TEXT_COLOR*/ /*text color, default "0,0,0", available up to "255,255,255"*/
/*$params['text_angle']=TEXT_ANGLE*/ /*text angle, a string representing float value, default "0"*/
/*$params['verical_align']=VERTICAL_ALIGN_TOP*/ /*Set multiline behaviour, default is top line fixed at "$params['text_y']" position and all others are going below. available values are: "top", "bottom" and "middle"*/
/*$params['line_height']=LINE_HEIGHT*/ /*Sets how many times line is higher then text size. Default line height is 1.5 times higher then text size.*/
/*$params['text_align']=ALIGN_LEFT*/ /*Align multi line text. Available values: "left", "right", "center", Default is "left" */
/*---------- usage ---------------
{invitation_card filename='uploads/images/padekos/sablonai/ss_padekos_sablonas.jpg'
text='Sed in turpis sed purus adipiscing rutrum.\nSed ut odio sit amet<br/>justo interdum dapibus'
font_path='modules/TruetypeText/fonts/MyriadWebPro.ttf'
text_size='2.5%'
text_y='10%'
output_file='uploads/images/ss_invitation_1.png'
text_x='center'
text_align='center'
vertical_align='top'
alpha='60'}
<p>Save your picture</p>
<img src="/uploads/images/ss_invitation_1.png" width='580' >
*/
define (CENTER, 'center');
define (MIDDLE, 'middle');
define(TEXT_SIZE, '5%');
define(TEXT_ANGLE, '0');
define(TEXT_COLOR, '0,0,0');
define(LINE_HEIGHT, '1.5');
define(ENDS_OF_LINE, '<br/>,<br>,<BR/>,<BR>');
define(END_OF_LINE, '\n');
define(OUTPUT_EXTENTION, 'jpg');
define(ALPHA, '0');
define (WRAP_WORD, 'word');
define (WRAP_CHAR, 'char');
define (WRAP_FALSE, 'false');
define(VERTICAL_ALIGN_TOP, 'top');
define(VERTICAL_ALIGN_BOTTOM, 'bottom');
define(VERTICAL_ALIGN_MIDDLE, 'middle');
define(ALIGN_LEFT, 'left' );
define(ALIGN_RIGHT, 'right' );
define(ALIGN_CENTER, 'center' );
global $gCms;
$config = $gCms->GetConfig();
$filepath = cms_join_path($config['root_path'], $params['filename']);
/*check file extention after last '.' */
switch (strtolower(substr($filepath, 1 + strrpos($filepath, '.')))){
case 'jpg':
case 'jpeg':
$image = imagecreatefromjpeg($params['filename']);
break;
case 'gif':
$image = imagecreatefromgif($params['filename']);
break;
case 'png':
$image = imagecreatefrompng($params['filename']);
break;
}
/*if image opened succesfully and extention of font file is 'ttf' */
if ($image && strtolower(substr($params['font_path'], 1 + strrpos($params['font_path'], '.'))) == 'ttf'){
/*-----------validitating rest of parameters------------*/
$image_size = getimagesize($params['filename']);
$params['text_size'] = (empty($params['text_size'])) ? TEXT_SIZE : $params['text_size'];
if (substr($params['text_size'], -1) =='%'){
$params['text_size'] = $image_size[1] * (float)trim($params['text_size'],'%') / 100;
}
$params['line_height'] = (empty($params['line_height'])) ? LINE_HEIGHT : (float)$params['line_height'];
$params['text_angle'] = (empty($params['text_angle'])) ? TEXT_ANGLE : (float)$params['text_angle'];
$params['text_x'] = (empty($params['text_x'])) ? CENTER : $params['text_x'];
if(substr($params['text_x'], -1) == '%'){
$params['text_x'] = $image_size[0] * (float)trim($params['text_x'],'%') / 100;
}
$params['text_y'] = (empty($params['text_y'])) ? MIDDLE : $params['text_y'];
if(substr($params['text_y'], -1) == '%'){
$params['text_y'] = $image_size[1] * (float)trim($params['text_y'],'%') / 100;
}
$params['alpha'] = (empty($params['alpha'])) ? ALPHA : (((int)$params['alpha'])%128 );
$params['text_color'] = (empty($params['text_color'])) ? TEXT_COLOR : $params['text_color'];
$text_color = explode(',',$params['text_color']);
// force valid color
for( $i = 0; $i <= 2; $i++){
$text_color[$i] = (empty($text_color[$i])) ? 0 : (int) trim($text_color[$i]);
$text_color[$i] = $text_color[$i] % 256;
}
$color = imagecolorallocatealpha($image, $text_color[0], $text_color[1], $text_color[2], $params['alpha']);
switch ($params['wrap']){
case WRAP_WORD:
case WRAP_CHAR:
case WRAP_FALSE:
break;
default:
$params['wrap'] = WRAP_WORD;
}
switch ($params['verical_align']){
case VERTICAL_ALIGN_TOP:
case VERTICAL_ALIGN_BOTTOM:
case VERTICAL_ALIGN_MIDDLE:
break;
default:
$params['verical_align'] = VERTICAL_ALIGN_TOP;
}
$width_correction = $params['text_x'];
switch ($params['text_align']){
case ALIGN_CENTER:
$width_correction = 0;
case ALIGN_LEFT:
case ALIGN_RIGHT:
break;
default:
$params['text_align'] = ALIGN_LEFT;
}
$params['font_path'] = cms_join_path($config['root_path'], $params['font_path']);
/*----------- END validitating rest of parameters------------*/
/*-----------set text top left coordinates----------------*/
$ends_of_line = explode(',',ENDS_OF_LINE);
$text = $params['text'];
foreach ($ends_of_line as $end_of_line){
$text = str_replace($end_of_line, END_OF_LINE, $text);
}
$text_lines = explode(END_OF_LINE, $text);
if ($params['text_x'] == CENTER){
$text_x = $image_size[0] / 2 ;
}
else{
$text_x = $params['text_x'];
}
$max_line_height = $params['line_height'] * $params['text_size'];
$max_line_width = 0;
$max_chars = 0;
foreach ($text_lines as $text_line){
$bbox = imagettfbbox($params['text_size'], $params['text_angle'], $params['font_path'], $text_line);
$max_line_width = max($max_line_width, ($bbox[2] - $bbox[0]));
$max_chars = max( $max_chars, ((int) floor(strlen($text_line) * ($image_size[0] - $width_correction) / ($bbox[4] - $bbox[0]))));
}
/*------ text wrapping -------*/
/**/ if (($image_size[0] - $text_x) < $max_line_width){
switch ($params['wrap']){
case WRAP_CHAR:
$max_line_width = $image_size[0] + 1; // just to make ensure wrapping process
$i = 0;
for ($i = $max_chars - 1; $i > 0; $i--){
if (($image_size[0] - $width_correction) < $max_line_width){
$max_line_width = 0;
$text_lines = explode(END_OF_LINE, wordwrap($text, $i, END_OF_LINE, TRUE));
foreach($text_lines as $text_line){
$bbox = imagettfbbox($params['text_size'], $params['text_angle'], $params['font_path'], $text_line);
$max_line_width = max($max_line_width, ($bbox[2] - $bbox[0]));
}
}
else {
break;
}
}
break;
case WRAP_FALSE:
//nothing to do
break;
default:
$max_line_width = $image_size[0] + 1; // just to make ensure wrapping process
$i = 0;
for ($i = $max_chars - 1; $i > 0; $i--){
if (($image_size[0] - $width_correction) < $max_line_width){
$max_line_width = 0;
$text_lines = explode(END_OF_LINE, wordwrap($text, $i, END_OF_LINE));
foreach($text_lines as $text_line){
$bbox = imagettfbbox($params['text_size'], $params['text_angle'], $params['font_path'], $text_line);
$max_line_width = max($max_line_width, ($bbox[2] - $bbox[0]));
}
}
else {
break;
}
}
break;
}
if ($i != 0){
$text = $temp_text;
}
}
/*------ END text wrapping -------*/
if ($params['text_y'] == MIDDLE){
$position_y = ($image_size[1] / 2) + ($max_line_height / 2);
}
else{
$position_y = $params['text_y'];
}
$position_y = (int)$position_y;
switch ($params['vertical_align']){
case VERTICAL_ALIGN_MIDDLE :
$max_line_height = $max_line_height / 2;
case VERTICAL_ALIGN_BOTTOM :
$position_y = (int) round( $position_y - ((count($text_lines) - 1 ) * $max_line_height));
break;
}
$position_y = ($position_y < 0)? 0 : $position_y;
$position_y = (int)$position_y;
/*----------- END set text top left coordinates----------------*/
/*----------- horizontal text alignment and drawing -------*/
foreach($text_lines as $text_line){
$bbox = imagettfbbox($params['text_size'], $params['text_angle'], $params['font_path'], $text_line);
$line_width = $bbox[2] - $bbox[0];
switch ($params['text_align']){
case ALIGN_CENTER:
$position_x = $text_x - $line_width / 2;
break;
case ALIGN_RIGHT:
$position_x = $text_x - $line_width;
break;
default:
$position_x = $text_x;
}
$position_x = (int)$position_x;
$position_x = ($position_x < 0)? 0 : $position_x;
imagettftext ( $image , $params['text_size'] , (float)$params['text_angle'] , $position_x , $position_y , $color , $params['font_path'] , $text_line );
$position_y = (int)round($position_y + $max_line_height);
}
/*----------- END horizontal text alignment and drawing -------*/
/*----------- create image file and save to hdd -------------*/
$params['output_file'] = (empty($params['output_file'])) ? $params['filename'].'.'.OUTPUT_EXTENTION : $params['output_file'];
$image_path = cms_join_path($config['root_path'],$params['output_file']);
switch (strtolower(substr($image_path, 1 + strrpos($image_path, '.')))){
case 'jpg':
case 'jpeg':
imagejpeg($image, $image_path );
break;
case 'gif':
imagegif($image, $image_path );
break;
case 'png':
imagepng($image, $image_path );
break;
}
/*----------- END create image file and save to hdd -------------*/
/*header('Content-type: image/jpeg');*/
/*header('Content-Disposition: attachment; filename="padeka.jpeg";');*/
/*imagejpeg($image);*/
/*imagedestroy($image);*/
/*header("Expires: 0");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Disposition: attachment; filename="padeka.jpg";');
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($image_path));
readfile($image_path );*/
}
return;
?>
Code: Select all
{invitation_card filename='uploads/images/padekos/sablonai/ss_padekos_sablonas.jpg'
text='Sed in turpis sed purus adipiscing rutrum.\nSed ut odio sit amet<br/>justo interdum dapibus'
font_path='modules/TruetypeText/fonts/MyriadWebPro.ttf'
text_size='2.5%'
text_y='10%'
output_file='uploads/images/ss_invitation_1.png'
text_x='center'
text_align='center'
vertical_align='top'
alpha='60'}
<p>Save your picture</p>
<img src="/uploads/images/ss_invitation_1.png" width='580' >