Skip to content

Commit c3d25a9

Browse files
authored
Add image processing examples (#658) [skip ci]
* add inplace image processing example * add s3 upload example * update dockerfile * for some reason, I need to include json if using ruby:dev * update read me * strip stdin input
1 parent 3e61005 commit c3d25a9

File tree

10 files changed

+123
-0
lines changed

10 files changed

+123
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bundle/
2+
.bundle/
3+
func.yaml
4+
Gemfile.lock
5+
*.png
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM iron/ruby:dev
2+
RUN apk --no-cache --update add imagemagick
3+
4+
COPY ./ /func
5+
WORKDIR /func
6+
7+
RUN gem install bundler --no-ri --no-rdoc
8+
RUN bundle
9+
10+
ENTRYPOINT bundle exec ruby func.rb
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'json'
4+
gem 'mini_magick'
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## Image processing with Ruby Example
2+
3+
This example will show you how to process images with a Ruby function.
4+
5+
```sh
6+
# create your func.yaml file
7+
fn init <YOUR_DOCKERHUB_USERNAME>/image-processing
8+
9+
# build the function
10+
fn build
11+
12+
# test it
13+
echo "http://www.sourcecertain.com/img/Example.png" | fn run > image.png
14+
15+
# push it to Docker Hub
16+
fn push
17+
18+
# Create a route to this function on IronFunctions (assuming you have an app called `test`)
19+
fn routes create test /image-processing
20+
21+
# you can now access via curl as well
22+
curl -v -X POST http://localhost:8080/r/test/image-processing -d "http://www.sourcecertain.com/img/Example.png" > image.png
23+
```
24+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require 'mini_magick'
2+
3+
payload = STDIN.read.strip
4+
image = MiniMagick::Image.open(payload)
5+
image.contrast
6+
image.resize "250x200"
7+
image.rotate "-90"
8+
image.write STDOUT
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bundle/
2+
.bundle/
3+
func.yaml
4+
Gemfile.lock
5+
*.png
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM iron/ruby:dev
2+
RUN apk --no-cache --update add imagemagick
3+
4+
COPY ./ /func
5+
WORKDIR /func
6+
7+
RUN gem install bundler --no-ri --no-rdoc
8+
RUN bundle
9+
10+
ENV AWS_ACCESS_KEY_ID {add your id here}
11+
ENV AWS_SECRET_ACCESS_KEY {add your key here}
12+
ENV AWS_S3_BUCKET iron-functions-image-resize
13+
ENV AWS_REGION us-east-1
14+
15+
ENTRYPOINT bundle exec ruby func.rb
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'json'
4+
gem 'mini_magick'
5+
gem 'aws-sdk-s3', '~> 1'
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
## Image processing with Ruby Example, Upload to S3
2+
3+
This example will show you how to process images with a Ruby function, and upload to a S3 bucket.
4+
5+
```sh
6+
# create your func.yaml file
7+
fn init <YOUR_DOCKERHUB_USERNAME>/image-processing
8+
9+
# build the function
10+
fn build
11+
12+
# test it
13+
echo "http://www.sourcecertain.com/img/Example.png" | fn run
14+
15+
# push it to Docker Hub
16+
fn push
17+
18+
# Create a route to this function on IronFunctions (assuming you have an app called `test`)
19+
fn routes create test /image-processing
20+
21+
# you can now access via curl as well
22+
curl -v -X POST http://localhost:8080/r/test/image-processing -d "https://www.nationalgeographic.com/content/dam/science/photos/000/010/1086.ngsversion.1491440409220.adapt.1900.1.jpg"
23+
> https://iron-functions-image-resize.s3.amazonaws.com/1086.ngsversion.1491440409220.adapt.1900.1.jpg
24+
```
25+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'mini_magick'
2+
require 'aws-sdk-s3'
3+
4+
file_uri = STDIN.read.strip
5+
image = MiniMagick::Image.open(file_uri)
6+
image.contrast
7+
image.resize "250x200"
8+
image.rotate "-90"
9+
10+
s3 = Aws::S3::Client.new
11+
bucket = ENV['AWS_S3_BUCKET']
12+
13+
obj = s3.put_object( bucket: bucket,
14+
key: File.basename(file_uri),
15+
body: image.tempfile,
16+
acl: "public-read",
17+
cache_control: "max-age=604800")
18+
19+
# Unfortunately put_object returns `put object output`, not an object.
20+
# So we create another reference here. Probably there is a better way to do this in S3 API.
21+
obj = Aws::S3::Object.new bucket_name: bucket, key: File.basename(file_uri)
22+
puts obj.public_url

0 commit comments

Comments
 (0)