Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Image {
- function __construct() {
- $this->fontSize = mt_rand(12, 48);
- $this->rotationDegree = mt_rand(0, 360);
- $this->font = 'http://ireader.googlecode.com/files/arial.ttf';
- }
- public function set($var, $val) {
- $this->$var = $val;
- }
- public function backgroundImage($src) {
- $this->source = imagecreatefrompng($src);
- $this->width = imagesx($this->source);
- $this->height = imagesy($this->source);
- }
- public function color($hex) {
- if ($hex[0] == '#') {
- $hex = substr($hex, 1);
- }
- if (strlen($hex) == 6) {
- list($r, $g, $b) = array($hex[0].$hex[1],
- $hex[2].$hex[3],
- $hex[4].$hex[5]);
- }
- else if (strlen($color) == 3) {
- list($r, $g, $b) = array($hex[0].$hex[0], $hex[1].$hex[1], $hex[2].$hex[2]);
- }
- else {
- return false;
- }
- $this->fontColor['r'] = hexdec($r);
- $this->fontColor['g'] = hexdec($g);
- $this->fontColor['b'] = hexdec($b);
- }
- public function text($text, $maxLength = false) {
- $this->text = ucfirst(strtolower(($maxLength !== false ? (strlen($text) <= $maxLength ? $text : substr($text, 0, $maxLength)) : $text)));
- }
- public function output() {
- $newImage = imagecreatetruecolor($this->width, $this->height);
- imagealphablending($newImage, false);
- imagesavealpha($newImage, true);
- $transparent = imagecolorallocatealpha($newImage, 255, 255, 255, 127);
- imagefilledrectangle($newImage, 0, 0, $this->width, $this->height, $transparent);
- imagecopyresampled($newImage, $this->source, 0, 0, 0, 0, $this->width, $this->height, $this->width, $this->height);
- imagealphablending($newImage, true);
- $xbox = imagettfbbox($this->fontSize, $this->rotationDegree, $this->font, $this->text);
- $x = ($this->width / 2) - (((($xbox[2] - $xbox[0]) + ($xbox[4] - $xbox[6])) / 2) / 2);
- $ybox = imagettfbbox($this->fontSize, $this->rotationDegree, $this->font, substr($this->text, 0, 1));
- $y = ($this->height / 2) - (((($ybox[3] - $ybox[5]) + ($ybox[1] - $ybox[7])) / 2) / 2) + ($ybox[3] - $ybox[5]);
- $color = imagecolorallocate($newImage, $this->fontColor['r'], $this->fontColor['g'], $this->fontColor['b']);
- imagettftext($newImage, $this->fontSize, $this->rotationDegree, $x, $y, $color, $this->font, $this->text);
- imagepng($newImage);
- imagedestroy($newImage);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement