Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/* A small preview for the newest HTZ comic.
Params:
@ size: an int to define the width x height sizes from an array
We have three different sizes:
1.- 700px width (2x2 panels comic)
2.- 1050px width (3-panels-in-a-row comic)
3.- Another size
*/
define("WEBPATH", "./");
define("COMICSPATH", WEBPATH . "comics/");
define("CACHEPATH", WEBPATH . "cache/");
define("DEFAULTWIDTHS", serialize(array("125", "200", "300")));
define("DEFAULTHEIGHTS", serialize(array("125", "200", "300")));
define("DEFAULTSIZEMODE", 3);
define("DEFAULTEXTENSION", "png");
define("SQUARECOMICWIDTH", 700);
define("SQUARECOMICHEIGHT", 688);
define("THREEINAROWCOMICWIDTH", 1050);
define("THREEINAROWCOMICHEIGHT", 356);
// -----------------------------------------------------
/*
Get the last comic filename and extension
Params:
Return:
@response
filename (ex: 2017-01-01-100)
extension (ex: png)
*/
function get_last_comic() {
$response = new stdClass();
$files = scandir(COMICSPATH, 1); // 1 instead of SCANDIR_SORT_DESCENDING (for old php versions)
if (!empty($files)) {
foreach($files as $file) { // loop 'til find the first file with extension
$file_path_info = pathinfo($file);
if (!empty($file_path_info["filename"]) && !empty($file_path_info["extension"])) { //not a dir or "error_log" file (no extension)
// Try to get the date from the filename, if comic date is greater than today, do not pick it (it's a future comic, wait 'til the day)
$matches = array();
preg_match("/(\d*)\-(\d*)\-(\d*).*/i", $file_path_info["filename"], $matches); // Comic filename format: "YYYY-MM-DD-xxxxx.extension"
if (!empty($matches)) {
if ((strtotime($matches[2] . "/" . $matches[3] . "/" . $matches[1]) - time()) <= 0) {
$response->filename = $file_path_info["filename"];
$response->extension = $file_path_info["extension"];
}
}
}
if (!empty($response->filename)) break; // Stop at first comic
}
}
return $response;
}
/*
Cut the $image according to some custom criteria
Params:
@image: The full path of an image
Return:
@destination_image
*/
function set_last_comic_image($image, $size) {
//Get the original image size
$original_image_size = getimagesize($image); // 0 for width, 1 for height
$original_width = (round($original_image_size[0] / 10) * 10); // round +- 10
$original_height = $original_image_size[1]; // do not round 'cause it's a min value
// This values are setted trying in several "standard" comics,
// final image may appear moved in comics with custom headers or footers
if ($original_width == SQUARECOMICWIDTH && $original_height >= SQUARECOMICHEIGHT) {
$source_width = 340;
$source_height = 340;
$origin_x = 5;
$origin_y = $original_image_size[1] - ($source_height * 2) - 7;
} else if ($original_width == THREEINAROWCOMICWIDTH && $original_height >= THREEINAROWCOMICHEIGHT) {
$source_width = 350;
$source_height = 350;
$origin_x = 2;
$origin_y = 2;
} else {
// If we have a "rogue comic", just get a piece of it,
// nevermind the position
$source_width = 350;
$source_height = 350;
$origin_x = rand(5, $original_width - $source_width - 5);
$origin_y = rand(5, $original_height - $source_height - 5);
}
//Get the size
$destination_width = unserialize(DEFAULTWIDTHS);
$destination_width = $destination_width[$size];
$destination_height = unserialize(DEFAULTHEIGHTS);
$destination_height = $destination_height[$size];
$current_image = imagecreatefromstring(file_get_contents($image));
$destination_image = imagecreatetruecolor($destination_width, $destination_height); //New image size = "cut size"
imagecopyresampled($destination_image, $current_image, 0, 0, $origin_x, $origin_y, $destination_width, $destination_height, $source_width, $source_height);
return $destination_image;
}
// -----------------------------------------------------
header('Content-Type: image/png');
//Get the last comic object from the path
$last_comic = get_last_comic();
if (!empty($last_comic->filename)) {
// Get the size, if proceed
$size = (!empty($_GET["size"]) && is_numeric($_GET["size"]) && $_GET["size"] >= 1 && $_GET["size"] <= 3 ? floor($_GET["size"]) : DEFAULTSIZEMODE) - 1; //-1 'cause it's an array index!
// Also append the size to the cached file (different files for different sizes)
// Get the cache filename
$cache_filename = CACHEPATH . $size . "_" . $last_comic->filename . "." . DEFAULTEXTENSION;
// If file is already cached, serve it
if (file_exists($cache_filename)) {
fpassthru(fopen($cache_filename, 'rb'));
} else {
// Get the comic filename
$comic_filename = COMICSPATH . $last_comic->filename . "." . $last_comic->extension;
$last_comic_img = set_last_comic_image($comic_filename, $size);
// Save and show the image
imagepng($last_comic_img, $cache_filename, 9);
imagepng($last_comic_img);
imagedestroy($last_comic_img);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment