Skip to content

Commit

Permalink
Animated item images: fix problem with transparency on some GIFs, add…
Browse files Browse the repository at this point in the history
… APNG format
  • Loading branch information
gesior committed Dec 27, 2022
1 parent cee601d commit 83487c7
Show file tree
Hide file tree
Showing 9 changed files with 936 additions and 528 deletions.
26 changes: 13 additions & 13 deletions tools/item-image-frames-to-animated-gif-converter/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
## PNG to GIF converter

In this folder are PHP files to generate animated item images from item images exported by `itemImageFramesGenerator.html`.

Put `items.zip` in this folder and run by command line:
```
php cli_convert.php
```

In `index.php` is version for hosting online PNG to GIF converter.

Official host is:
https://item-images.ots.me/png-to-gif-converter/
## PNG to APNG/GIF converter

In this folder are PHP files to generate animated item images from item images exported by `itemImageFramesGenerator.html`.

Put `items.zip` in this folder and run by command line:
```
php cli_convert.php APNG items.zip
```

In `index.php` is version for hosting online PNG to GIF converter.

Official host is:
https://item-images.ots.me/png-to-gif-converter/
68 changes: 41 additions & 27 deletions tools/item-image-frames-to-animated-gif-converter/cli_convert.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
<?php

include_once(__DIR__ . '/lib/converterPngToGifAnimation.php');

if (!isset($argv[1])) {
echo 'Missing path to items.zip file.';
echo 'CLI usage example: php cli_convert.php items.zip';
exit;
}
$zipFilePath = $argv[1];

$startTime = time();

echo 'Started converter - it may take few minutes to generate GIFs!' . PHP_EOL;
$converter = new ConverterPngToGifAnimation($zipFilePath, true);
$gifImagesZipArchivePath = './generated-gif-zip-archives/items_' . microtime(true) . '.zip';
try {
$converter->convert($gifImagesZipArchivePath);
} catch (Exception $exception) {
exit('Exception occured during GIF generation: ' . $exception->getMessage());
}

echo 'Peak memory allocated: ' . round(memory_get_peak_usage(false) / 1024 / 1024, 2) . ' MB' . PHP_EOL;
echo 'Peak memory usage (real): ' . round(memory_get_peak_usage(true) / 1024 / 1024, 2) . ' MB' . PHP_EOL;
echo 'Execution time: ' . (time() - $startTime) . ' seconds' . PHP_EOL;

echo 'GIF IMAGES AND ANIMATIONS SAVED TO: ' . $gifImagesZipArchivePath;
<?php

include_once(__DIR__ . '/lib/converterPngToApngAnimation.php');
include_once(__DIR__ . '/lib/converterPngToGifAnimation.php');

if (!isset($argv[1]) || !in_array($argv[1], ['GIF', 'APNG'])) {
echo 'Missing animation format: GIF or APNG' . PHP_EOL;
echo 'CLI usage example: php cli_convert.php APNG items.zip' . PHP_EOL;
echo 'CLI usage example: php cli_convert.php GIF items.zip' . PHP_EOL;
exit;
}
if (!isset($argv[2])) {
echo 'Missing path to items.zip file.' . PHP_EOL;
echo 'CLI usage example: php cli_convert.php APNG items.zip' . PHP_EOL;
echo 'CLI usage example: php cli_convert.php GIF items.zip' . PHP_EOL;
exit;
}

$exportFormat = $argv[1];
$zipFilePath = $argv[2];

$startTime = time();

echo 'Started converter - it may take few minutes to generate APNGs/GIFs!' . PHP_EOL;
if ($exportFormat == 'APNG') {
$converter = new ConverterPngToApngAnimation($zipFilePath, true);
} else {
$converter = new ConverterPngToGifAnimation($zipFilePath, true);
}
$animatedImagesZipArchivePath = './generated-zip-archives/items_' . microtime(true) . '.zip';
try {
$converter->convert($animatedImagesZipArchivePath);
} catch (Exception $exception) {
exit('Exception occurred during APNG/GIF generation: ' . $exception->getMessage());
}

echo 'Peak memory allocated: ' . round(memory_get_peak_usage(false) / 1024 / 1024, 2) . ' MB' . PHP_EOL;
echo 'Peak memory usage (real): ' . round(memory_get_peak_usage(true) / 1024 / 1024, 2) . ' MB' . PHP_EOL;
echo 'Execution time: ' . (time() - $startTime) . ' seconds' . PHP_EOL;

echo 'APNG/GIF IMAGES AND ANIMATIONS SAVED TO: ' . $animatedImagesZipArchivePath;
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
*
!.gitignore
*
!.gitignore
!index.html
43 changes: 26 additions & 17 deletions tools/item-image-frames-to-animated-gif-converter/index.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

include_once(__DIR__ . '/lib/converterPngToApngAnimation.php');
include_once(__DIR__ . '/lib/converterPngToGifAnimation.php');

$generatedArchivesPath = './generated-gif-zip-archives/';
$generatedArchivesPath = './generated-zip-archives/';

// clean up downloads directory
$list = scandir($generatedArchivesPath);
Expand Down Expand Up @@ -31,45 +32,49 @@

$startTime = time();

echo 'Started converter - it may take few minutes to generate GIFs!<br />';
$converter = new ConverterPngToGifAnimation($zipFilePath, false);
$gifImagesZipArchivePath = $generatedArchivesPath . 'items_' . microtime(true) . '.zip';
echo 'Started converter - it may take few minutes to generate APNGs/GIFs!<br />';
if (isset($_POST['export_format']) && $_POST['export_format'] == 'APNG') {
$converter = new ConverterPngToApngAnimation($zipFilePath, true);
} else {
$converter = new ConverterPngToGifAnimation($zipFilePath, true);
}
$animatedImagesZipArchivePath = $generatedArchivesPath . 'items_' . microtime(true) . '.zip';
$framesInterval = 0.2;
if(isset($_POST['frames_interval']) && ((float) $_POST['frames_interval']) > 0.01) {
$framesInterval = (float) $_POST['frames_interval'];
if (isset($_POST['frames_interval']) && ((float)$_POST['frames_interval']) > 0.01) {
$framesInterval = (float)$_POST['frames_interval'];
echo 'Frames interval: ' . $framesInterval . '<br />';
}
try {
$converter->convert($gifImagesZipArchivePath, $framesInterval);
$converter->convert($animatedImagesZipArchivePath, $framesInterval);
} catch (Exception $exception) {
exit('Exception occurred during GIF generation: ' . $exception->getMessage());
exit('Exception occurred during APNG/GIF generation: ' . $exception->getMessage());
}

echo 'Peak memory allocated: ' . round(memory_get_peak_usage(false) / 1024 / 1024, 2) . ' MB<br />';
echo 'Peak memory usage (real): ' . round(memory_get_peak_usage(true) / 1024 / 1024, 2) . ' MB<br />';
echo 'Execution time: ' . (time() - $startTime) . ' seconds<br />';

echo 'GIF IMAGES AND ANIMATIONS SAVED TO: ' . $gifImagesZipArchivePath . '<br />';
echo '<h2>DOWNLOAD LINK: <a href="' . $gifImagesZipArchivePath . '">' . $gifImagesZipArchivePath . '</a></h2>';
echo 'APNG/GIF IMAGES AND ANIMATIONS SAVED TO: ' . $animatedImagesZipArchivePath . '<br />';
echo '<h2>DOWNLOAD LINK: <a href="' . $animatedImagesZipArchivePath . '">' . $animatedImagesZipArchivePath . '</a></h2>';
exit;
}
?>
<html lang="en">
<head>
<title>PNG to GIF converter</title>
<title>PNG to APNG/GIF converter</title>
</head>
<body>
<h2>PNG to GIF converter</h2>
<h4>Converts PNG images from Item Image Frames Generator into GIF animations.</h4>
<h4>Also works with not animated PNG images from Item Images Generator - just converts them to GIF, without
animations.</h4>
<h2>PNG to APNG/GIF converter</h2>
<h4>Converts PNG images from Item Image Frames Generator into APNG/GIF animations.</h4>
<h4>Also works with not animated PNG images from Item Images Generator - just converts them to APNG/GIF, without
animation.</h4>
<blockquote>
How to use:<br/><br/>
1. Generate items.zip using Item Images Generator:<br/>
<a href="https://item-images.ots.me/generator/">https://item-images.ots.me/generator/</a><br/>
<a href="https://item-images.ots.me/generator-animated-items/">https://item-images.ots.me/generator-animated-items/</a><br/>
2. Select generated file in upload form below.<br/>
3. Click "Convert to GIF"<br/>
3. Click "Convert to APNG/GIF"<br/>
4. Wait... it can take few minutes.<br/>
5. Click generated download link.<br/>
<br/>
Expand All @@ -82,7 +87,11 @@
<input type="file" name="items_zip"><br/>
Frames interval (0.2 = 5 frames per second):<br/>
<input type="text" name="frames_interval" value="0.2"><br/>
<input type="submit" value="Convert to GIF" name="submit">
<select name="export_format">
<option value="APNG">APNG - animated PNG</option>
<option>GIF</option>
</select><br/>
<input type="submit" value="Convert to APNG/GIF" name="submit">
</form>
</body>
</html>

0 comments on commit 83487c7

Please sign in to comment.