Skip to content

Instantly share code, notes, and snippets.

@Da-Fecto
Last active December 23, 2015 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Da-Fecto/6553387 to your computer and use it in GitHub Desktop.
Save Da-Fecto/6553387 to your computer and use it in GitHub Desktop.
This is a PW template. Pages using this template are actual images. The advantage of using this approach is a query-able image where you can store meta data & do some GD modifications. Don't forget to enable template caching with this template. Pages using this template should not end with a slash. Pages using this template should end with jpg g…
<?php
// text to print on Image
$text = $page->title;
$image = $page->image->first()->httpUrl;
$info = getimagesize($image);
$type = $info['mime'];
switch ($type) {
case "image/jpeg":
$gd_img = imagecreatefromjpeg($image); //jpeg file
break;
case "image/gif":
$gd_img = imagecreatefromgif($image); //gif file
break;
case "image/png":
$gd_img = imagecreatefrompng($image); //png file
break;
default:
die('Not a valid Image');
break;
}
// Allocate A Color For The Text
$white = imagecolorallocate($gd_img, 255, 255, 255);
// path to font
$font = $config->paths->templates . "fonts/Arial.ttf";
// height of the font
$font_size = 25;
// left
$x = 0;
// top
$y = $font_size + 0;
// (array) Print Text On Image, third value is angle
imagettftext($gd_img, $font_size, 0, $x, $y, $white, $font, $text);
/**
* Output Page Image
*
*/
// collect infos
$maxAge = (60 * 60 * 24 * 2); // 48 hours
$imgTimestamp = filemtime($imgFilename);
$imgExpiration = intval(time() + $maxAge);
// create headers
$imgHeaders = array();
$imgHeaders[] = 'Content-type: ' . $type; // or other type
//$imgHeaders[] = 'Content-Length: ' . filesize($image);
$imgHeaders[] = 'Date: ' . gmdate('D, d M Y H:i:s',time()) . ' GMT';
$imgHeaders[] = 'Last-Modified: ' . gmdate('D, d M Y H:i:s',$imgTimestamp) . ' GMT';
$imgHeaders[] = 'Expires: ' . gmdate('D, d M Y H:i:s', $imgExpiration) . ' GMT';
$imgHeaders[] = 'pragma: cache';
$imgHeaders[] = "Cache-Control: max-age={$maxAge}";
$imgHeaders[] = "Cache-Control: no-transform, public, s-maxage={$maxAge}, max-age={$maxAge}"; // public|private
// send header
foreach($imgHeaders as $imgHeader) header($imgHeader);
switch ($type) {
case "image/jpeg":
imagejpeg($gd_img);
break;
case "image/gif":
imagegif($gd_img);
break;
case "image/png":
imagepng($gd_img);
break;
default:
die('Not a valid Image');
break;
}
// Free up memory
imagedestroy($gd_img);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment