<?php
class ttfTextOnImage
{
// jpg -
public $jpegQuality = 100;
//
public $ttfFontDir = 'ttf' ;
private $ttfFont = false ;
private $ttfFontSize = false ;
private $hImage = false ;
private $hColor = false ;
public function __construct($imagePath)
{
if (!is_file($imagePath) || !list(,,$type) = @getimagesize($imagePath)) return false ;
switch ($type)
{
case 1: $ this ->hImage = @imagecreatefromgif($imagePath); break ;
case 2: $ this ->hImage = @imagecreatefromjpeg($imagePath); break ;
case 3: $ this ->hImage = @imagecreatefrompng($imagePath); break ;
default : $ this ->hImage = false ;
}
}
public function __destruct()
{
if ($ this ->hImage) imagedestroy($ this ->hImage);
}
/**
*
*
*/
public function setFont($font, $size = 14, $color = false , $alpha = false )
{
if (!is_file($font) && !is_file($font = $ this ->ttfFontDir. '/' .$font))
return false ;
$ this ->ttfFont = $font;
$ this ->ttfFontSize = $size;
if ($color) $ this ->setColor($color, $alpha);
}
/**
*
*
*/
public function writeText ($x, $y, $text, $angle = 0)
{
if (!$ this ->ttfFont || !$ this ->hImage || !$ this ->hColor) return false ;
imagettftext(
$ this ->hImage,
$ this ->ttfFontSize, $angle, $x, $y + $ this ->ttfFontSize,
$ this ->hColor, $ this ->ttfFont, $text);
}
/**
* ( ),
* ($bWidth, $bHeight)
*
*/
public function textFormat($bWidth, $bHeight, $text)
{
// ,
//
$strings = explode( "\n" ,
preg_replace( '!([^\s]{24})[^\s]!su' , '\\1 ' ,
str_replace(array( "\r" , "\t" ),array( "\n" , ' ' ), $text)));
$textOut = array(0 => '' );
$i = 0;
foreach ($strings as $str)
{
// ,
$words = array_filter(explode( ' ' , $str));
foreach ($words as $word)
{
// ?
$sizes = imagettfbbox($ this ->ttfFontSize, 0, $ this ->ttfFont, $textOut[$i].$word. ' ' );
// ,
//
//
if ($sizes[2] > $bWidth) $textOut[++$i] = $word. ' ' ; else $textOut[$i].= $word. ' ' ;
// ,
if ($i*$ this ->ttfFontSize >= $bHeight) break (2);
}
// ""
$textOut[++$i] = '' ; if ($i*$ this ->ttfFontSize >= $bHeight) break ;
}
return implode ( "\n" , $textOut);
}
/**
* #34dc12
*
*/
public function setColor($color, $alpha = false )
{
if (!$ this ->hImage) return false ;
list($r, $g, $b) = array_map( 'hexdec' , str_split(ltrim($color, '#' ), 2));
return $alpha === false ?
$ this ->hColor = imagecolorallocate($ this ->hImage, $r+1, $g+1, $b+1) :
$ this ->hColor = imagecolorallocatealpha($ this ->hImage, $r+1, $g+1, $b+1, $alpha);
}
/**
* . .
*
*/
public function output ($target, $replace = true )
{
if (is_file ($target) && !$replace) return false ;
$ext = strtolower(substr($target, strrpos($target, "." ) + 1));
switch ($ext)
{
case "gif" :
imagegif ($ this ->hImage, $target);
break ;
case "jpg" :
case "jpeg" :
imagejpeg($ this ->hImage, $target, $ this ->jpegQuality);
break ;
case "png" :
imagepng($ this ->hImage, $target);
break ;
default : return false ;
}
return true ;
}
}
?>
* This source code was highlighted with Source Code Highlighter .
// -
$ttfImg = new ttfTextOnImage( 'images/hlwn.jpg' );
// Scrawn 64 80%-
$ttfImg->setFont( 'files/fonts/scra.ttf' , 64, "#800000" , 80);
$ttfImg->writeText(40, 570, "Happy halloween!" );
// Constantin 15 90%-
$ttfImg->setFont( 'files/fonts/constan.ttf' , 15, "#ff8200" , 90);
// ,
$message = $ttfImg->textFormat(400, 500,
" (. Halloween) — , 31 1 .
« ». ." );
// (- )
$ttfImg->writeText(40, 100, $message, 5);
//
$ttfImg->output( 'images/postcard.jpg' );
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/43744/
All Articles