.autoOrient() doesn't work with .thumb() #99

Closed
ARAtlas opened this Issue Nov 20, 2012 · 5 comments

Comments

Projects
None yet
4 participants
@ARAtlas

ARAtlas commented Nov 20, 2012

I need to create thumbnails of images that may be EXIF-rotated. I tried sticking an .autoOrient() before the .thumb() call like this:

imageMagick('public/images/'+originalImage)
  .autoOrient()
  .thumb(size, size, 'public/images/'+thumbImage, quality, 'center', function(err) {

When I do that, the output image re-orients correctly, but now the thumbnail is squashed-to-square instead of cropped-to-square. Is this a bug or am I missing something?

@aheckmann

This comment has been minimized.

Show comment
Hide comment
@aheckmann

aheckmann Nov 21, 2012

Owner

Not sure, both thumb and autoOrient are sugar and involve several calls to graphicsmagick which may not play perfectly well together. It should be easy to track down what this issue is by just executing the code inline and playing with the options that are used.

If thumb doesn't work right without autoOrient that could be related to changes made in 1.6 and how size is calculated: https://github.com/aheckmann/gm/blob/master/lib/convenience/thumb.js#L21

Let me know what you find out.

Owner

aheckmann commented Nov 21, 2012

Not sure, both thumb and autoOrient are sugar and involve several calls to graphicsmagick which may not play perfectly well together. It should be easy to track down what this issue is by just executing the code inline and playing with the options that are used.

If thumb doesn't work right without autoOrient that could be related to changes made in 1.6 and how size is calculated: https://github.com/aheckmann/gm/blob/master/lib/convenience/thumb.js#L21

Let me know what you find out.

@dncrews

This comment has been minimized.

Show comment
Hide comment
@dncrews

dncrews Nov 24, 2012

autoOrient doesn't play well with resize(), either. To get it to do both, I have to do an orient, and then a write, like this: (coffeescript)

img = im(path).autoOrient().write path, (err) ->
    if err
        return console.log err
    img = im(path)
    img.resize(200, 200).write newPath, (err) ->
        if err
            return console.log err
        # success code

I wonder if this is only an issue because both me and OP are using the ImageMagick subClass...?

dncrews commented Nov 24, 2012

autoOrient doesn't play well with resize(), either. To get it to do both, I have to do an orient, and then a write, like this: (coffeescript)

img = im(path).autoOrient().write path, (err) ->
    if err
        return console.log err
    img = im(path)
    img.resize(200, 200).write newPath, (err) ->
        if err
            return console.log err
        # success code

I wonder if this is only an issue because both me and OP are using the ImageMagick subClass...?

@ARAtlas

This comment has been minimized.

Show comment
Hide comment
@ARAtlas

ARAtlas Nov 27, 2012

The problem appears to be that autoOrient() somehow clears out the options set by thumb(). If I run thumb() alone, then this is the arguments that get used:

"-quality" "80" "public/images/7b96fdc5f296fedecdb1bdb9c191bb25/image.jpg" "-scale" "133x100" "-crop" "100x100+16.5+0" "+profile" ""*"" "public/images/7b96fdc5f296fedecdb1bdb9c191bb25/image_100x100.jpeg"

However, if I chain in an autoOrient().thumb(), and then hold my phone in such a way that no rotation is necessary, then these arguments get used:

convert "public/images/a7728a4ca56ad238721459961fc36f45/image.jpg" "public/images/a7728a4ca56ad238721459961fc36f45/image_100x100.jpeg"

If I chain autoOrient().thumb(), and hold my phone at a 90 degree angle, these arguments get used:

"public/images/2c263cc7fc55edee15e7e3ae2e66649c/image.jpg" "-rotate" "90" "-page" "+0+0" "+profile" ""*"" "public/images/2c263cc7fc55edee15e7e3ae2e66649c/image_100x100.jpeg"

Again, these arguments are ones set by autoOrient(), with none of the options set by thumb() making it through to the end.

ARAtlas commented Nov 27, 2012

The problem appears to be that autoOrient() somehow clears out the options set by thumb(). If I run thumb() alone, then this is the arguments that get used:

"-quality" "80" "public/images/7b96fdc5f296fedecdb1bdb9c191bb25/image.jpg" "-scale" "133x100" "-crop" "100x100+16.5+0" "+profile" ""*"" "public/images/7b96fdc5f296fedecdb1bdb9c191bb25/image_100x100.jpeg"

However, if I chain in an autoOrient().thumb(), and then hold my phone in such a way that no rotation is necessary, then these arguments get used:

convert "public/images/a7728a4ca56ad238721459961fc36f45/image.jpg" "public/images/a7728a4ca56ad238721459961fc36f45/image_100x100.jpeg"

If I chain autoOrient().thumb(), and hold my phone at a 90 degree angle, these arguments get used:

"public/images/2c263cc7fc55edee15e7e3ae2e66649c/image.jpg" "-rotate" "90" "-page" "+0+0" "+profile" ""*"" "public/images/2c263cc7fc55edee15e7e3ae2e66649c/image_100x100.jpeg"

Again, these arguments are ones set by autoOrient(), with none of the options set by thumb() making it through to the end.

JNissi added a commit to JNissi/gm that referenced this issue Dec 6, 2012

Avoid losing already set arguments on identify.
When we exec identify, we don't want to lose the options set for the other commands that will come after identify.

This should fix autoOrient (calls orientation, which in turn execs identify) acting up as in #99

@aheckmann aheckmann closed this in 1344dcd Dec 21, 2012

@aheckmann

This comment has been minimized.

Show comment
Hide comment
@aheckmann

aheckmann Dec 21, 2012

Owner

fix is in 1.8.1

Owner

aheckmann commented Dec 21, 2012

fix is in 1.8.1

@danieldkim

This comment has been minimized.

Show comment
Hide comment
@danieldkim

danieldkim Feb 17, 2013

this is still an issue for me. i'm on 1.8.1 and the arguments are no longer being lost the but the final generated command does not produce the intended result.

i have some coffescript that does this:

gm(filePath).
  autoOrient().
  setFormat('jpeg').
  gravity('Center').
  thumb width, height, outPath, quality, (err) => next err, outPath

with a portrait image and width and height both equal to 213, this command is generated:

gm "convert" "-quality" "90" "-size" "285x213" "/tmp/foo.jpg" "-rotate" "90" "-page" "+0+0" "+profile" "\"*\"" "-gravity" "Center" "-scale" "285x213" "-crop" "213x213+0+0" "+profile" "\"*\"" "/tmp/foo-thumb.jpg"

the final image is not cropped, however. removing the "-scale" option fixes the issue. this command produces the proper thumb:

gm "convert" "-quality" "90" "-size" "285x213" "/tmp/foo.jpg" "-rotate" "90" "-page" "+0+0" "+profile" "\"*\"" "-gravity" "Center" "-crop" "213x213+0+0" "+profile" "\"*\"" "/tmp/foo-thumb.jpg"

i'm not sure why the "-scale" option is only an issue when auto-orienting but that's the behavior i'm observing.

this is still an issue for me. i'm on 1.8.1 and the arguments are no longer being lost the but the final generated command does not produce the intended result.

i have some coffescript that does this:

gm(filePath).
  autoOrient().
  setFormat('jpeg').
  gravity('Center').
  thumb width, height, outPath, quality, (err) => next err, outPath

with a portrait image and width and height both equal to 213, this command is generated:

gm "convert" "-quality" "90" "-size" "285x213" "/tmp/foo.jpg" "-rotate" "90" "-page" "+0+0" "+profile" "\"*\"" "-gravity" "Center" "-scale" "285x213" "-crop" "213x213+0+0" "+profile" "\"*\"" "/tmp/foo-thumb.jpg"

the final image is not cropped, however. removing the "-scale" option fixes the issue. this command produces the proper thumb:

gm "convert" "-quality" "90" "-size" "285x213" "/tmp/foo.jpg" "-rotate" "90" "-page" "+0+0" "+profile" "\"*\"" "-gravity" "Center" "-crop" "213x213+0+0" "+profile" "\"*\"" "/tmp/foo-thumb.jpg"

i'm not sure why the "-scale" option is only an issue when auto-orienting but that's the behavior i'm observing.

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