mirror of
https://github.com/Tailszefox/Pony-Fusion.git
synced 2024-11-27 07:57:58 +01:00
Added cache
This will decrease the amount of CPU and RAM needed when generating fusions, but will use more disk space since we store each fusion as a PNG. Check the changes in this commit for more information on how caching works.
This commit is contained in:
parent
d1b05b73f6
commit
3e638ff216
3 changed files with 112 additions and 40 deletions
3
cache/.gitignore
vendored
Normal file
3
cache/.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
# This directory is used to cache fusion as PNG files
|
||||||
|
# This way, they are only generated once, saving CPU and RAM on the server, at the expense of disk space
|
||||||
|
# Remember to delete the corresponding fusions in this directory if you make any change to an image in the /ponies directory, otherwise the changes won't be reflected
|
35
fusion.php
35
fusion.php
|
@ -1,14 +1,28 @@
|
||||||
<?php
|
<?php
|
||||||
|
function makeFusion($getFrom, $getTo, $fromCli)
|
||||||
|
{
|
||||||
$debug = FALSE;
|
$debug = FALSE;
|
||||||
|
|
||||||
include("ponies.php");
|
include("ponies.php");
|
||||||
|
|
||||||
$getFrom = $_GET["from"];
|
|
||||||
$getTo = $_GET["to"];
|
|
||||||
|
|
||||||
if(!isset($ponies[$getFrom]) || !isset($ponies[$getTo]))
|
if(!isset($ponies[$getFrom]) || !isset($ponies[$getTo]))
|
||||||
die("Invalid combination");
|
die("Invalid combination");
|
||||||
|
|
||||||
|
$cacheFilename = "./cache/" . $getFrom . "_" . $getTo . ".png";
|
||||||
|
$cacheUrl = "/cache/" . $getFrom . "_" . $getTo . ".png";
|
||||||
|
$useCache = TRUE;
|
||||||
|
|
||||||
|
// If this fusion was already cached, just redirect to it.
|
||||||
|
if($useCache && file_exists($cacheFilename))
|
||||||
|
{
|
||||||
|
if($fromCli === FALSE)
|
||||||
|
{
|
||||||
|
header("Location: " . $cacheUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$from = imagecreatefrompng("./ponies/" . $ponies[$getFrom]["filename"]);
|
$from = imagecreatefrompng("./ponies/" . $ponies[$getFrom]["filename"]);
|
||||||
$to = imagecreatefrompng("./ponies/" . $ponies[$getTo]["filename"]);
|
$to = imagecreatefrompng("./ponies/" . $ponies[$getTo]["filename"]);
|
||||||
|
|
||||||
|
@ -45,11 +59,24 @@ imagefill($from, 0, 0, imagecolorallocatealpha($from, 255, 255, 255, 127));
|
||||||
// Crop 1px from bottom to avoid showing swatch
|
// Crop 1px from bottom to avoid showing swatch
|
||||||
$from = imagecrop($from, ['x' => 0, 'y' => 0, 'width' => imagesx($from), 'height' => imagesy($from) - 1]);
|
$from = imagecrop($from, ['x' => 0, 'y' => 0, 'width' => imagesx($from), 'height' => imagesy($from) - 1]);
|
||||||
|
|
||||||
if(!$debug)
|
if(!$debug && !$fromCli)
|
||||||
{
|
{
|
||||||
header('Content-Type: image/png');
|
header('Content-Type: image/png');
|
||||||
imagepng($from);
|
imagepng($from);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Save fusion to cache
|
||||||
|
imagepng($from, $cacheFilename);
|
||||||
|
|
||||||
imagedestroy($to);
|
imagedestroy($to);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Don't run the function if we're running from the command line
|
||||||
|
if (php_sapi_name() !== 'cli')
|
||||||
|
{
|
||||||
|
$getFrom = $_GET["from"];
|
||||||
|
$getTo = $_GET["to"];
|
||||||
|
makeFusion($getFrom, $getTo, FALSE);
|
||||||
|
}
|
||||||
|
|
||||||
?>
|
?>
|
42
generate_all.php
Normal file
42
generate_all.php
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
<?php
|
||||||
|
// Use this script to generate a cache of all the fusions. For security reasons this can only be done through the command line.
|
||||||
|
// If you want to regenerate a fusion already in the cache, delete it from the cache first. This script will not generate a fusion that's already cached.
|
||||||
|
// There is a small pause between each fusion in order to not overload the server. Depending on the number of ponies, this may take a while to run.
|
||||||
|
|
||||||
|
if(php_sapi_name() !== 'cli')
|
||||||
|
{
|
||||||
|
exit('You cannot run this script in your browser.');
|
||||||
|
}
|
||||||
|
|
||||||
|
require("ponies.php");
|
||||||
|
require("fusion.php");
|
||||||
|
|
||||||
|
$totalPonies = 0;
|
||||||
|
|
||||||
|
foreach($ponies as $id => $properties)
|
||||||
|
{
|
||||||
|
if(strpos($id, "break") === FALSE)
|
||||||
|
$totalPonies++;
|
||||||
|
}
|
||||||
|
|
||||||
|
$totalFusions = $totalPonies * $totalPonies;
|
||||||
|
|
||||||
|
$i = 0;
|
||||||
|
foreach($ponies as $id => $properties)
|
||||||
|
{
|
||||||
|
if(strpos($id, "break") === FALSE)
|
||||||
|
{
|
||||||
|
foreach($ponies as $idSecond => $propertiesSecond)
|
||||||
|
{
|
||||||
|
if(strpos($idSecond, "break") === FALSE)
|
||||||
|
{
|
||||||
|
$i++;
|
||||||
|
$percentage = sprintf("%3d", floor(($i / $totalFusions) * 100));
|
||||||
|
echo "($percentage%) [$i/$totalFusions] $id + $idSecond\n";
|
||||||
|
makeFusion($id, $idSecond, TRUE);
|
||||||
|
sleep(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
Loading…
Reference in a new issue