php Auto-trim Text to Image v1 slower but more asthetic improvement ov

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php /* *********************************************************
* Auto-trim Text to Image v1.2
* A slower but more asthetic improvement over using imagettfbbox
* Copyright (C) 2004 GreyWyvern
*
* This program may be distributed under the terms of the GPL
* - http://www.gnu.org/licenses/gpl.txt
*
* !! Requires GD 2.0.8+ or bundled equivalent !!
*
* Changelog:
* 1.2 - Fixed black lines which occasionally appeared at angles:
* 0, 90, 180 and 270
* - Small speed tweak
*
* 1.1 - Better handling of angled text and borders
* - Added angle parameter
* - Added border parameter
*
* Set the default variables below.
* - $font: The full server path to your font file
* - $point: Default point size for text
* - $angle: Text Angle
* - $border: pixels of background colour to leave around text
* - $ratio['x']: squish or stretch horizontally
* - $ratio['y']: squish or stretch vertically
* - $color['text']: Default text colour
* - $color['back']: Default background colour
*
* Call this script using an HTML <img> tag like so:
*
* <img src="path/to/script/auto-trim_1.0.php?text=Text" />
*
* - It is recommended that you set the alt attribute to the text
* string of the image: alt="Text" />
*
* - You can also add up to five optional arguments which can be
* placed in any order. These arguments will override the
* defaults:
* 1) &size=##
* Font Size: ## is the desired point size for text.
* 2) &angle=##
* Font Angle: Desired rotation angle for text.
* 3) &border=##
* Image Border Width: Desired border width (in pixels) of
* background colour to leave around the text
* 4) &tcol=123abc
* Text Colour: 123abc is a hex colour code without the
* preceeding #.
* 5) &tcol=123abc
* Background Colour: 123abc is a hex colour code without the
* preceeding #.
*
* An example request might look like:
* /auto-trim_1.2.php?text=Hello&size=12&tcol=45e29c&bcol=99006e
*
* See the inline comments and http://www.greywyvern.com/php.php
* for more info
*************************************************************** */
/* ***** Set Default Variables ******************************* */
$font = "/path/to/your/font.ttf";
$point = 18;
$angle = 0;
$border = 1;
$ratio['x'] = 1;
$ratio['y'] = 1;
$color['text'] = array(0, 0, 0);
$color['back'] = array(255, 255, 255);
/* ***** Set Variables From $_GET data *********************** */
$text = (isset($_GET['text'])) ? trim(rawurldecode($_GET['text'])) : "Auto-trim 1.2";
if (isset($_GET['size'])) $point = trim(rawurldecode($_GET['size']));
if (isset($_GET['angle'])) $angle = trim(rawurldecode($_GET['angle']));
if (isset($_GET['border'])) $border = trim(rawurldecode($_GET['border']));
if (isset($_GET['tcol']) && preg_match("/[0-9a-f]{6}/i", $_GET['tcol']))
$color['text'] = array(hexdec(substr($_GET['tcol'], 0, 2)), hexdec(substr($_GET['tcol'], 2, 2)), hexdec(substr($_GET['tcol'], 4, 2)));
if (isset($_GET['bcol']) && preg_match("/[0-9a-f]{6}/i", $_GET['bcol']))
$color['back'] = array(hexdec(substr($_GET['bcol'], 0, 2)), hexdec(substr($_GET['bcol'], 2, 2)), hexdec(substr($_GET['bcol'], 4, 2)));
/* ***** Prepare Initial Image ******************************* */
header("Content-type: image/png");
$size = imagettfbbox($point, 0, $font, $text);
$dx = max($size[2], $size[4]) - min($size[0], $size[6]);
$dy = max($size[1], $size[3]) - min($size[5], $size[7]);
$dz = max($dx, $dy);
$im = imagecreatetruecolor($dz * 1.5 + $border * 2, $dz * 1.5 + $border * 2);
$tc = imagecolorallocate($im, $color['text'][0], $color['text'][1], $color['text'][2]);
$bgc = imagecolorallocate($im, $color['back'][0], $color['back'][1], $color['back'][2]);
imagefill($im, 0, 0, $bgc);
imagettftext($im, $point, $angle, (imagesx($im) * (2 - cos(deg2rad($angle)))) / 4, (imagesy($im) * (2 + sin(deg2rad($angle)))) / 4, $tc, $font, $text);
/* ***** Trim To Text **************************************** */
for ($x = 0; $x < imagesx($im); $x++) {
for ($y = 0; $y < imagesy($im); $y++) {
if (!isset($itrim['left'])) {
$rgb = imagecolorat($im, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
if (array($r, $g, $b) != $color['back']) $itrim['left'] = $x - $border;
}
if (!isset($itrim['right'])) {
$rgb = imagecolorat($im, imagesx($im) - $x - 1, imagesy($im) - $y - 1);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
if (array($r, $g, $b) != $color['back']) $itrim['right'] = imagesx($im) - $x + $border;
}
}
}
for ($y = 0; $y < imagesy($im); $y++) {
for ($x = 0; $x < imagesx($im); $x++) {
if (!isset($itrim['top'])) {
$rgb = imagecolorat($im, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
if (array($r, $g, $b) != $color['back']) $itrim['top'] = $y - $border;
}
if (!isset($itrim['bottom'])) {
$rgb = imagecolorat($im, imagesx($im) - $x - 1, imagesy($im) - $y - 1);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
if (array($r, $g, $b) != $color['back']) $itrim['bottom'] = imagesy($im) - $y + $border;
}
}
}
$im2 = imagecreatetruecolor(($itrim['right'] - $itrim['left']) * $ratio['x'], ($itrim['bottom'] - $itrim['top']) * $ratio['y']);
$bgc2 = imagecolorallocate($im2, $color['back'][0], $color['back'][1], $color['back'][2]);
imagecopyresampled($im2, $im, 0, 0, $itrim['left'], $itrim['top'], imagesx($im2), imagesy($im2), $itrim['right'] - $itrim['left'], $itrim['bottom'] - $itrim['top']);
imagefill($im2, 0, 0, $bgc2);
/* ***** Output Image **************************************** */
imagepng($im2);
imagedestroy($im2);
imagedestroy($im);
?>