Lemsim, glad it’s of help!
Dacriburdan, do you mean an example scenario for the batch converter? In case that’s what you mean, let me explain.
General background: Normally I have a bunch of layered graphics files in very high resolution. This is the design image with all objects separate. These files might be called main.cpt, menu.cpt, stingy-bee.cpt etc., of which I make very frequent phase backups. Then when I need to prepare the graphics as PNG, I export them one by one and in the Lua file, note down the respective (work-resolution-based) width and height of the PNG. I have a small sprite creation class that helps me with some of the details, and I also have a spritesheet workaround (as I ran into problems with spritesheets & universal apps which use physics, so I basically don’t use spritesheets anymore).
As for the batch: On paper, I drew around 50 images, which are kind of like achievement bonus images. I scanned all them and cropped them, then saved them as high resolution PNGs. But now they’re all of different sizes, so I needed a way to have them all be in my preferred width and height (plus the @2x Retina/ iPad size). The following is the full converter code (it makes references to “glasses” because these images are images of glasses and mugs) – the converter also generates the necessary Lua data table. The goal width and height is 180*111 pixels:
[code]
require_once(‘misc.php5’);
header(‘Content-type: text/plain; charset=UTF-8’);
// error_reporting(E_ALL);
main();
function main() {
$factor = 2;
$maxWidth = 180 * $factor;
$maxHeight = 111 * $factor;
$path = ‘glass’;
$filepaths = Misc::getFilesInFolder($path);
foreach ($filepaths as $filepath) {
$filename = misc::getTextBetween($filepath, $path . ‘/’, ‘.png’);
$width = 0; $height = 0;
list($width, $height) = getImageSize($filepath);
$filepathNew = $filepath;
if ($factor == 2) { $filepathNew = str_iReplace(’.png’, ‘@2x.png’, $filepathNew); }
resizeImage($filepath, $filepathNew, $maxWidth, $maxHeight, $width, $height);
$title = substr( $filename, strLen(‘00-’) );
$title = str_iReplace(’-’, ’ ', $title);
$title = misc::getProperTitleCase($title);
misc::echoNow( " app.glasses[#app.glasses + 1] = { filename = ‘$filename’, title = ‘$title’ } \r" );
}
}
function resizeImage($filepath, $filepathNew, $maxWidth, $maxHeight, $originalWidth, $originalHeight) {
$ratio = ( (float)$maxWidth ) / $originalWidth;
$ratio2 = ( (float)$maxHeight ) / $originalHeight;
if ($ratio2 < $ratio) { $ratio = $ratio2; }
$newWidth = $originalWidth * $ratio;
$newHeight = $originalHeight * $ratio;
$resizedImage = imageCreateTrueColor($maxWidth, $maxHeight);
$white = imagecolorallocate($resizedImage, 255, 255, 255);
imageFilledRectangle($resizedImage, 0, 0, $maxWidth, $maxHeight, $white);
$source = imageCreateFromPng($filepath);
imageAlphaBlending($resizedImage, false);
imageSaveAlpha($resizedImage, true);
imageAlphaBlending($source, false);
imageSaveAlpha($source, true);
$destinationX = intVal( ($maxWidth - $newWidth) / 2 );
$destinationY = intVal( ($maxHeight - $newHeight) / 2 );
imageCopyResampled($resizedImage, $source, $destinationX, $destinationY, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
$compression = 9;
imagePng($resizedImage, $filepathNew, $compression);
imageDestroy($resizedImage);
imageDestroy($source);
}
?>[/code]
I also got a Misc tools class which I won’t fully paste here as it’s very long, but for instance above code uses the getFilesInFolder() function:
[code]
class Misc {
public static function getFilesInFolder($fullPath) {
if ( subStr( $fullPath, strLen($fullPath) - 1 ) != ‘/’ ) { $fullPath .= ‘/’; }
$dh = opendir($fullPath);
while ( false !== ($filename = readdir($dh)) ) {
if( !is_dir($fullPath . $filename) ) {
$files[] = $fullPath . $filename;
}
}
return $files;
}
}
?>[/code] [import]uid: 10284 topic_id: 12316 reply_id: 44943[/import]