Skip to content

ImgExtractor is a library that extracts images from html (http links as well as data URIs), save in place you want and replaces sources in html.

License

Notifications You must be signed in to change notification settings

GreenEyedCat/ImgExtractor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NuGet License: MIT

Summary

ImgExtractor is a library that extracts images from html (http links as well as data URIs), save in place you want and replaces sources in html.

Usage

Initialization in startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    //...
    services.AddSingleton<LocalImageSaver>();          //Register implementation of IImageUploader in IServiceCollection
    services.AddHtmlImageExtractor<LocalImageSaver>()  //Add HtmlImageExtractor to IServiceCollection
        .UseRandomName();                              //You should call this if you want to generate random names for files.
    //...
}

Example of implementaion of IImageUploader:

//your implementation of IImageUploader, which saves files in place you want
public class LocalImageSaver : IImageUploader
{
    private readonly IHostEnvironment hostEnvironment;

    public LocalImageSaver(IHostEnvironment hostEnvironment)
    {
        this.hostEnvironment = hostEnvironment;
    }

    //name is a file name
    //data[] is a file content
    //function should return new url of file
    public string UploadImage(string name, byte[] data)
    {
        string filePath = Path.Combine(hostEnvironment.ContentRootPath, "wwwroot", "files", name);
        File.WriteAllBytes(filePath, data);
        var url = "/files/" + name;
        return url;
    }

    public async Task<string> UploadImageAsync(string name, byte[] data)
    {
        string filePath = Path.Combine(hostEnvironment.ContentRootPath, "wwwroot", "files", name);
        await File.WriteAllBytesAsync(filePath, data);
        var url = "/files/" + name;
        return url;
    }
}

Usage in controller:

var extractionResult = imageExtractor.ExtractImagesFromHtml(html); //parse html and replace <img> sources with links to reuploaded files
//You can use ExtractImagesFromHtmlAsync() in async methods
SomeVeryUsefulFunction(extractionResult.Mapping);                  //extractionResult.Mapping is a dictionary that contains original image urls as keys and new image urls as values
SaveHtml(extractionResult.Html);                                   //extractionResult.Html contains html with replaced <img> sources

About

ImgExtractor is a library that extracts images from html (http links as well as data URIs), save in place you want and replaces sources in html.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages