-
Notifications
You must be signed in to change notification settings - Fork 7
/
functions.php
149 lines (122 loc) · 4.64 KB
/
functions.php
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
<?php
/**
* Memegen
*
* Generate meme-style image
*
* memegen_build_image()
* memegen_font_size_guess()
* memegen_imagettfstroketext()
* memegen_sanitize()
*
*/
/**
* Build the image based on array of args
*
* @param array $args
* top_text string Text for top of image
* bottom_text string Text for bottom of image
* filename string Filename to save image as
* font string Path to font file
* memebase string Path to base image
* textsize int Font size
* textfit bool Fit text to image
* linespacing int Line spacing // Pending
* padding int Padding between text and image
* @return void
*/
function memegen_build_image( $args = array() ) {
list( $width, $height ) = getimagesize( $args['memebase'] );
$args['textsize'] = empty( $args['textsize'] ) ? round( $height/10 ) : $args['textsize'];
extract( $args );
// alright, lets make an image
$im = imagecreatefromjpeg( $args['memebase'] );
// make base image transparent
$black = imagecolorallocate( $im, 0, 0, 0 );
imagecolortransparent( $im, $black );
$textcolor = imagecolorallocate( $im, 255, 255, 255 );
$angle = 0;
$top_text = strtoupper( trim( $args['top_text'] ) );
$bottom_text = strtoupper( trim( $args['bottom_text'] ) );
$fit = isset( $textfit ) ? $textfit : true;
// top layer text
extract( memegen_font_size_guess( $textsize, ($width-$padding*2), $font, $top_text, $fit ) );
$from_side = ($width - $box_width)/2;
$from_top = $box_height + $padding;
// imagettftext( $im, $textsize, $angle, $from_side, $from_top, $textcolor, $font, $top_text );
memegen_imagettfstroketext( $im, $fontsize, $angle, $from_side, $from_top, $textcolor, $black, $font, $top_text, 1 );
// bottom layer text
extract( memegen_font_size_guess( $textsize, ($width-$padding*2), $font, $bottom_text, $fit ) );
$from_side = ($width - $box_width)/2;
$from_top = $height - $padding;
// imagettftext( $im, $textsize, $angle, $from_side, $from_top, $textcolor, $font, $bottom_text );
memegen_imagettfstroketext( $im, $fontsize, $angle, $from_side, $from_top, $textcolor, $black, $font, $bottom_text, 1 );
$basename = basename( $args['memebase'], '.jpg' );
// output
header('Content-Type: image/jpeg');
header('Content-Disposition: filename="'. $basename .'-'. $filename .'.jpg"');
imagejpeg( $im );
imagedestroy( $im );
}
/**
* Font size guess
*
* Check if font box is too big for image and reduce recursively as needed till it does
*
* @param int $fontsize
* @param int $imwidth
* @param string $font TTF
* @param string $text
* @param bool $fit Try and fit text to image
* @return array Font size, font box width, font box height
*/
function memegen_font_size_guess( $fontsize, $imwidth, $font, $text, $fit ) {
$angle = 0;
$_box = imageftbbox( $fontsize, $angle, $font, $text );
$box_width = $_box[4] - $_box[6];
$box_height = $_box[3] - $_box[5];
if ( $box_width > $imwidth && $fit ) {
// $sub = round( ( $box_width - $imwidth) * .08, 0, PHP_ROUND_HALF_DOWN );
// if ( $sub < 1 ) $sub = 1;
$sub = 1;
$fontsize = $fontsize - $sub;
return memegen_font_size_guess( $fontsize, $imwidth, $font, $text, $fit );
}
return compact( 'fontsize', 'box_width', 'box_height' );
}
/**
* Writes the given text with a border into the image using TrueType fonts.
* http://www.johnciacia.com/2010/01/04/using-php-and-gd-to-add-border-to-text/
* @author John Ciacia
* @param image An image resource
* @param size The font size
* @param angle The angle in degrees to rotate the text
* @param x Upper left corner of the text
* @param y Lower left corner of the text
* @param textcolor This is the color of the main text
* @param strokecolor This is the color of the text border
* @param fontfile The path to the TrueType font you wish to use
* @param text The text string in UTF-8 encoding
* @param px Number of pixels the text border will be
* @see http://us.php.net/manual/en/function.imagettftext.php
*/
function memegen_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);
}
/**
* Sanitize
*
* Replace non-alphanumeric characters with hyphens
* Reduce any multihyphens down to one
*
* @param string $input
* @return string $input
*/
function memegen_sanitize( $input ) {
$input = preg_replace( '/[^a-zA-Z0-9-_]/', '-', $input );
$input = preg_replace( '/--*/', '-', $input );
return $input;
}