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
|
67
fusion.php
67
fusion.php
|
@ -1,19 +1,33 @@
|
||||||
<?php
|
<?php
|
||||||
$debug = FALSE;
|
function makeFusion($getFrom, $getTo, $fromCli)
|
||||||
|
{
|
||||||
|
$debug = FALSE;
|
||||||
|
|
||||||
include("ponies.php");
|
include("ponies.php");
|
||||||
|
|
||||||
$getFrom = $_GET["from"];
|
if(!isset($ponies[$getFrom]) || !isset($ponies[$getTo]))
|
||||||
$getTo = $_GET["to"];
|
|
||||||
|
|
||||||
if(!isset($ponies[$getFrom]) || !isset($ponies[$getTo]))
|
|
||||||
die("Invalid combination");
|
die("Invalid combination");
|
||||||
|
|
||||||
$from = imagecreatefrompng("./ponies/" . $ponies[$getFrom]["filename"]);
|
$cacheFilename = "./cache/" . $getFrom . "_" . $getTo . ".png";
|
||||||
$to = imagecreatefrompng("./ponies/" . $ponies[$getTo]["filename"]);
|
$cacheUrl = "/cache/" . $getFrom . "_" . $getTo . ".png";
|
||||||
|
$useCache = TRUE;
|
||||||
|
|
||||||
for($i = 0; $i < imagecolorstotal($from); $i++)
|
// 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"]);
|
||||||
|
$to = imagecreatefrompng("./ponies/" . $ponies[$getTo]["filename"]);
|
||||||
|
|
||||||
|
for($i = 0; $i < imagecolorstotal($from); $i++)
|
||||||
|
{
|
||||||
if($i < imagecolorstotal($to) && $i <= 16)
|
if($i < imagecolorstotal($to) && $i <= 16)
|
||||||
{
|
{
|
||||||
$colorFrom = imagecolorsforindex($to, $i);
|
$colorFrom = imagecolorsforindex($to, $i);
|
||||||
|
@ -35,21 +49,34 @@ for($i = 0; $i < imagecolorstotal($from); $i++)
|
||||||
imagecolorset($from, $i, $colorFrom["red"], $colorFrom["green"], $colorFrom["blue"], $colorFrom["alpha"]);
|
imagecolorset($from, $i, $colorFrom["red"], $colorFrom["green"], $colorFrom["blue"], $colorFrom["alpha"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retain transparency
|
// Retain transparency
|
||||||
imagealphablending($from, false);
|
imagealphablending($from, false);
|
||||||
imagesavealpha($from, true);
|
imagesavealpha($from, true);
|
||||||
imagefill($from, 0, 0, imagecolorallocatealpha($from, 255, 255, 255, 127));
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
imagedestroy($to);
|
|
||||||
?>
|
?>
|
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