📜 ⬆️ ⬇️

We write in pictures

During the last time, articles about image processing on php often began to flash. There were already rounded edges, there were shadows, there was a wet floor, there was still a whole lot.

But the inscriptions did not seem to be. It means they will. :-)

I present to your attention not a big, but at the same time, quite rich in possibilities class (based on GD) of writing the text in pictures with TrueType-fonts.

Class Code:


<?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 .

')
I think it’s not difficult to figure out what he can do, especially since there are comments. Now use example:

// -
$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 .


Well, actually the result:



And lastly, the possibility of application:
1. Write something in the picture (for example, your signature)
2. To make “glamorous headlines” - when some texts are made with beautiful pictures (in WordPress there’s something like that)
3. CAPTCHA (just need to take a completely e ** nuyu font)

Use on health!

PS: Designed for UTF-8 encoding

Source: https://habr.com/ru/post/43744/


All Articles