Skip to content

Instantly share code, notes, and snippets.

@Illvili
Last active August 29, 2015 14:08
Show Gist options
  • Save Illvili/613358e5b55155623517 to your computer and use it in GitHub Desktop.
Save Illvili/613358e5b55155623517 to your computer and use it in GitHub Desktop.
iOS App asset catalogs gen
<?php
if (!isset($argv[1])) {
echo '[!] Need image path', PHP_EOL;
echo $argv[0], ' [imagepath]', PHP_EOL;
exit(1);
}
$image = realpath($argv[1]);
if (!file_exists($image)) {
echo '[!] Image not exists', PHP_EOL;
}
$output_dir = dirname($image) . DIRECTORY_SEPARATOR . 'out';
$image_name = basename($image);
echo 'Making dir ', $output_dir, ' ...';
if (is_dir($output_dir)) {
echo 'Already Exists!', PHP_EOL;
} else {
mkdir($output_dir);
echo 'Done', PHP_EOL;
}
// iOS
echo 'iOS', PHP_EOL;
$image_dir = $output_dir . DIRECTORY_SEPARATOR . 'AppIcon.appiconset';
echo 'Making dir ', $image_dir, ' ...';
if (is_dir($image_dir)) {
echo 'Already Exists!', PHP_EOL;
} else {
mkdir($image_dir);
echo 'Done', PHP_EOL;
}
$image_dir .= DIRECTORY_SEPARATOR;
echo 'Copy Contents.json...';
copy('./appicon.json', $image_dir . 'Contents.json');
echo 'Done', PHP_EOL;
echo 'Resizing image...', PHP_EOL;
$image_data = imagecreatefrompng($image);
$appicon_settings = file_get_contents('./appicon.json');
$appicon_settings = str_replace("\n", '', $appicon_settings);
$sizes = json_decode($appicon_settings, true);
foreach ($sizes['images'] as $image_setting) {
echo ' ', join(' ', $image_setting), ' ';
$size = $image_setting['size'];
$scale = (int)$image_setting['scale'];
$filename = $image_setting['filename'];
list($width, $height) = explode('x', $size);
$width *= $scale;
$height *= $scale;
$new_file = imagecreatetruecolor($width, $height);
imagealphablending($new_file, false);
imagesavealpha($new_file, true);
$transparent = imagecolorallocatealpha($new_file, 255, 255, 255, 127);
imagefilledrectangle($new_file, 0, 0, $width, $height, $transparent);
imagecopyresampled($new_file, $image_data, 0, 0, 0, 0, $width, $height, 1024, 1024);
imagepng($new_file, $image_dir . $filename);
echo 'Done', PHP_EOL;
}
// Android
echo 'Android', PHP_EOL;
$image_dir = $output_dir . DIRECTORY_SEPARATOR . 'res';
echo 'Making dir ', $image_dir, ' ...';
if (is_dir($image_dir)) {
echo 'Already Exists!', PHP_EOL;
} else {
mkdir($image_dir);
echo 'Done', PHP_EOL;
}
$image_dir .= DIRECTORY_SEPARATOR;
$android_image_sizes = array(
72 => 'drawable-hdpi',
48 => 'drawable-mdpi',
96 => 'drawable-xhdpi',
144 => 'drawable-xxhdpi'
);
foreach ($android_image_sizes as $size => $folder_name) {
echo ' ', $size, ' ', $folder_name, ' ';
$folder_path = $image_dir . $folder_name;
if (!is_dir($folder_path)) mkdir($folder_path);
$new_file = imagecreatetruecolor($size, $size);
imagealphablending($new_file, false);
imagesavealpha($new_file, true);
$transparent = imagecolorallocatealpha($new_file, 255, 255, 255, 127);
imagefilledrectangle($new_file, 0, 0, $size, $size, $transparent);
imagecopyresampled($new_file, $image_data, 0, 0, 0, 0, $size, $size, 1024, 1024);
imagepng($new_file, $folder_path . DIRECTORY_SEPARATOR . 'ic_launcher.png');
echo 'Done', PHP_EOL;
}
echo 'All Done', PHP_EOL;
exit(0);
{
"images" : [
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "1x",
"filename" : "Icon-29.png"
},
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "2x",
"filename" : "[email protected]"
},
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "3x",
"filename" : "[email protected]"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "2x",
"filename" : "[email protected]"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "3x",
"filename" : "[email protected]"
},
{
"idiom" : "iphone",
"size" : "57x57",
"scale" : "1x",
"filename" : "Icon-57.png"
},
{
"idiom" : "iphone",
"size" : "57x57",
"scale" : "2x",
"filename" : "[email protected]"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "2x",
"filename" : "[email protected]"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "3x",
"filename" : "[email protected]"
},
{
"idiom" : "ipad",
"size" : "29x29",
"scale" : "1x",
"filename" : "Icon-29.png"
},
{
"idiom" : "ipad",
"size" : "29x29",
"scale" : "2x",
"filename" : "[email protected]"
},
{
"idiom" : "ipad",
"size" : "40x40",
"scale" : "1x",
"filename" : "Icon-40.png"
},
{
"idiom" : "ipad",
"size" : "40x40",
"scale" : "2x",
"filename" : "[email protected]"
},
{
"idiom" : "ipad",
"size" : "50x50",
"scale" : "1x",
"filename" : "Icon-50.png"
},
{
"idiom" : "ipad",
"size" : "50x50",
"scale" : "2x",
"filename" : "[email protected]"
},
{
"idiom" : "ipad",
"size" : "72x72",
"scale" : "1x",
"filename" : "Icon-72.png"
},
{
"idiom" : "ipad",
"size" : "72x72",
"scale" : "2x",
"filename" : "[email protected]"
},
{
"idiom" : "ipad",
"size" : "76x76",
"scale" : "1x",
"filename" : "Icon-76.png"
},
{
"idiom" : "ipad",
"size" : "76x76",
"scale" : "2x",
"filename" : "[email protected]"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
},
"properties" : {
"pre-rendered" : true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment