Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
ngx-picture
Advanced tools
An Angular library to properly size and lazy load images in next generation formats
An Angular library to help properly size, lazy load images, and use next generation formats.
Help improve app performance and fix common Lighthouse opportunities:
Ready made configurations available for:
For live demos:
Angular 9+
npm i --save ngx-picture@latest
Angular < 9
npm i --save ngx-picture@2.0.4
Import NgxPictureModule into app.module.ts and call forRoot suppyling the config.
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import {
DEFAULT_BREAKPOINTS,
ImageFormat,
NgxPictureModule
} from 'ngx-picture';
import { AppComponent } from './app.component';
// 1: supply a function to create the srcset urls for each breakpoint
export function srcInterpolator(
url: string,
imageFormat: ImageFormat,
breakpoint: string,
breakpointValue: number
) {
return `${url.split('.')[0]}-${breakpointValue}.${
imageFormat === 'jpeg' ? 'jpg' : 'webp'
}`;
}
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
,
NgxPictureModule.forRoot({
breakpoints: DEFAULT_BREAKPOINTS, //2. the break points to create sources for
imageFormats: ['webp', 'jpeg'], //3. the image formats to create sources for. *
srcInterpolator
})
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
* Image formats must be in order of precedence. In this example if webp is supported it will be used.
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { NgxPictureModule, CLOUDINARY_CONFIG } from 'ngx-picture';
import { AppComponent } from './app.component';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, NgxPictureModule.forRoot(CLOUDINARY_CONFIG)],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
<ngx-picture
src="assets/images/banner.jpg"
alt="test"
[lazyLoad]="true"
></ngx-picture>
If lazyLoad is true the component will use an IntersectionObserver (if it is supported by the browser) to only render the picture element if the component is in view.
*Remember to import the NgxPictureModule into the relevant module.
NgxPictureConfig is generic so you can change the brreakpoint values to anything required in the srcInterPolator function. This example is using the Angular CDK breakpoints for the breakpoint keys.
import { Breakpoints } from '@angular/cdk/layout';
export interface Dimensions {
h: number;
w: number;
}
const ngxPictureConfig: NgxPictureConfig<Dimensions> = {
breakpoints: {
[Breakpoints.XSmall]: { h: 10, w: 10 },
[Breakpoints.Medium]: { h: 100, w: 100 },
[Breakpoints.Large]: { h: 200, w: 200 }
},
imageFormats: ['webp', 'jpg'],
srcInterpolator: (
url: string,
imageFormat: ImageFormat,
breakpoint: string,
breakpointValue: Dimensions
) => `${url}/w:${breakpointValue.w}/h:${breakpointValue.h}`
};
export function srcInterpolator(
url: string,
imageFormat: ImageFormat,
breakpoint: string,
breakpointValue: number
) {
return `${url.split('.')[0]}-${breakpointValue}.${
imageFormat === 'jpeg' ? 'jpg' : 'webp'
}`;
}
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule.withServerTransition({ appId: 'serverApp' }),
BrowserAnimationsModule,
MatButtonModule,
MatCardModule,
MatListModule,
NgxPictureModule.forRoot(ngxPictureConfig)
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
To use a custom img element provide an ngTemplate called #imgTemplate.
<ngx-picture
src="assets/images/banner.jpg"
alt="test"
[lazyLoad]="true"
#picture
>
<ng-template #imgTemplate let-imageData>
<img class="custom-template" [src]="imageData.src" [alt]="imageData.alt" />
</ng-template>
</ngx-picture>
The data context for the template is:
{
src: string,
alt: string
}
<picture class="ngx-picture__picture">
<source
srcset="assets/images/banner-300.webp"
media="(max-width: 599.99px)"
type="image/webp"
/>
<source
srcset="assets/images/banner-600.webp"
media="(min-width: 600px) and (max-width: 959.99px)"
type="image/webp"
/>
<source
srcset="assets/images/banner-960.webp"
media="(min-width: 960px) and (max-width: 1279.99px)"
type="image/webp"
/>
<source
srcset="assets/images/banner-1280.webp"
media="(min-width: 1280px) and (max-width: 1919.99px)"
type="image/webp"
/>
<source
srcset="assets/images/banner-1920.webp"
media="(min-width: 1920px)"
type="image/webp"
/>
<source
srcset="assets/images/banner-300.jpg"
media="(max-width: 599.99px)"
type="image/jpeg"
/>
<source
srcset="assets/images/banner-600.jpg"
media="(min-width: 600px) and (max-width: 959.99px)"
type="image/jpeg"
/>
<source
srcset="assets/images/banner-960.jpg"
media="(min-width: 960px) and (max-width: 1279.99px)"
type="image/jpeg"
/>
<source
srcset="assets/images/banner-1280.jpg"
media="(min-width: 1280px) and (max-width: 1919.99px)"
type="image/jpeg"
/>
<source
srcset="assets/images/banner-1920.jpg"
media="(min-width: 1920px)"
type="image/jpeg"
/>
<img
class="ngx-picture__picture__img"
src="assets/images/banner.jpg"
alt="test"
loading="lazy"
/>
</picture>
The picture element in the component has the class ngx-picture__picture and the img element has the class ngx-picturepictureimg.
.your-picture-class {
.ngx-picture__picture {
width: 100%;
.ngx-picture__picture__img {
width: 100%;
}
}
}
To clone this repo and run it locally.
git clone https://github.com/JayChase/ngx-picture.git
cd ngx-picture
npm i
npm run build
ng s
npm run storybook
FAQs
An Angular library to properly size, lazy load images, and use next generation formats
The npm package ngx-picture receives a total of 113 weekly downloads. As such, ngx-picture popularity was classified as not popular.
We found that ngx-picture demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.