Make all images content aware
Signed-off-by: trivernis <trivernis@protonmail.com>pull/4/head
parent
79bcf7c528
commit
d3c94bc1ec
@ -0,0 +1,6 @@
|
|||||||
|
<div #imageContainer class="image-container">
|
||||||
|
<img #image [src]="this.imageSrc" alt="" (load)="this.adjustSize(image, imageContainer)"
|
||||||
|
(window:resize)="this.adjustSize(image, imageContainer)"
|
||||||
|
[class.scale-width]="scaleWidth" [class.scale-height]="!scaleWidth"
|
||||||
|
decoding="async">
|
||||||
|
</div>
|
@ -0,0 +1,19 @@
|
|||||||
|
.image-container {
|
||||||
|
height: 100%;
|
||||||
|
width: 100%;
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
|
|
||||||
|
.image-container > img {
|
||||||
|
margin: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
img.scale-height {
|
||||||
|
height: 100%;
|
||||||
|
width: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
img.scale-width {
|
||||||
|
width: 100%;
|
||||||
|
height: auto;
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
|
import { ContentAwareImageComponent } from './content-aware-image.component';
|
||||||
|
|
||||||
|
describe('ContentAwareImageComponent', () => {
|
||||||
|
let component: ContentAwareImageComponent;
|
||||||
|
let fixture: ComponentFixture<ContentAwareImageComponent>;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
await TestBed.configureTestingModule({
|
||||||
|
declarations: [ ContentAwareImageComponent ]
|
||||||
|
})
|
||||||
|
.compileComponents();
|
||||||
|
});
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
fixture = TestBed.createComponent(ContentAwareImageComponent);
|
||||||
|
component = fixture.componentInstance;
|
||||||
|
fixture.detectChanges();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should create', () => {
|
||||||
|
expect(component).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
@ -0,0 +1,31 @@
|
|||||||
|
import {Component, Input, OnInit} from '@angular/core';
|
||||||
|
import {SafeResourceUrl} from "@angular/platform-browser";
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: 'app-content-aware-image',
|
||||||
|
templateUrl: './content-aware-image.component.html',
|
||||||
|
styleUrls: ['./content-aware-image.component.scss']
|
||||||
|
})
|
||||||
|
export class ContentAwareImageComponent {
|
||||||
|
|
||||||
|
@Input() imageSrc!: string | SafeResourceUrl;
|
||||||
|
|
||||||
|
scaleWidth = false;
|
||||||
|
|
||||||
|
constructor() { }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fits the image into the container
|
||||||
|
* @param {HTMLImageElement} image
|
||||||
|
* @param {HTMLDivElement} imageContainer
|
||||||
|
*/
|
||||||
|
public adjustSize(image: HTMLImageElement, imageContainer: HTMLDivElement): void {
|
||||||
|
const containerHeight = Math.abs(imageContainer.clientHeight);
|
||||||
|
const containerWidth = Math.abs(imageContainer.clientWidth);
|
||||||
|
console.log(containerHeight, ',', containerWidth);
|
||||||
|
const imageRelativeHeight = image.height / containerHeight;
|
||||||
|
const imageRelativeWidth = image.width / containerWidth;
|
||||||
|
console.log(imageRelativeHeight, ',', imageRelativeWidth);
|
||||||
|
this.scaleWidth = imageRelativeWidth > imageRelativeHeight;
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
<div class="image-wrapper" (click)="fileSelectEvent.emit(this.file)" [class.selected]="this.file.selected">
|
<div class="image-wrapper" (click)="fileSelectEvent.emit(this.file)" [class.selected]="this.file.selected">
|
||||||
<mat-progress-spinner *ngIf="!contentUrl"></mat-progress-spinner>
|
<mat-progress-spinner *ngIf="!contentUrl"></mat-progress-spinner>
|
||||||
<img [src]="contentUrl">
|
<app-content-aware-image *ngIf="contentUrl" [imageSrc]="contentUrl"></app-content-aware-image>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
Reference in New Issue