diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml new file mode 100644 index 0000000..d526652 --- /dev/null +++ b/.github/workflows/npm-publish.yml @@ -0,0 +1,13 @@ +name: Node.js Package +on: push +jobs: + publish: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: bahmutov/npm-install@v1 + - id: publish + uses: JS-DevTools/npm-publish@v1 + with: + token: ${{ secrets.npm_token }} + diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..62eb6f6 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,20 @@ +{ + "name": "@spoonconsulting/cordova-plugin-thumbnail", + "version": "1.0.3", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "@spoonconsulting/cordova-plugin-thumbnail", + "version": "1.0.3", + "license": "Apache 2.0", + "engines": { + "cordovaDependencies": { + "0.1.0": { + "cordova-plugin-file": "> 6.0.0" + } + } + } + } + } +} diff --git a/package.json b/package.json index 7279db8..2d5b9cd 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { - "name": "cordova-plugin-thumbnail", - "version": "1.0.0", + "name": "@spoonconsulting/cordova-plugin-thumbnail", + "version": "1.0.3", "description": "Image thumbnail generator for Cordova project", "cordova": { - "id": "cordova-plugin-thumbnail", + "id": "@spoonconsulting/cordova-plugin-thumbnail", "platforms": [ "ios", "android" @@ -11,7 +11,7 @@ }, "repository": { "type": "git", - "url": "git+https://github.com/almas/cordova-plugin-thumbnail.git" + "url": "git+https://github.com/spoonconsulting/cordova-plugin-thumbnail.git" }, "keywords": [ "thumbnail", @@ -19,12 +19,12 @@ "cordova-android", "cordova-ios" ], - "author": "Almas", + "author": "Almas & Spoonconsulting", "license": "Apache 2.0", "bugs": { - "url": "https://github.com/almas/cordova-plugin-thumbnail/issues" + "url": "https://github.com/spoonconsulting/cordova-plugin-thumbnail/issues" }, - "homepage": "https://github.com/almas/cordova-plugin-thumbnail#readme", + "homepage": "https://github.com/spoonconsulting/cordova-plugin-thumbnail#readme", "engines": { "cordovaDependencies": { "0.1.0": { diff --git a/plugin.xml b/plugin.xml index b66a9ba..f637e5c 100755 --- a/plugin.xml +++ b/plugin.xml @@ -1,5 +1,5 @@ - + Thumbnail Cordova Thumbnail Plugin Apache 2.0 @@ -12,6 +12,7 @@ + diff --git a/src/android/Thumbnails.java b/src/android/Thumbnails.java index 31739d0..cb65cc4 100755 --- a/src/android/Thumbnails.java +++ b/src/android/Thumbnails.java @@ -19,7 +19,7 @@ public class Thumbnails { - public static void thumbnail(Options thumbnailOptions) throws IOException { + public static void thumbnail(Options thumbnailOptions) throws Exception { long begin = System.currentTimeMillis(); Bitmap bitmap = thumbnailSmallImage(thumbnailOptions); @@ -37,12 +37,16 @@ public static void thumbnail(Options thumbnailOptions) throws IOException { bitmap = null; } - private static Bitmap thumbnailSmallImage(Options thumbnailOptions) throws IOException { + private static Bitmap thumbnailSmallImage(Options thumbnailOptions) throws Exception { BitmapFactory.Options options = calculateImageSize(thumbnailOptions.sourcePath); options.inJustDecodeBounds = false; Bitmap bitmap = BitmapFactory.decodeFile(thumbnailOptions.sourcePath, options); + if (bitmap == null) { + throw new Exception("Could not decode file into bitmap object { sourcePath: " + thumbnailOptions.sourcePath + " options: }"); + } + long begin = System.currentTimeMillis(); int oWidth = bitmap.getWidth(); int oHeight = bitmap.getHeight(); @@ -123,9 +127,6 @@ public static Boolean saveBitmapToFile(Bitmap bitmap, String targetPath, Options bitmap.compress(guessImageType(targetPath), 90, os); } catch (FileNotFoundException ex) { throw new TargetPathNotFoundException(ex); - } catch (IOException ex) { - Log.e("Thumbnails.saveBitmapToFile()", "Error opening file stream:" + targetPath); - ex.printStackTrace(); } finally { if (os != null) { try { diff --git a/src/ios/Thumbnail.m b/src/ios/Thumbnail.m index 9f76ca0..5e396f1 100755 --- a/src/ios/Thumbnail.m +++ b/src/ios/Thumbnail.m @@ -85,24 +85,35 @@ + (void) thumbnail:(NSString *)imageURL size:(CGFloat)maxSize toURL:(NSString *) + (UIImage *)thumbnailWithContentsOfURL:(NSURL *)URL maxPixelSize:(CGFloat)maxPixelSize { - CGImageSourceRef imageSource = CGImageSourceCreateWithURL((__bridge CFURLRef)URL, NULL); - if(imageSource == NULL) { - NSLog(@"Can not read from source: %@", URL); - return NULL; + NSData *data = [NSData dataWithContentsOfURL:URL]; + UIImage* image = [[UIImage alloc] initWithData:data]; + double thumbnailHeight; + double thumbnailWidth; + CGSize thumbnailSize; + double maxPointSize = maxPixelSize / (double) (image.scale); + + double ratio; + if ((image.size.width / image.size.height) > 1) { + ratio = MAX(image.size.width / maxPointSize, image.size.height / maxPointSize); + } else { + ratio = MIN(image.size.width / maxPointSize, image.size.height / maxPointSize); + } + + if (@available(iOS 15.0, *)) { + thumbnailHeight = (image.size.height / ratio) / image.scale; + thumbnailWidth = (image.size.width / ratio) / image.scale; + thumbnailSize = CGSizeMake(thumbnailWidth, thumbnailHeight); + return [image imageByPreparingThumbnailOfSize: thumbnailSize]; + } else { + thumbnailHeight = (image.size.height / ratio) / UIScreen.mainScreen.scale; + thumbnailWidth = (image.size.width / ratio) / UIScreen.mainScreen.scale; + thumbnailSize = CGSizeMake(thumbnailWidth, thumbnailHeight); + UIGraphicsBeginImageContextWithOptions(thumbnailSize, false, 0.0); + [image drawInRect:CGRectMake(0, 0, thumbnailSize.width, thumbnailSize.height)]; + UIImage *thumbnailImage = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + return thumbnailImage; } - - NSDictionary *imageOptions = @{ - (NSString const *)kCGImageSourceCreateThumbnailFromImageIfAbsent : (NSNumber const *)kCFBooleanTrue, - (NSString const *)kCGImageSourceThumbnailMaxPixelSize : @(maxPixelSize), - (NSString const *)kCGImageSourceCreateThumbnailWithTransform : (NSNumber const *)kCFBooleanTrue - }; - CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, (__bridge CFDictionaryRef)imageOptions); - CFRelease(imageSource); - - UIImage *result = [[UIImage alloc] initWithCGImage:thumbnail]; - CGImageRelease(thumbnail); - - return result; } @end