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

Broken jpeg data when using toDataURL image/jpeg on 2.3.0 #1350

Closed
pstaffan opened this issue Jan 12, 2019 · 4 comments
Closed

Broken jpeg data when using toDataURL image/jpeg on 2.3.0 #1350

pstaffan opened this issue Jan 12, 2019 · 4 comments
Assignees

Comments

@pstaffan
Copy link

Issue or Feature

The dataURL generated for image/jpeg seems broken on version 2.3.0. It works well on version 2.2.0.
Example: brokenjpeg.txt

Steps to Reproduce

const {createCanvas, Image} = require("canvas")
const {writeFileSync, readFileSync} = require("fs")

const canvas = createCanvas(800, 50)
const ctx = canvas.getContext("2d")
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, 800, 50);
ctx.font = '16.5pt sans-serif'
ctx.fillStyle = 'black';
ctx.fillText('A B C D E F G H I J K L M N O P Q R S T U V W X Y Z', 10, 30);
canvas.toDataURL('image/jpeg', function (error, dataUrl) {
    console.log(dataUrl);
});
writeFileSync("out.jpg", canvas.toBuffer())

Your Environment

  • Version of node-canvas (output of npm list canvas or yarn list canvas):
    canvas@2.3.0

  • Environment (e.g. node 4.2.0 on Mac OS X 10.8):
    node v10.15.0 on Ubuntu 18 LTS
    build-essential is already the newest version (12.4ubuntu1).
    libcairo2-dev is already the newest version (1.15.10-2).
    libgif-dev is already the newest version (5.1.4-2).
    libjpeg-dev is already the newest version (8c-2ubuntu8).
    librsvg2-dev is already the newest version (2.40.20-2).
    libpango1.0-dev is already the newest version (1.40.14-1ubuntu0.1).

@zbjornson
Copy link
Collaborator

Hrm, works on Windows but confirmed broken on Ubuntu. Will look at today, thanks for reporting.

@zbjornson zbjornson self-assigned this Jan 12, 2019
zbjornson added a commit to zbjornson/node-canvas that referenced this issue Jan 13, 2019
Was mixing vec.capacity() and vec.size(). With MSVC the vec's growth factor is 1.5 (same as what I'm using), so this bug didn't manifest. With glibc it's 2.0. Also had an off-by-one.

Fixes Automattic#1350
zbjornson added a commit to zbjornson/node-canvas that referenced this issue Jan 13, 2019
Was mixing vec.capacity() and vec.size(). With MSVC the vec's growth factor is 1.5 (same as what I'm using), so this bug didn't manifest. With glibc it's 2.0. Also had an off-by-one.

Fixes Automattic#1350
@zbjornson
Copy link
Collaborator

Fix in #1352. BTW your last line is actually producing a PNG; you need writeFileSync("out.jpg", canvas.toBuffer("image/jpeg")).

@pstaffan
Copy link
Author

Many thanks!! I've tested the changes in my projects test suit and it looks good here

@LinusU
Copy link
Collaborator

LinusU commented Jan 14, 2019

2.3.1 is being released with a fix for this 👍

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

No branches or pull requests

3 participants