From 3e638ff2162d5e4e0adf1b4598e5c6e72ce52d40 Mon Sep 17 00:00:00 2001 From: Tailszefox Date: Wed, 29 Mar 2023 21:31:29 +0200 Subject: [PATCH] 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. --- cache/.gitignore | 3 ++ fusion.php | 107 +++++++++++++++++++++++++++++------------------ generate_all.php | 42 +++++++++++++++++++ 3 files changed, 112 insertions(+), 40 deletions(-) create mode 100644 cache/.gitignore create mode 100644 generate_all.php diff --git a/cache/.gitignore b/cache/.gitignore new file mode 100644 index 0000000..00f63ad --- /dev/null +++ b/cache/.gitignore @@ -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 diff --git a/fusion.php b/fusion.php index f205c1b..540b14e 100644 --- a/fusion.php +++ b/fusion.php @@ -1,55 +1,82 @@ 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 -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) +// Don't run the function if we're running from the command line +if (php_sapi_name() !== 'cli') { - header('Content-Type: image/png'); - imagepng($from); + $getFrom = $_GET["from"]; + $getTo = $_GET["to"]; + makeFusion($getFrom, $getTo, FALSE); } -imagedestroy($to); ?> \ No newline at end of file diff --git a/generate_all.php b/generate_all.php new file mode 100644 index 0000000..b77e3df --- /dev/null +++ b/generate_all.php @@ -0,0 +1,42 @@ + $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); + } + } + } +} +?> \ No newline at end of file