Skip to content

Commit

Permalink
Fixes generation for already supported image formats (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bungeefan committed Nov 27, 2022
1 parent c44509f commit 2f6e090
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 27 deletions.
38 changes: 36 additions & 2 deletions lib/cli_commands.dart
Expand Up @@ -284,9 +284,20 @@ String? _checkImageExists({
exit(1);
}

if (p.extension(image).toLowerCase() != '.png') {
// https://github.com/brendan-duncan/image#supported-image-formats
final List<String> supportedFormats = [
"png", "apng", // PNG
"jpg", "jpeg", "jpe", "jfif", // JPEG
"tga", "tpic", // TGA
"gif", // GIF
"ico", // ICO
"bmp", "dib", // BMP
];

if (!supportedFormats
.any((format) => p.extension(image).toLowerCase() == ".$format")) {
print(
'Unsupported file format: $image Your image must be a PNG file.',
'Unsupported file format: $image Your image must be in one of the following formats: $supportedFormats',
);
exit(1);
}
Expand All @@ -295,6 +306,29 @@ String? _checkImageExists({
return image == '' ? null : image;
}

void createBackgroundImage({
required String imageDestination,
required String imageSource,
}) {
// Copy will not work if the directory does not exist, so createSync
// will ensure that the directory exists.
File(imageDestination).createSync(recursive: true);

// If source image is not already png, convert it, otherwise just copy it.
if (p.extension(imageSource).toLowerCase() != '.png') {
final image = decodeImage(File(imageSource).readAsBytesSync());
if (image == null) {
print('$imageSource could not be read');
exit(1);
}
File(imageDestination)
..createSync(recursive: true)
..writeAsBytesSync(encodePng(image));
} else {
File(imageSource).copySync(imageDestination);
}
}

/// Get config from `pubspec.yaml` or `flutter_native_splash.yaml`
Map<String, dynamic> getConfig({
required String? configFile,
Expand Down
16 changes: 8 additions & 8 deletions lib/ios.dart
Expand Up @@ -433,10 +433,10 @@ void _createBackground({
file.createSync(recursive: true);
file.writeAsBytesSync(encodePng(background));
} else if (backgroundImageSource != null) {
// Copy will not work if the directory does not exist, so createSync
// will ensure that the directory exists.
File(backgroundImageDestination).createSync(recursive: true);
File(backgroundImageSource).copySync(backgroundImageDestination);
createBackgroundImage(
imageDestination: backgroundImageDestination,
imageSource: backgroundImageSource,
);
} else {
throw Exception('No color string or background image!');
}
Expand All @@ -453,10 +453,10 @@ void _createBackground({
file.createSync(recursive: true);
file.writeAsBytesSync(encodePng(background));
} else if (darkBackgroundImageSource != null) {
// Copy will not work if the directory does not exist, so createSync
// will ensure that the directory exists.
File(darkBackgroundImageDestination).createSync(recursive: true);
File(darkBackgroundImageSource).copySync(darkBackgroundImageDestination);
createBackgroundImage(
imageDestination: darkBackgroundImageDestination,
imageSource: darkBackgroundImageSource,
);
} else {
final file = File(darkBackgroundImageDestination);
if (file.existsSync()) file.deleteSync();
Expand Down
37 changes: 20 additions & 17 deletions lib/web.dart
Expand Up @@ -102,27 +102,30 @@ void createBackgroundImages({
required String? backgroundImage,
required String? darkBackgroundImage,
}) {
const backgroundDestination = '${_webSplashImagesFolder}light-background.png';
print('[Web] Creating background images');
_createBackgroundImage(
backgroundImage: backgroundImage,
fileName: "light-background.png",
);
_createBackgroundImage(
backgroundImage: darkBackgroundImage,
fileName: "dark-background.png",
);
}

void _createBackgroundImage({
required String? backgroundImage,
required String fileName,
}) {
final backgroundDestination = '$_webSplashImagesFolder$fileName';
if (backgroundImage == null) {
final file = File(backgroundDestination);
if (file.existsSync()) file.deleteSync();
} else {
// Copy will not work if the directory does not exist, so createSync
// will ensure that the directory exists.
File(backgroundDestination).createSync(recursive: true);
File(backgroundImage).copySync(backgroundDestination);
}

const darkBackgroundDestination =
'${_webSplashImagesFolder}dark-background.png';
if (darkBackgroundImage == null) {
final file = File(darkBackgroundDestination);
if (file.existsSync()) file.deleteSync();
} else {
// Copy will not work if the directory does not exist, so createSync
// will ensure that the directory exists.
File(darkBackgroundDestination).createSync(recursive: true);
File(darkBackgroundImage).copySync(darkBackgroundDestination);
createBackgroundImage(
imageDestination: backgroundDestination,
imageSource: backgroundImage,
);
}
}

Expand Down

0 comments on commit 2f6e090

Please sign in to comment.