Skip to content

Instantly share code, notes, and snippets.

@tomschlick
Created September 17, 2013 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomschlick/6599457 to your computer and use it in GitHub Desktop.
Save tomschlick/6599457 to your computer and use it in GitHub Desktop.
Simple example for creating an avatar based on the user's initials and a background color
<?php
$string = 'TS';
$imagine = new Imagine\Gd\Imagine();
$size = new Imagine\Image\Box($imgsize, $imgsize);
$color = new Imagine\Image\Color('#333', 100);
$image = $imagine->create($size, $color);
$textImg = new Imagine\Gd\Font(DOCROOT.'/Arial.ttf', ceil($imgsize * .35), new Imagine\Image\Color('#fff'));
$YdescriptionBoxImg = $textImg->box($string, 0)->getHeight();
$XdescriptionBoxImg = $textImg->box($string, 0)->getWidth();
// set the point to start drawing text, depending on parent image width
$YCenter = ceil(($image->getSize()->getHeight() - $YdescriptionBoxImg) / 2);
$XCenter = ceil(($image->getSize()->getWidth() - $XdescriptionBoxImg) / 2);
$image->draw()
->text(
$string,
$textImg,
new Imagine\Image\Point($XCenter, $YCenter)
);
$image->show('jpg');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment