( ′∀`)σ≡σ☆))Д′)レ(゚∀゚;)ヘ=З=З=Зε≡(ノ´_ゝ`)ノ
<?php
class TextToImage {
var $fontSize = 12;
var $fontColor = "#8892BF";
var $fontColorAllocated = null;
var $lineHeight = 0;
var $align = "center";
var $text = "Sachin Puri";
var $angle = 0;
var $underline = true;
var $fontFile = 'assets/fonts/HelveticaNeueLTStd-Ex.otf'; //font used
var $image = "san.png";
var $imageWidth = 0;
var $imageHeight = 0;
var $startYPosition=400;
var $padding=0;
var $lines=array();
var $backgroundColor='transparent';
var $elements = "v/uploads/text/name.png";
var $rectangle = 100;
function setStrokeWidth($strokeWidth=0){
$this->strokeWidth = $strokeWidth;
}
function setStrokeColor($colour=array(0, 0, 0)){
$this->strokeColor = $colour;
}
function setText($text){
$this->text=$text;
}
function setFontSize($fontSize){
$this->fontSize=$fontSize*0.75;
}
function setFontFile($fontFile){
$this->fontFile=$fontFile;
}
function setFontColor($fontColorHex){
$this->fontColor = $fontColorHex;
}
function startYPosition($YPosition){
$this->startYPosition = $YPosition;
}
function setLineHeight($lineHeight){
$this->lineHeight=$lineHeight;
}
function setUnderline($isUnderline){
$this->underline=$isUnderline;
}
function setPadding($padding){
$this->padding=$padding;
}
function setRectangle($rectangle){
$this->rectangle=$rectangle;
}
function setHAlignment($alignment){
$this->align=$alignment;
}
function setWidth($width){
$this->imageWidth=$width;
}
function setHeight($height){
$this->imageHeight=$height;
}
function saveImage($int){
$this->saveImage=$int;
}
function setAngle($angle){
$this->angle=$angle;
}
function sety($text_y){
$this->text_y=$text_y;
}
function setBackgroundColor($bgColor){
$this->backgroundColor = $bgColor;
}
/**
* Draw a rectangle
*
* @param integer $x
* @param integer $y
* @param integer $width
* @param integer $height
* @param array $colour
* @param float $opacity
* @param boolean $outline
* @see http://www.php.net/manual/en/function.imagefilledrectangle.php
* @return $this
*/
function textToLines(){
$lines=array();
$wordSizeArray=array();
$arrWords=explode(" ", $this->text);
foreach($arrWords as $word){
$wordSize=imagettfbbox($this->fontSize, $this->angle, $this->fontFile, $word. " ");
$wordWidth=abs($wordSize[0])+abs($wordSize[2]);
$wordSizeArray[]=array('word'=>$word, 'size'=>$wordWidth);
}
$line='';
$lineWidth=0;
foreach($wordSizeArray as $wordDetail){
$word=$wordDetail['word'];
$wordWidth=$wordDetail['size'];
$lineWidth+=$wordWidth;
if($lineWidth<$this->imageWidth-($this->padding*2)){
$line.= ' ' . $word;
}else{
$line.= "\n" . $word;
$lineWidth=$wordWidth;
}
}
return explode("\n",$line);
}
function draw() {
$this->lines = $this->textToLines();
if($this->lineHeight == 0){
if($this->underline){
$this->lineHeight = $this->fontSize + ($this->fontSize*80/100);
}else{
$this->lineHeight = $this->fontSize + ($this->fontSize*60/100);
}
}
$this->startYPosition = $this->fontSize+1;
list($fontColorRed, $fontColorBlue, $fontColorGreen) = $this->hex2rgb($this->fontColor);
if($this->imageHeight==0){
$this->imageHeight = $this->lineHeight * count($this->lines) + ($this->padding*2);
}
$this->image = imagecreatetruecolor($this->imageWidth, $this->imageHeight);
imagealphablending($this->image, false); imagesavealpha($this->image, true);
$this->fontColorAllocated = imagecolorallocate($this->image, $fontColorRed, $fontColorBlue, $fontColorGreen);
if($this->backgroundColor == 'transparent'){
$bgcolor = imagecolorallocatealpha($this->image, 0, 0, 0, 127);
}else{
list($bgColorRed, $bgColorBlue, $bgColorGreen) = $this->hex2rgb($this->backgroundColor);
$bgcolor = imagecolorallocate($this->image, $bgColorRed, $bgColorBlue, $bgColorGreen);
}
imagefill($this->image, 0, 0, $bgcolor);
$this->writeText();
/*$this->rectangle();*/
/*$this->rectangle();*/
if($this->saveImage =="1")
{
imagepng($this->image,"v/uploads/text/".$_GET["pichaNi"].".png");
imagedestroy($this->image);
exit();
}
else
{
header("content-type: image/png");
imagepng($this->image);
imagedestroy($this->image);
}
}
function rectangle(){
$x=0;
$y=0;
$width=100;
$height=50;
$colour=array(35, 233, 234);
$opacity="1.0";
$outline=false;
if($outline === true){
imagerectangle($this->image, $x, $y, $x + $width, $y + $height, imagecolorallocatealpha($this->image, $colour[0], $colour[1], $colour[2], (1 - $opacity) * 127));
}else{
imagefilledrectangle($this->image, $x, $y, $x + $width, $y + $height, imagecolorallocatealpha($this->image, $colour[0], $colour[1], $colour[2], (1 - $opacity) * 127));
}
return $this->image;
}
function getStartXPositionOfLine($xBottomLeft, $lineWidth){
switch ($this->align) {
case 'left':
$startXPosition = abs($xBottomLeft) + $this->padding;
break;
case 'right':
$startXPosition = $this->imageWidth - $lineWidth - 1 - $this->padding;
break;
case 'center':
$startXPosition = ($this->imageWidth - $lineWidth) / 2;
break;
default:
$startXPosition = abs($xBottomLeft);
break;
}
return $startXPosition;
}
function strokeIt() {
$lineNumber=1;
foreach($this->lines as $line){
$line=trim($line);
list($xBLeft, $yBLeft, $xBRight, $yBRight, $xTRight, $yTRight, $xTLeft, $yTLeft) = imagettfbbox($this->fontSize, $this->angle, $this->fontFile, $line);
$lineWidth = abs($xBLeft) + abs($xBRight);
$startXPosition = $this->getStartXPositionOfLine($xBLeft, $lineWidth);
if($lineNumber==1 && $this->padding>0){
$this->startYPosition+=$this->padding;
}
if (!empty($_GET['position']) && empty($_GET['type']))
{
if($strokeWidth > 0){
$opacity = 80;
$strokeColor = imagecolorallocatealpha($this->image, $strokeColor[0], $strokeColor[1], $strokeColor[2], (1 - $opacity) * 127);
for($sx = ($x-abs($strokeWidth)); $sx <= ($x+abs($strokeWidth)); $sx++){
for($sy = ($y-abs($strokeWidth)); $sy <= ($y+abs($strokeWidth)); $sy++){
imagettftext($this->image, $this->fontSize, $this->angle, $_GET['x'] + $offsetx, $_GET['y']+22 + $offsety, $strokeColor, $this->fontFile, $line);
}
}
}
imagettftext($this->image, $this->fontSize, $this->angle, $_GET['x'], $_GET['y']+22, $this->fontColorAllocated, $this->fontFile, $line);
}
else if (!empty($_GET['position']) && !empty($_GET['type']))
{
if($strokeWidth > 0){
$opacity = 80;
$strokeColor = imagecolorallocatealpha($this->image, $strokeColor[0], $strokeColor[1], $strokeColor[2], (1 - $opacity) * 127);
for($sx = ($x-abs($strokeWidth)); $sx <= ($x+abs($strokeWidth)); $sx++){
for($sy = ($y-abs($strokeWidth)); $sy <= ($y+abs($strokeWidth)); $sy++){
imagettftext($this->image, $this->fontSize, $this->angle, $_GET['x'] + $offsetx, $_GET['y']+22 + $offsety, $strokeColor, $this->fontFile, $line);
}
}
}
imagettftext($this->image, $this->fontSize, $this->angle, $startXPosition+$_GET['x'], $this->startYPosition+$_GET['y']-20, $this->fontColorAllocated, $this->fontFile, $line);
}
else
{
if($strokeWidth > 0){
$opacity = 10;
$strokeColor = imagecolorallocatealpha($this->image, $strokeColor[0], $strokeColor[1], $strokeColor[2], (1 - $opacity) * 127);
for($sx = ($startXPosition-abs($strokeWidth)); $sx <= ($startXPosition+abs($strokeWidth)); $sx++){
for($sy = ($this->startYPosition-abs($strokeWidth)); $sy <= ($this->startYPosition+abs($strokeWidth)); $sy++){
}
}
}
imagettftext($this->image, $this->fontSize, $this->angle, $startXPosition, $this->startYPosition, $this->fontColorAllocated, $this->fontFile, $line);
}
if(strlen($line)>0){
$this->underline($startXPosition, $lineWidth);
}
$this->startYPosition+=$this->lineHeight;
$lineNumber++;
}
}
function writeText() {
$lineNumber=1;
foreach($this->lines as $line){
$line=trim($line);
list($xBLeft, $yBLeft, $xBRight, $yBRight, $xTRight, $yTRight, $xTLeft, $yTLeft) = imagettfbbox($this->fontSize, $this->angle, $this->fontFile, $line);
$lineWidth = abs($xBLeft) + abs($xBRight);
$startXPosition = $this->getStartXPositionOfLine($xBLeft, $lineWidth);
if($lineNumber==1 && $this->padding>0){
$this->startYPosition+=$this->padding;
}
if (!empty($_GET['position']) && empty($_GET['type']))
{
imagettftext($this->image, $this->fontSize, $this->angle, $_GET['x'], $_GET['y']+22, $this->fontColorAllocated, $this->fontFile, $line);
}
else if (!empty($_GET['position']) && !empty($_GET['type']))
{
imagettftext($this->image, $this->fontSize, $this->angle, $startXPosition+$_GET['x'], $this->startYPosition+$_GET['y']-20, $this->fontColorAllocated, $this->fontFile, $line);
}
else
{
/* $font_color = imagecolorallocate($background, 89,89,89); // Create blue color
$this->imagettfstroketext($this->image, $this->fontSize, $this->angle, $startXPosition, $this->startYPosition, $this->fontColorAllocated, $this->fontColorAllocated, $this->fontFile, $line, 1);
*/
imagettftext($this->image, $this->fontSize, $this->angle, $startXPosition, $this->startYPosition, $this->fontColorAllocated, $this->fontFile, $line);
}
if(strlen($line)>0){
$this->underline($startXPosition, $lineWidth);
}
$this->startYPosition+=$this->lineHeight;
$lineNumber++;
}
}
function underline($startXPosition, $lineWidth){
if ($this->underline) {
$underlineY=$this->startYPosition+5;
imagesetthickness($this->image, 2);
imageline($this->image, $startXPosition, $underlineY, $startXPosition+$lineWidth, $underlineY, $this->fontColorAllocated);
}
}
function imagettfstroketext(&$image, $size, $angle, $x, $y, &$textcolor, &$strokecolor, $fontfile, $text, $px) {
for($c1 = ($x-abs($px)); $c1 <= ($x+abs($px)); $c1++)
for($c2 = ($y-abs($px)); $c2 <= ($y+abs($px)); $c2++)
$bg = imagettftext($image, $size, $angle, $c1, $c2, $strokecolor, $fontfile, $text);
return imagettftext($image, $size, $angle, $x, $y, $textcolor, $fontfile, $text);
}
function hex2rgb($hex) {
$hex = str_replace("#", "", $hex);
if (strlen($hex) == 3) {
$r = hexdec(substr($hex, 0, 1) . substr($hex, 0, 1));
$g = hexdec(substr($hex, 1, 1) . substr($hex, 1, 1));
$b = hexdec(substr($hex, 2, 1) . substr($hex, 2, 1));
} else {
$r = hexdec(substr($hex, 0, 2));
$g = hexdec(substr($hex, 2, 2));
$b = hexdec(substr($hex, 4, 2));
}
$rgb = array($r, $g, $b);
return $rgb;
}
function pr($str){
echo "<pre>";
print_r($str);
echo "</pre>";
}
}
?>