GithubHelp home page GithubHelp logo

viliusle / hermite-resize Goto Github PK

View Code? Open in Web Editor NEW
286.0 7.0 46.0 547 KB

Canvas image resize/resample using Hermite filter with JavaScript.

License: Other

JavaScript 61.15% HTML 38.85%
resample resize canvas

hermite-resize's People

Contributors

thenickdude avatar viliusle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hermite-resize's Issues

Is resample() expected to work on an element outside the DOM?

I am using Hermite-resize along with react-avatar-editor. After editing my image in react-avatar-editor, it returns the image to me as a free-standing HTMLCanvasElement (not in the DOM). Now when I try to resize the canvas using Hermite-resize, I am getting a blank image. Is this expected to work?

Here's the code that react-avatar-editor uses to give me a canvas (original code here):

getImage () {
    const canvas = document.createElement('canvas')
    const context = canvas.getContext('2d')

    ...

    context.drawImage(image.resource, -cropRect.x, -cropRect.y)
    return canvas
}

After getting this canvas, here's how I am passing it to Hermite-resize:

const HERMITE = new Hermite_class();
HERMITE.resample(canvas, 1024, 1024);

Finally when I save this canvas to a file. I get a blank image in the file. Just as a check, if I don't pass the canvas through Hermite-resize, I get the expected image in the file (without resizing).

Thanks in advance for your help.

Blank canvas with html2canvas

 html2canvas(document.getElementById('resfinal'), {
      allowTaint: true,
      useCORS: true,
    }).then(function (canvas) {
      HERMITE.resample(canvas, 1920, 1080)
      setReadyImage(canvas.toDataURL('image/jpeg', 0.9))
    })

By adding HERMITE is the returning Canvas blank ( without it , all the code works correctly)

IndexSizeError when resizing from Blob

I am attempting to resize images before uploading.

I realized that

HERMITE.resize_image();

expects an id, so I created an element and set img.src to be a Blob version of the inputted image.

To be clear, this creates an <img tag with src attributes that looks like this:

src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAASABIAAD......

But this throws the error on the resample method.

IndexSizeError: Index or size is negative or greater than the allowed amount

Hermite_class/this.resample hermite.npm.js:7:1103

I tried to resize from an actual element that I placed on the page and it worked fine, so this seems to be an issue with the Blob.

I verified that the image is actually displayed on the page as well.

This is a React app, so here's the code:

The HTML

      <form>
           <div>
                <input name="form[imgFile]" id="form_imgFile" type="file">
           </div>
           <div><ul></ul></div>
           <div>
                 <button type="submit" id="form_save" name="form[save]" disabled=""> Submit</button>
           </div>
      </form>

This checks the inputted file and assigns it to the state when the user selects the file.

handleFileSelected =  event => {
    this.setState({upload_errors: []});
    let file = event.target.files[0];
    let errors = [];
    if (!ACCEPTED_MIME_TYPES.includes(file.type)) {
        errors.push('Invalid file type. Only jpeg and png accepted');
    }
    if (file.size > UPLOAD_MAX_SIZE) {
        errors.push('File size is bigger than allowed( 2MB )');
    }
    this.setState({upload_errors: errors});
    if (errors.length === 0) {
        document.getElementById("form_save").disabled = false;
        this.setState({selectedFile: file})
    }
}

This to resize, then upload the file:

handleImageUploadFormSubmit = (event) => {
    event.preventDefault();
    this.setState({imageUploadResponse: {}});
    const formData = new FormData();
    const HERMITE = new Hermite_class();

    let reader = new FileReader();
    let image = document.createElement("img");

    reader.onload = (readerEvent) => {
        image.onload = () => { 
            console.log('image on load', image); 
        };
        image.src = readerEvent.target.result;
    };
    reader.readAsDataURL(this.state.selectedFile);

    image.setAttribute("id", "temp_img");
    // display while on development only
    document.getElementById("cc").appendChild(image);
    console.log('img_elem', image)

    HERMITE.resize_image('temp_img', 300, 200);
    .......
}

How do I contribute?

Hi
I have found a couple of bugs and created fixes for them.
I've created a pull request (#8), however do you have a preferred process?
Should I create the dist. versions as well, and if so, how can I go about building them?

Thanks,
Oisin

Incorrect resampling with multiple cores

When I resample an 112x113 image to 40x40, I end up with half the image missing on my Mac (8 cores). Resampling the image using resample_simple produces the correct results.

Source image (112x113):

rainbow 112x113

Incorrect result using resample(canvas, 40, 40, true):

rainbow 40x40 8 cores

Correct result using resample_single(canvas, 40, 40, true):

rainbow 40x40 single

FWIW, doing a resample(canvas, 50, 50, true) on the same source images produces a correct result; as does resampling larger images to 40x40. Other resample values for this source images produce wrong results (e.g. 39x39, 32x32, etc.) Probably a rounding error somewhere in there...

IE10 support

Hi,
lovely fast script with nice quality and with no artifacts or halos on transparent images.
Thanks for sharing.

I have an issue with ie10.

Resizer fails with a DataCloneError on line 152.

my_worker.postMessage(objData, [objData.source]);

For now i do a try-catch and inform user for unsupported browser like:

var resizer = new Hermite_class();

      	try {
            resizer.resample(testCanvas1, canvas.width, canvas.height, true, function () {
          	    // give canvas to user function
  		    options.callback(testCanvas1);
             });
        } catch (e) {
        	//Errors here
        	options.callback(false);
        }

Any other ideas ?

NPM

your npm is out of date.
can u update to this repo? thanks

and

block_height and offset_y
duplicate defined.
maybe can use eslint check code?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.