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

modifying Thumbor conf value on Mac OS X (RESPECT_ORIENTATION = True) #25

Closed
mattsly opened this issue Mar 29, 2018 · 15 comments
Closed

Comments

@mattsly
Copy link

mattsly commented Mar 29, 2018

I need to change the Thumbor to not rotate images by default by changing this config value
RESPECT_ORIENTATION = True to avoid the case like the attached image (basically all landscape mobile photos)

(I'm befuddled why RESPECT_ORIENTATION is False by default? Why would we want Thumbor to rotate an source image w/out explicitly asking for it?)

I'm on Mac OS X (High Sierra) - checked out repo, set up Python w/ virtualenv, ran ./build-s3-dist.sh {{bucketname...actually doesn't matter}} and all compiled well locally - then I uploaded the generated file from dist/serverless-image-handler.zip into an s3 bucket (needed to do that rather than direct upload since > 50MB) and then uploaded from the associated Lamda.

Excited to see my images not rotated...but got an internal server error trying to render an image and looking at CloudWatch see the following:

Unable to import module 'image_handler/lambda_function': /var/task/PIL/_imaging.so: invalid ELF header

Almost sure this is b/c I'm compiling in an environment that (OS X) that doesn't match that of the Lamda. (@arahayrabedian apparently had more luck: #11 - guessing he was on Linux?)

Any hints? Do I need to spin up an EC2 Linux or use Docker?

This is a lot of pain for a 1 line change! I wish Thumbor config files be adjusted from Lamda environment variables, but I believe they're build time so not possible.

image

@mattsly
Copy link
Author

mattsly commented Mar 29, 2018

Also - for this scenario of proxying source images I'd (obviously) advocate for overriding Thumbor's default and preserving the orientation on EXIF - honestly cannot imagine why you wouldn't want to do that

@mattsly
Copy link
Author

mattsly commented Mar 30, 2018

Update - was able to finally get this to work - posting a summary overview here for anyone else who is on a Mac and wants to make changes to the default Thumbor config

  1. Check out this project
  2. Make the one line (or more) edits per above in source/image_handler/thumbor.conf file
  3. Download and install Docker: https://docs.docker.com/docker-for-mac/install/
  4. Follow this basic outline from blog post and leverage this set of AWS Lamda specific built docker images to create a docker container based on the Lamda Python 2.7 environment and mount the directory of this project: - docker run -v <directory with this project>:/working -it lambci/lambda:build-python2.7 bash
  5. Within that Docker container, follow the README steps to set up the environment. (Note you are already root, so don't need sudo. Also skipped these steps: sudo yum-config-manager --enable epel and alias sudo='sudo env PATH=$PATH')
  6. Within Docker container, per the README here, cd deployment and ./build-s3-dist.sh source-bucket-base-name (note that you can use anything for the bucket name since the only file you care about is the deployment/dist/serverless-image-handler.zip file that gets generated following all of this)
  7. Load that .zip into your Lamda from the AWS UI (it's probably larger than 50MB, so will need to use S3 as an intermediate step or AWS command line if you're fancy)
  8. Bask in the wonder of unrotated images

@stevemorad
Copy link
Contributor

@mattsly thanks for the instructions. We typically recommend build this on an Amazon Linux instance (like a t2.micro or nano) because it's compiling binaries that will be executed in Lambda.

Looks like we need to think about an easier way to modify the thumbor.conf file if that's the only required changes.

@stevemorad
Copy link
Contributor

How are these instructions for simply modifying the thumbor.conf file without rebuilding the entire package?

Download the latest ZIP file:
https://s3.amazonaws.com/solutions-us-east-1/serverless-image-handler/latest/serverless-image-handler.zip

unzip -d serverless-image-handler serverless-image-handler.zip
cd serverless-image-handler
#Edit the thumbor/thumbor.conf file (vi thumbor/thumbor.conf)
zip -r9 ../serverless-image-handler.zip thumbor/thumbor.conf

Put the updated ZIP file in an S3 bucket and update the Lambda function.

@mattsly
Copy link
Author

mattsly commented Apr 11, 2018

@stevemorad I tried that and it didn't work - I think it's b/c thumbor.conf is read during build time so and not actually relevant at the Lamda runtime

(edit - remembering now - when I did that the zip file generated from my mac was generating an invalid ELF header error so I needed to spin up the Docker image to get a zip file that work)

@stevemorad
Copy link
Contributor

Couple of quick thoughts:

  1. You can create a Lambda environment variable to override any Thumbor conf file. So, you could create a Lambda envionrment varable RESPECT_ORIENTATION = True Thumor will prefer this value over what is in the conf file without even modifying the conf file.
  2. Make sure you are not getting caught in CloudFormation caching when you say the change didn't work. Did you try modifying the Thumbor filters to make sure you weren't just viewing a cached object?

@mattsly
Copy link
Author

mattsly commented Apr 18, 2018

wrt #1 - that would require code changes in the lamda function - it looks like RESPECT_ORIENTATION is only being read from the thumbor.conf file - not from the environment variable - https://github.com/awslabs/serverless-image-handler/blob/master/source/image-handler/lambda_function.py

A nice update to this project would be to many/most of the Thumbor.conf variables into the Lamda environment variables to enable.

@triunm
Copy link

triunm commented Apr 22, 2018

I have the same problem. There's no easy way for thumbor to maintain the rotation in EXIF data. I got it working w/ @stevemorad 's answer. Thanks. Hope we can have an update on this issue one to make it more seemless in future.

@triunm
Copy link

triunm commented Apr 22, 2018

And btw, on @stevemorad 's answer, editing the thumbor.conf in thumbor folder didn't worked for me. I also added the RESPECT_ORIENTATION = True in another thumbor.conf inside the image_handler folder to w/c the lambda function is using. See screenshot below

screen shot 2018-04-22 at 3 52 17 pm

@stevemorad
Copy link
Contributor

@mattsly -- the code should already be allowing thumber.conf variables to be specified with environment variables with this command:
https://github.com/awslabs/serverless-image-handler/blob/master/source/image-handler/lambda_function.py#L117

Are you saying that this does not work for RESPECT_ORIENTATION by creating a Lambda environment variable?

@stevemorad
Copy link
Contributor

@triunm -- thanks for pointing out the correct file to modify. Revised steps are here (although using Lambda environment variables is recommended if you do not want to modify the actual lambda ZIP file):

unzip -d serverless-image-handler serverless-image-handler.zip
cd serverless-image-handler
#Edit the image_handler/thumbor.conf file (vi image_handler/thumbor.conf)
zip -r9 ../serverless-image-handler.zip image_handler/thumbor.conf

@isTravis
Copy link

@stevemorad Thanks so much for pointing out that environment variables can be used to set Thumbor conf values. I found that this works for string values, but not for int values, e.g. QUALITY.

I get an error when I set an environment variable with key QUALITY and value 95.

Here is the error I'm receiving.
screen shot 2018-04-24 at 10 14 28 am

And here is where the error originates from in PIL

Any suggestions on how to work around this? Or do I have to just manually set the value in thumbor.conf as you describe above?

@mattsly
Copy link
Author

mattsly commented Apr 24, 2018

@stevemorad I definitely tried using the Lamda env variables from the Lamda UI and set RESPECT_ORIENTATION = True as well as ALLOW_UNSAFE_URL = False / did a reload w/ a new uncached URL (fit-in/301x302 - and confirmed in CloudWatch the requests were getting through) etc...but the new variable values are not being used as I would expect.

Possibly a typing (Boolean in this case) issue similar to what @isTravis notes above? (though I was not seeing any exceptions)

@stevemorad
Copy link
Contributor

For your RESPECT_ORIENTATION example, would you provide a direct link to an image that is not working? I tried cropping your example, but can't replicate the solution incorrectly rotating the image.

@mattsly
Copy link
Author

mattsly commented May 17, 2018

Sorry for the delay - I was able to get RESPECT_ORIENTATION to indeed work based on the Lamda env variable change only. I think I must have been testing w/ an already rotated image 🤦‍♂️ - closing this one out thanks for your help @stevemorad

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

4 participants