Skip to content

Commit 2f6e090

Browse files
authored
Fixes generation for already supported image formats (#460)
1 parent c44509f commit 2f6e090

File tree

3 files changed

+64
-27
lines changed

3 files changed

+64
-27
lines changed

lib/cli_commands.dart

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,20 @@ String? _checkImageExists({
284284
exit(1);
285285
}
286286

287-
if (p.extension(image).toLowerCase() != '.png') {
287+
// https://github.com/brendan-duncan/image#supported-image-formats
288+
final List<String> supportedFormats = [
289+
"png", "apng", // PNG
290+
"jpg", "jpeg", "jpe", "jfif", // JPEG
291+
"tga", "tpic", // TGA
292+
"gif", // GIF
293+
"ico", // ICO
294+
"bmp", "dib", // BMP
295+
];
296+
297+
if (!supportedFormats
298+
.any((format) => p.extension(image).toLowerCase() == ".$format")) {
288299
print(
289-
'Unsupported file format: $image Your image must be a PNG file.',
300+
'Unsupported file format: $image Your image must be in one of the following formats: $supportedFormats',
290301
);
291302
exit(1);
292303
}
@@ -295,6 +306,29 @@ String? _checkImageExists({
295306
return image == '' ? null : image;
296307
}
297308

309+
void createBackgroundImage({
310+
required String imageDestination,
311+
required String imageSource,
312+
}) {
313+
// Copy will not work if the directory does not exist, so createSync
314+
// will ensure that the directory exists.
315+
File(imageDestination).createSync(recursive: true);
316+
317+
// If source image is not already png, convert it, otherwise just copy it.
318+
if (p.extension(imageSource).toLowerCase() != '.png') {
319+
final image = decodeImage(File(imageSource).readAsBytesSync());
320+
if (image == null) {
321+
print('$imageSource could not be read');
322+
exit(1);
323+
}
324+
File(imageDestination)
325+
..createSync(recursive: true)
326+
..writeAsBytesSync(encodePng(image));
327+
} else {
328+
File(imageSource).copySync(imageDestination);
329+
}
330+
}
331+
298332
/// Get config from `pubspec.yaml` or `flutter_native_splash.yaml`
299333
Map<String, dynamic> getConfig({
300334
required String? configFile,

lib/ios.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,10 @@ void _createBackground({
433433
file.createSync(recursive: true);
434434
file.writeAsBytesSync(encodePng(background));
435435
} else if (backgroundImageSource != null) {
436-
// Copy will not work if the directory does not exist, so createSync
437-
// will ensure that the directory exists.
438-
File(backgroundImageDestination).createSync(recursive: true);
439-
File(backgroundImageSource).copySync(backgroundImageDestination);
436+
createBackgroundImage(
437+
imageDestination: backgroundImageDestination,
438+
imageSource: backgroundImageSource,
439+
);
440440
} else {
441441
throw Exception('No color string or background image!');
442442
}
@@ -453,10 +453,10 @@ void _createBackground({
453453
file.createSync(recursive: true);
454454
file.writeAsBytesSync(encodePng(background));
455455
} else if (darkBackgroundImageSource != null) {
456-
// Copy will not work if the directory does not exist, so createSync
457-
// will ensure that the directory exists.
458-
File(darkBackgroundImageDestination).createSync(recursive: true);
459-
File(darkBackgroundImageSource).copySync(darkBackgroundImageDestination);
456+
createBackgroundImage(
457+
imageDestination: darkBackgroundImageDestination,
458+
imageSource: darkBackgroundImageSource,
459+
);
460460
} else {
461461
final file = File(darkBackgroundImageDestination);
462462
if (file.existsSync()) file.deleteSync();

lib/web.dart

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -102,27 +102,30 @@ void createBackgroundImages({
102102
required String? backgroundImage,
103103
required String? darkBackgroundImage,
104104
}) {
105-
const backgroundDestination = '${_webSplashImagesFolder}light-background.png';
105+
print('[Web] Creating background images');
106+
_createBackgroundImage(
107+
backgroundImage: backgroundImage,
108+
fileName: "light-background.png",
109+
);
110+
_createBackgroundImage(
111+
backgroundImage: darkBackgroundImage,
112+
fileName: "dark-background.png",
113+
);
114+
}
115+
116+
void _createBackgroundImage({
117+
required String? backgroundImage,
118+
required String fileName,
119+
}) {
120+
final backgroundDestination = '$_webSplashImagesFolder$fileName';
106121
if (backgroundImage == null) {
107122
final file = File(backgroundDestination);
108123
if (file.existsSync()) file.deleteSync();
109124
} else {
110-
// Copy will not work if the directory does not exist, so createSync
111-
// will ensure that the directory exists.
112-
File(backgroundDestination).createSync(recursive: true);
113-
File(backgroundImage).copySync(backgroundDestination);
114-
}
115-
116-
const darkBackgroundDestination =
117-
'${_webSplashImagesFolder}dark-background.png';
118-
if (darkBackgroundImage == null) {
119-
final file = File(darkBackgroundDestination);
120-
if (file.existsSync()) file.deleteSync();
121-
} else {
122-
// Copy will not work if the directory does not exist, so createSync
123-
// will ensure that the directory exists.
124-
File(darkBackgroundDestination).createSync(recursive: true);
125-
File(darkBackgroundImage).copySync(darkBackgroundDestination);
125+
createBackgroundImage(
126+
imageDestination: backgroundDestination,
127+
imageSource: backgroundImage,
128+
);
126129
}
127130
}
128131

0 commit comments

Comments
 (0)