Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base64 image cannot access in html when set image URL #462

Open
damithmalalanayake opened this issue May 29, 2020 · 7 comments
Open

Base64 image cannot access in html when set image URL #462

damithmalalanayake opened this issue May 29, 2020 · 7 comments

Comments

@damithmalalanayake
Copy link

I have a problem accessing the loaded base4 image in the template when I set URL to [lazyload] attribute. Is there any way of doing that?

@tjoskar
Copy link
Owner

tjoskar commented May 29, 2020

That should be possible.
Do you have an example of a base64 image? Or how you are using it?

@damithmalalanayake
Copy link
Author

damithmalalanayake commented May 29, 2020

Base64 is loading perfectly when I set my image URL(which returns base64 string by my backend service) to [lazyload] property. I want to get that loaded base64 string to use in my other service. Is it possible?

@tjoskar
Copy link
Owner

tjoskar commented May 29, 2020

I guess so, here is an example of using a base 64 image in a component: https://codesandbox.io/s/lazy-load-image-with-base-64-image-gml2t?file=/src/app/app.component.ts

It should just work ;)

@damithmalalanayake
Copy link
Author

Nope. This is not what I'm saying. This is really working well. I need to get the base 64 string which returns to tag.
Below example, it returns base64 after executing this URL "https://abcd.com/img/1".
And the image is loaded. I want to access that returned base64 string. Because I want to download the already loaded image without calling backend. I think now you'll get my problem.

<img [lazyLoad]="https://abcd.com/img/1"
defaultImage="assets/img/default_img.gif">

@tjoskar
Copy link
Owner

tjoskar commented May 29, 2020

So in your case https://abcd.com/img/1 will return a base64 image? In that case I think you need to create your own function to download the image.
Will all your images be downloaded in the same way? In that case, should the following code work:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { LazyLoadImageModule, IntersectionObserverHooks, Attributes } from 'ng-lazyload-image';
import { AppComponent } from './app.component';

class LazyLoadImageHooks extends IntersectionObserverHooks {
  loadImage({ imagePath }: Attributes) {
    return fetch(imagePath).then(res => res.text());
  }
}

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, LazyLoadImageModule.forRoot(LazyLoadImageHooks)],
  bootstrap: [AppComponent],
})
export class MyAppModule {}

@damithmalalanayake
Copy link
Author

Nope. I want to access the base64 in HTML.

@tjoskar
Copy link
Owner

tjoskar commented Feb 7, 2021

Do you mean something like this:

@Component({
  selector: 'image',
  template: `<image [lazyLoad]="base64image | async">`,
})
class ImageComponent {
  base64image = fetch('https://abcd.com/img/1').then(r => r.text());
}

Otherwise, I need a small project where you can reproduce the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants