From 21d99e7919d5b3bffd8276e36d2d04955ce1e806 Mon Sep 17 00:00:00 2001 From: Ariel Gentile Date: Wed, 22 Apr 2026 12:03:45 -0300 Subject: [PATCH] feat: add configurable thumbnail quality option Introduces a new 'quality' option (default 90) for both Android and iOS. It controls the JPEG/PNG compression level passed to Bitmap.compress on Android and the UIImageJPEGRepresentation compressionQuality on iOS. --- README.md | 1 + .../java/com/createthumbnail/CreateThumbnailModule.java | 6 +++--- index.d.ts | 1 + ios/CreateThumbnail.m | 3 ++- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ee70b4f..33eb5e9 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ createThumbnail({ | format | `String` (default `jpeg`) | Thumbnail format, can be one of: `jpeg`, or `png` | | maxWidth | `Number` (default `512`) | Max thumbnail width in px | | maxHeight | `Number` (default `512`) | Max thumbnail height in px | +| quality | `Number` (default `90`) | Thumbnail quality | | dirSize | `Number` (default `100`) | Maximum size of the cache directory (in megabytes). When this directory is full, the previously generated thumbnails will be deleted to clear about half of it's size. | | headers | `Object` | Headers to load the video with. e.g. `{ Authorization: 'someAuthToken' }` | | cacheName | `String` (optional) | Cache name for this thumbnail to avoid duplicate generation. If specified, and a thumbnail already exists with the same cache name, it will be returned instead of generating a new one. | diff --git a/android/src/main/java/com/createthumbnail/CreateThumbnailModule.java b/android/src/main/java/com/createthumbnail/CreateThumbnailModule.java index ecc91d9..48ce4d2 100644 --- a/android/src/main/java/com/createthumbnail/CreateThumbnailModule.java +++ b/android/src/main/java/com/createthumbnail/CreateThumbnailModule.java @@ -91,6 +91,7 @@ private ReadableMap processData(ReadableMap options) throws IOException { int timeStamp = options.hasKey("timeStamp") ? options.getInt("timeStamp") : 0; int maxWidth = options.hasKey("maxWidth") ? options.getInt("maxWidth") : 512; int maxHeight = options.hasKey("maxHeight") ? options.getInt("maxHeight") : 512; + int quality = options.hasKey("quality") ? options.getInt("quality") : 90; boolean onlySyncedFrames = options.hasKey("onlySyncedFrames") ? options.getBoolean("onlySyncedFrames") : true; HashMap headers = options.hasKey("headers") ? options.getMap("headers").toHashMap() : new HashMap(); String fileName = TextUtils.isEmpty(cacheName) ? ("thumb-" + UUID.randomUUID().toString()) : cacheName + "." + format; @@ -101,11 +102,10 @@ private ReadableMap processData(ReadableMap options) throws IOException { file.createNewFile(); fOut = new FileOutputStream(file); - // 100 means no compression, the lower you go, the stronger the compression if (format.equals("png")) { - image.compress(Bitmap.CompressFormat.PNG, 100, fOut); + image.compress(Bitmap.CompressFormat.PNG, quality, fOut); } else { - image.compress(Bitmap.CompressFormat.JPEG, 90, fOut); + image.compress(Bitmap.CompressFormat.JPEG, quality, fOut); } fOut.flush(); diff --git a/index.d.ts b/index.d.ts index 3a61af8..58ed221 100644 --- a/index.d.ts +++ b/index.d.ts @@ -8,6 +8,7 @@ declare module "react-native-create-thumbnail" { cacheName?: string; maxWidth?: number; maxHeight?: number; + quality?: number; /** * iOS Only */ diff --git a/ios/CreateThumbnail.m b/ios/CreateThumbnail.m index 4c87cfa..b8f18cf 100644 --- a/ios/CreateThumbnail.m +++ b/ios/CreateThumbnail.m @@ -14,6 +14,7 @@ @implementation CreateThumbnail NSDictionary *headers = config[@"headers"] ?: @{}; CGFloat maxWidth = [config[@"maxWidth"] floatValue] ?: 512; CGFloat maxHeight = [config[@"maxHeight"] floatValue] ?: 512; + CGFloat quality = [config[@"quality"] floatValue] ?: 90; int timeToleranceMs = [config[@"timeToleranceMs"] intValue] ?: 2000; unsigned long long cacheDirSize = dirSize * 1024 * 1024; @@ -61,7 +62,7 @@ @implementation CreateThumbnail if ([format isEqual: @"png"]) { data = UIImagePNGRepresentation(thumbnail); } else { - data = UIImageJPEGRepresentation(thumbnail, 1.0); + data = UIImageJPEGRepresentation(thumbnail, quality / 100); } NSFileManager *fileManager = [NSFileManager defaultManager];