functions/imagemagick/app.rb
# Blurs the given file using ImageMagick.
Blurs the given file using ImageMagick.
Blurs the given file using ImageMagick .
def blur_image bucket_name, file_name
  tempfile = Tempfile.new
  begin
    bucket = global(:storage_client).bucket bucket_name
    file = bucket.file file_name
    file.download tempfile
    tempfile.close
    MiniMagick::Image.new tempfile.path do |image|
      image.blur "0x16"
    end
    logger.info "Image #{file_name} was blurred"
    blur_bucket_name = ENV["BLURRED_BUCKET_NAME"]
    blur_bucket = global(:storage_client).bucket blur_bucket_name
    blur_bucket.create_file tempfile.path, file_name
    logger.info "Blurred image uploaded to gs://#{blur_bucket_name}/#{file_name}"
  ensure
    tempfile.unlink
  end
end
def blur_image bucket_name , file_name tempfile = Tempfile . new begin bucket = global ( :storage_client ) . bucket bucket_name file = bucket . file file_name file . download tempfile tempfile . close MiniMagick :: Image . new tempfile . path do | image | image . blur "0x16" end logger . info "Image #{file_name} was blurred" blur_bucket_name = ENV [ "BLURRED_BUCKET_NAME" ] blur_bucket = global ( :storage_client ) . bucket blur_bucket_name blur_bucket . create_file tempfile . path , file_name logger . info "Blurred image uploaded to gs://#{blur_bucket_name}/#{file_name}" ensure tempfile . unlink end end
Blurs the given file using ImageMagick.
Blurs the given file using ImageMagick .
# Download the image file # Blur the image using ImageMagick # Upload result to a second bucket, to avoid re-triggering the function. # You could instead re-upload it to the same bucket and tell your function # to ignore files marked as blurred (e.g. those with a "blurred" prefix.) # Ruby will remove the temp file when garbage collecting the object, # but it is good practice to remove it explicitly.
bucket_name
<not_specific>
file_name
<not_specific>