Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
ngx-image-cropper
Advanced tools
The ngx-image-cropper is an Angular library that provides a highly customizable image cropping tool. It allows users to crop images in various shapes and sizes, and offers a range of features to enhance the image cropping experience.
Basic Image Cropping
This feature allows users to crop images while maintaining a specific aspect ratio. The cropped image is emitted in the specified format (e.g., PNG).
<image-cropper [imageChangedEvent]="imageChangedEvent" [maintainAspectRatio]="true" [aspectRatio]="4 / 3" format="png" (imageCropped)="imageCropped($event)"></image-cropper>
Free Aspect Ratio Cropping
This feature allows users to crop images without maintaining a fixed aspect ratio, providing more flexibility in selecting the crop area.
<image-cropper [imageChangedEvent]="imageChangedEvent" [maintainAspectRatio]="false" format="jpeg" (imageCropped)="imageCropped($event)"></image-cropper>
Round Cropping
This feature allows users to crop images in a circular shape, which is useful for profile pictures and other round image requirements.
<image-cropper [imageChangedEvent]="imageChangedEvent" [maintainAspectRatio]="true" [aspectRatio]="1 / 1" [roundCropper]="true" format="png" (imageCropped)="imageCropped($event)"></image-cropper>
Image Rotation
This feature allows users to rotate the image before cropping, ensuring the image is properly oriented.
<image-cropper [imageChangedEvent]="imageChangedEvent" [maintainAspectRatio]="true" [aspectRatio]="4 / 3" format="png" (imageCropped)="imageCropped($event)" (imageLoaded)="imageLoaded()" (cropperReady)="cropperReady()" (loadImageFailed)="loadImageFailed()"></image-cropper>
Cropper.js is a JavaScript image cropping library that provides a rich set of features for cropping images. It is highly customizable and can be used with various frameworks, including Angular. Compared to ngx-image-cropper, Cropper.js offers more advanced features and greater flexibility but requires more setup and integration effort.
React Image Crop is a library specifically designed for React applications. It provides a simple and easy-to-use interface for cropping images. While it offers similar functionalities to ngx-image-cropper, it is tailored for React rather than Angular, making it a better choice for React developers.
Vue Cropper is a Vue.js component for image cropping. It offers a range of features similar to ngx-image-cropper but is designed for use with Vue.js. It provides a seamless integration with Vue applications and is a good alternative for developers working with the Vue framework.
npm install ngx-image-cropper --save
Add the ImageCropperModule to the imports of the module which will be using the Image Cropper.
import { NgModule } from '@angular/core';
import { ImageCropperModule } from 'ngx-image-cropper';
@NgModule({
imports: [
...
ImageCropperModule
],
declarations: [
...
],
exports: [
...
],
providers: [
...
]
})
export class YourModule {
}
Add the element to your HTML:
<input type="file" (change)="fileChangeEvent($event)" />
<image-cropper
[imageChangedEvent]="imageChangedEvent"
[maintainAspectRatio]="true"
[aspectRatio]="4 / 3"
[resizeToWidth]="128"
format="png"
(imageCropped)="imageCropped($event)"
(imageLoaded)="imageLoaded()"
(cropperReady)="cropperReady()"
(loadImageFailed)="loadImageFailed()"
></image-cropper>
<img [src]="croppedImage" />
And add this to your ts file:
import { ImageCroppedEvent } from 'ngx-image-cropper';
export class YourComponent {
imageChangedEvent: any = '';
croppedImage: any = '';
fileChangeEvent(event: any): void {
this.imageChangedEvent = event;
}
imageCropped(event: ImageCroppedEvent) {
this.croppedImage = event.base64;
}
imageLoaded() {
// show cropper
}
cropperReady() {
// cropper ready
}
loadImageFailed() {
// show message
}
}
When you choose a file from the file input, it will trigger fileChangeEvent
.
That event is then passed to the image cropper through imageChangedEvent
which will load the image into the cropper.
Everytime you release the mouse, the imageCropped
event will be triggerd with the cropped image as a Base64 string in its payload.
All inputs are optional. Either the imageChangedEvent
, imageBase64
or imageFile
should be set to load an image into the cropper.
Name | Type | Default | Description |
---|---|---|---|
imageChangedEvent | FileEvent | The change event from your file input (set to null to reset the cropper) | |
imageFile | Blob(File) | The file you want to change (set to null to reset the cropper) | |
imageBase64 | string | If you don't want to use a file input, you can set a base64 image directly and it will be loaded into the cropper | |
format | string | png | Output format (png, jpeg, webp, bmp, ico) (not all browsers support all types, png is always supported, others are optional) |
outputType | string | base64 | Output type ('base64', 'file' or 'both'). Converting the image to a Blob can be quite a heavy operation. With this option, you could choose to only get the base64 which will improve the speed of cropping significantly |
aspectRatio | number | 1 / 1 | The width / height ratio (e.g. 1 / 1 for a square, 4 / 3, 16 / 9 ...) |
maintainAspectRatio | boolean | true | Keep width and height of cropped image equal according to the aspectRatio |
containWithinAspectRatio | boolean | false | When set to true, padding will be added around the image to make it fit to the aspect ratio |
resizeToWidth | number | 0 (disabled) | Cropped image will be resized to this width (in px) |
resizeToHeight | number | 0 (disabled) | Cropped image will be resized to this height (in px) (will be ignored if resizeToWidth is set) |
cropperMinWidth | number | 0 (disabled) | The cropper cannot be made smaller than this number of pixels in width (relative to original image's size) (in px) |
cropperMinHeight | number | 0 (disabled) | The cropper cannot be made smaller than this number of pixels in height (relative to original image's size) (in px) (will be ignored if maintainAspectRatio is set) |
onlyScaleDown | boolean | false | When the resizeToWidth is set, enabling this option will make sure smaller images are not scaled up |
cropper | CropperPosition | To be able to overwrite the cropper coordinates, you can use this input. Create a new object of type CropperPosition and assign it to this input. Make sure to create a new object each time you wish to overwrite the cropper's position and wait for the cropperReady event to have fired. | |
roundCropper | boolean | false | Set this to true for a round cropper. Resulting image will still be square, use border-radius: 100% on resulting image to show it as round. |
imageQuality | number | 92 | This only applies when using jpeg or webp as output format. Entering a number between 0 and 100 will determine the quality of the output image. |
autoCrop | boolean | true | When set to true, the cropper will emit an image each time the position or size of the cropper is changed. When set to false, you can call the crop method yourself (use @ViewChild to get access to the croppers methods). |
alignImage | 'left' or 'center' | 'center' | Use this to align the image in the cropper either to the left or center. |
backgroundColor | string | Use this to set a backgroundColor, this is useful if you upload an image of a format with transparent colors and convert it to 'jpeg' or 'bmp'. The transparent pixels will then become the set color or the default value. Enter a color HashCode or one of known HTML color names (https://www.w3schools.com/tags/ref_colornames.asp). |
Name | Type | Description |
---|---|---|
imageCropped | ImageCroppedEvent | Emits an ImageCroppedEvent each time the image is cropped |
imageLoaded | void | Emits when the image was loaded into the cropper |
cropperReady | void | Emits when the cropper is ready to be interacted |
startCropImage | void | Emits when the component started cropping the image |
loadImageFailed | void | Emits when a wrong file type was selected (only png, gif and jpg are allowed) |
To gain access to the image cropper's methods use @ViewChild(ImageCropperComponent) imageCropper: ImageCropperComponent;
Name | Returns | Description |
---|---|---|
rotateLeft | void | Rotates the image to the left |
rotateRight | void | Rotates the image to the right |
flipHorizontal | void | Flips the image horizontally |
flipVertical | void | Flips the image vertically |
crop | ImageCroppedEvent (when outputType is base64 ) or Promise<ImageCroppedEvent> (when outputType is file or both )) | Crops the source image to the current cropper position. Accepts an output type as an argument, default is the one given in the outputType input (base64 , file or both ). Be sure to set autoCrop to false if you only wish to use this function directly. |
Property | Type | Description |
---|---|---|
x1 | number | X position of first coordinate (in px) |
y1 | number | Y position of first coordinate (in px) |
x2 | number | X position of second coordinate (in px) |
y2 | number | Y position of second coordinate (in px) |
Property | Type | Description |
---|---|---|
rotation | number | 0=No Rotation, 1=90°, 2=180°, 3=270° |
flipH | boolean | Flipped Horizontally |
flipV | boolean | Flipped Vertically |
Property | Type | Description |
---|---|---|
base64 | string | Base64 string of the cropped image |
file | Blob(File) | Blob of the cropped image |
width | number | Width of the cropped image |
height | number | Height of the cropped image |
cropperPosition | CropperPosition | Position of the cropper when it was cropped relative to the displayed image size |
imagePosition | CropperPosition | Position of the cropper when it was cropped relative to the original image size |
offsetImagePosition | CropperPosition | Position of the cropper when it was cropped relative to the original image size without padding when containWithinAspectRatio is true |
transform | Transformations | Rotation and Flip State |
If you wish to use the file output, you'll need to polyfill the toBlob
method of the HTML Canvas for IE and Edge.
if (!HTMLCanvasElement.prototype.toBlob) {
Object.defineProperty(HTMLCanvasElement.prototype, 'toBlob', {
value: function (callback, type, quality) {
var dataURL = this.toDataURL(type, quality).split(',')[1];
setTimeout(function() {
var binStr = atob( dataURL ),
len = binStr.length,
arr = new Uint8Array(len);
for (var i = 0; i < len; i++ ) {
arr[i] = binStr.charCodeAt(i);
}
callback( new Blob( [arr], {type: type || 'image/png'} ) );
});
}
});
}
FAQs
An image cropper for Angular
The npm package ngx-image-cropper receives a total of 125,044 weekly downloads. As such, ngx-image-cropper popularity was classified as popular.
We found that ngx-image-cropper demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.