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

Make helper methods to load images from any source and convert them into base64 string #7

Open
arivera12 opened this issue Aug 28, 2020 · 11 comments
Assignees
Labels
enhancement New feature or request

Comments

@arivera12
Copy link
Owner

I guess that answers my question.

Will it be a good idea to have some way of identifying if the string set was a file path and then convert to dataURL internally?

Originally posted by @sc231997 in #6 (comment)

@arivera12 arivera12 added the enhancement New feature or request label Aug 28, 2020
@arivera12
Copy link
Owner Author

arivera12 commented Aug 28, 2020

I think we could create some static helper methods to load images into the pdf document

Proposed API:

PdfMakeNet.LoadImageFromUrlAsync(HttpClient httpClient, string httpMethod, string requestUri);
PdfMakeNet.LoadImageFromPath(string path);

Take note that both methods should return a string and this string should be the base64 encoded image.

@arivera12
Copy link
Owner Author

I am grabbing this one.

@arivera12
Copy link
Owner Author

Static methods added

PdfMakeNet.LoadImageFromUrlAsync(HttpClient httpClient, string httpMethod, string requestUri, SourceImageFormat sourceImageFormat);
PdfMakeNet.LoadImageFromUrlAsync(HttpClient httpClient, string httpMethod, string requestUri, SourceImageFormat sourceImageFormat, CancellationToken cancellationToken);
PdfMakeNet.LoadSvgFromUrlAsync(HttpClient httpClient, string httpMethod, string requestUri);
PdfMakeNet.LoadImageFromPath(string path, SourceImageFormat sourceImageFormat);

@sc231997
Copy link
Collaborator

Sorry for the late reply. Can we chnage the methods to this

// I guess you are taking the httpclient from as argument from the user to deal the scenario when the image is behind the authentication.
// if possible can we have enum for httpMethod or some other class
PdfMakeNet.LoadImageFromUrlAsync(Uri imageUrl, SourceImageFormat sourceImageFormat,HttpClient httpClient = new HttpClient(), string httpMethod = "GET");
PdfMakeNet.LoadSvgFromUrlAsync(Uri imageUrl, HttpClient httpClient = new HttpClient(), string httpMethod = "GET");
// Path class has a way to detect the extension for the file so we might not need to take SourceImageFormat
PdfMakeNet.LoadImageFromPath(Path path);
PdfMakeNet.LoadImageFromPath(Path path, SourceImageFormat sourceImageFormat);

@arivera12
Copy link
Owner Author

arivera12 commented Aug 29, 2020

Sorry for the late reply. Can we chnage the methods to this

// I guess you are taking the httpclient from as argument from the user to deal the scenario when the image is behind the authentication.
// if possible can we have enum for httpMethod or some other class
PdfMakeNet.LoadImageFromUrlAsync(Uri imageUrl, SourceImageFormat sourceImageFormat,HttpClient httpClient = new HttpClient(), string httpMethod = "GET");
PdfMakeNet.LoadSvgFromUrlAsync(Uri imageUrl, HttpClient httpClient = new HttpClient(), string httpMethod = "GET");
// Path class has a way to detect the extension for the file so we might not need to take SourceImageFormat
PdfMakeNet.LoadImageFromPath(Path path);
PdfMakeNet.LoadImageFromPath(Path path, SourceImageFormat sourceImageFormat);

Makes sense for the http methods, but the others from loading from path I think is ok as it is implemented.

What we do if an image file is extension less and its still an image?

What we do in that situation?

The correct way of determine a file type and extension is reading the file signature but this takes extra operation with bytes reading and comparing to check if its really a file of a X type.

Your approach from reading the type from extension is ok, few implementations works like that but I prefer explicit image format instead of guessing and also support extension less images.

Does this makes sense to you?

Do you agree?

Reopening.

@arivera12 arivera12 reopened this Aug 29, 2020
@sc231997
Copy link
Collaborator

sc231997 commented Aug 30, 2020

What we do if an image file is extension less and its still an image?
What we do in that situation?

In that case, they can use this method

PdfMakeNet.LoadImageFromPath(Path path, SourceImageFormat sourceImageFormat);

The correct way of determine a file type and extension is reading the file signature but this takes extra operation with bytes reading and comparing to check if its really a file of a X type.

I do agree that there is some extra read operation but I feel that's worth it. For example, If there is a non-image(or any non-supported image format) file passed to the function we can return the exception saying file not supported.

We also need a method to read the svg file form file system.

@arivera12
Copy link
Owner Author

For reference of implementation
https://en.m.wikipedia.org/wiki/List_of_file_signatures

@arivera12
Copy link
Owner Author

arivera12 commented Aug 30, 2020

We also need a method to read the svg file form file system.

Loading svg would be as simple as reading it as text, no need for this.

System.IO.File.ReadAllText(svgFilePath)

Can svg be binary type?

@arivera12
Copy link
Owner Author

Looks promising maybe we can take the implementantion from here only for .jpeg and .png

https://github.com/neilharvey/FileSignatures

@sc231997
Copy link
Collaborator

We also need a method to read the svg file form file system.

Loading svg would be as simple as reading it as text, no need for this.

System.IO.File.ReadAllText(svgFilePath)

Can svg be binary type?

Not sure on that one.

@arivera12
Copy link
Owner Author

arivera12 commented Aug 31, 2020

I will do a little bit more of research over this and reply back here with the results and final implementations.

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

No branches or pull requests

2 participants