Skip to content

Commit

Permalink
Use output buffering so we can determine the Content-Length header.
Browse files Browse the repository at this point in the history
  • Loading branch information
kingkool68 committed Sep 14, 2016
1 parent 04f7190 commit 4812e99
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions code.php
Expand Up @@ -162,12 +162,14 @@ function imagettfbbox_t( $size, $text_angle, $fontfile, $text ){
$bg_color = imageColorAllocate( $img, $background->get_rgb('r'), $background->get_rgb('g'), $background->get_rgb('b') );
$fg_color = imageColorAllocate( $img, $foreground->get_rgb('r'), $foreground->get_rgb('g'), $foreground->get_rgb('b') );

if ( ! $_GET['text'] ) {
preg_match( '/&text=(.+)/i', $_GET["x"], $matches );
$_GET['text'] = urldecode( $matches[1] );
if ( empty( $_GET['text'] ) || ! isset( $_GET['text'] ) ) {
preg_match( '/&text=(.+)/i', $_GET['x'], $matches );
if ( isset( $matches[1] ) ) {
$_GET['text'] = urldecode( $matches[1] );
}
}

if ( $_GET['text'] ) {
if ( isset( $_GET['text'] ) && $_GET['text'] ) {
$_GET['text'] = preg_replace_callback(
"/(0x[0-9A-F]{,3})/ui",
function( $matches ) {
Expand Down Expand Up @@ -203,25 +205,42 @@ function( $matches ) {
//Create and positions the text
imagettftext( $img, $fontsize, $text_angle, $textX, $textY, $fg_color, $font, $text );

// Caching Headers
$offset = 60 * 60 * 24 * 90; //90 Days
header( 'Cache-control: max-age=' . $offset );
// Set a far future expire date. This keeps the image locally cached by the user for less hits to the server
header( 'Expires: ' . gmdate( DATE_RFC1123, time() + $offset ) );
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', time() ) . ' GMT' );
// Set the header so the browser can interpret it as an image and not a bunch of weird text
header( 'Content-type: image/' . $file_format);

function process_output_buffer( $buffer = '' ) {
$buffer = trim( $buffer );
if( strlen( $buffer ) == 0 ) {
return '';
}
return $buffer;
}
// Start output buffering so we can determine the Content-Length of the file
ob_start( 'process_output_buffer' );

// Create the final image based on the provided file format.
switch ( $file_format ) {
case 'gif':
imagegif( $img );
break;
case 'png':
imagepng( $img );
imagepng( $img );
break;
case 'jpg':
case 'jpeg':
imagejpeg( $img );
break;
}
$output = ob_get_contents();

ob_end_clean();

// Caching Headers
$offset = 60 * 60 * 24 * 90; //90 Days
header( 'Cache-Control: public, max-age=' . $offset );
// Set a far future expire date. This keeps the image locally cached by the user for less hits to the server
header( 'Expires: ' . gmdate( DATE_RFC1123, time() + $offset ) );
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', time() ) . ' GMT' );
// Set the header so the browser can interpret it as an image and not a bunch of weird text
header( 'Content-type: image/' . $file_format );
header( 'Content-Length: ' . strlen( $output ) );

echo $output;

0 comments on commit 4812e99

Please sign in to comment.