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:
Tailszefox 2023-03-29 21:31:29 +02:00
parent d1b05b73f6
commit 3e638ff216
3 changed files with 112 additions and 40 deletions

3
cache/.gitignore vendored Normal file
View 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

View file

@ -1,55 +1,82 @@
<?php <?php
$debug = FALSE; function makeFusion($getFrom, $getTo, $fromCli)
include("ponies.php");
$getFrom = $_GET["from"];
$getTo = $_GET["to"];
if(!isset($ponies[$getFrom]) || !isset($ponies[$getTo]))
die("Invalid combination");
$from = imagecreatefrompng("./ponies/" . $ponies[$getFrom]["filename"]);
$to = imagecreatefrompng("./ponies/" . $ponies[$getTo]["filename"]);
for($i = 0; $i < imagecolorstotal($from); $i++)
{ {
if($i < imagecolorstotal($to) && $i <= 16) $debug = FALSE;
include("ponies.php");
if(!isset($ponies[$getFrom]) || !isset($ponies[$getTo]))
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))
{ {
$colorFrom = imagecolorsforindex($to, $i); if($fromCli === FALSE)
if($debug)
{ {
$colorTo = imagecolorsforindex($from, $i); header("Location: " . $cacheUrl);
echo "$i\n";
print_r($colorFrom);
print_r($colorTo);
echo "\n";
} }
if($getFrom == $getTo) return;
}
$from = imagecreatefrompng("./ponies/" . $ponies[$getFrom]["filename"]);
$to = imagecreatefrompng("./ponies/" . $ponies[$getTo]["filename"]);
for($i = 0; $i < imagecolorstotal($from); $i++)
{
if($i < imagecolorstotal($to) && $i <= 16)
{ {
imagecolorset($from, $i, 255 - $colorFrom["red"], 255 - $colorFrom["green"], 255 - $colorFrom["blue"], $colorFrom["alpha"]); $colorFrom = imagecolorsforindex($to, $i);
} if($debug)
else {
{ $colorTo = imagecolorsforindex($from, $i);
imagecolorset($from, $i, $colorFrom["red"], $colorFrom["green"], $colorFrom["blue"], $colorFrom["alpha"]); echo "$i\n";
print_r($colorFrom);
print_r($colorTo);
echo "\n";
}
if($getFrom == $getTo)
{
imagecolorset($from, $i, 255 - $colorFrom["red"], 255 - $colorFrom["green"], 255 - $colorFrom["blue"], $colorFrom["alpha"]);
}
else
{
imagecolorset($from, $i, $colorFrom["red"], $colorFrom["green"], $colorFrom["blue"], $colorFrom["alpha"]);
}
} }
} }
// Retain transparency
imagealphablending($from, false);
imagesavealpha($from, true);
imagefill($from, 0, 0, imagecolorallocatealpha($from, 255, 255, 255, 127));
// Crop 1px from bottom to avoid showing swatch
$from = imagecrop($from, ['x' => 0, 'y' => 0, 'width' => imagesx($from), 'height' => imagesy($from) - 1]);
if(!$debug && !$fromCli)
{
header('Content-Type: image/png');
imagepng($from);
}
// Save fusion to cache
imagepng($from, $cacheFilename);
imagedestroy($to);
} }
// Retain transparency // Don't run the function if we're running from the command line
imagealphablending($from, false); if (php_sapi_name() !== 'cli')
imagesavealpha($from, true);
imagefill($from, 0, 0, imagecolorallocatealpha($from, 255, 255, 255, 127));
// Crop 1px from bottom to avoid showing swatch
$from = imagecrop($from, ['x' => 0, 'y' => 0, 'width' => imagesx($from), 'height' => imagesy($from) - 1]);
if(!$debug)
{ {
header('Content-Type: image/png'); $getFrom = $_GET["from"];
imagepng($from); $getTo = $_GET["to"];
makeFusion($getFrom, $getTo, FALSE);
} }
imagedestroy($to);
?> ?>

42
generate_all.php Normal file
View 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);
}
}
}
}
?>