Skip to content

Instantly share code, notes, and snippets.

@mr-fan
Forked from Da-Fecto/pageimage.php
Created August 6, 2014 15:50
Show Gist options
  • Save mr-fan/7d77ae80833c017e76c4 to your computer and use it in GitHub Desktop.
Save mr-fan/7d77ae80833c017e76c4 to your computer and use it in GitHub Desktop.
<?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