Nicolas Garnier
8 years ago
5 changed files with 150 additions and 0 deletions
@ -0,0 +1,32 @@ |
|||
# Automatically Convert Images |
|||
|
|||
This sample demonstrates how to automatically convert images that are uploaded to Firebase Storage to JPEG using ImageMagick. |
|||
|
|||
|
|||
## Functions Code |
|||
|
|||
See file [functions/index.js](functions/index.js) for the email sending code. |
|||
|
|||
The image conversion is performed using ImagMagick which is installed by default on all Firebase Functions. This is a CLI for which we use a NodeJS wrapper. The image is first downloaded locally from the Firebase Storage bucket to the `tmp` folder using the [google-cloud](https://github.com/GoogleCloudPlatform/google-cloud-node) SDK. |
|||
|
|||
The dependencies are listed in [functions/package.json](functions/package.json). |
|||
|
|||
|
|||
## Trigger rules |
|||
|
|||
The function triggers on upload of any file to the Firebase Functions bucket. |
|||
|
|||
|
|||
## Setting up the sample |
|||
|
|||
Create a Firebase project on the [Firebase Console](https://console.firebase.google.com) and visit the **Storage** tab. |
|||
|
|||
Replace the placeholder `FIREBASE_STORAGE_BUCKET_NAME` with the name of the Firebase Storage bucket which can be found in the **Storage** tab of your Firebase project's console. It is typically of the form `<project-id>.appspot.com`. |
|||
|
|||
|
|||
## Deploy and test |
|||
|
|||
To test the sample: |
|||
|
|||
- Deploy your project using `firebase deploy` |
|||
- Go to the Firebase Console **Storage** tab and upload an image that is not a JEPG, for instance a PNG. After a short time an image with the same base name but the `.jpg` extension will be created in the same folder (make sure you refresh the UI to see the new file). |
@ -0,0 +1 @@ |
|||
{} |
@ -0,0 +1,99 @@ |
|||
/** |
|||
* Copyright 2016 Google Inc. All Rights Reserved. |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for t`he specific language governing permissions and
|
|||
* limitations under the License. |
|||
*/ |
|||
'use strict'; |
|||
|
|||
const functions = require('firebase-functions'); |
|||
const im = require('imagemagick'); |
|||
const mkdirp = require('mkdirp'); |
|||
const gcs = require('@google-cloud/storage')(); |
|||
const Q = require('q'); |
|||
|
|||
// File extension for the created JPEG files.
|
|||
const JPEG_EXTENSION = 'jpg'; |
|||
|
|||
/** |
|||
* When an image is uploaded in the Storage bucket it is converted to JPEG automatically using |
|||
* ImageMagick. |
|||
*/ |
|||
// TODO(DEVELOPER): Replace the placeholder below with the name of the Firebase Functions bucket.
|
|||
exports.imagetojpg = functions.cloud.storage(FIREBASE_STORAGE_BUCKET_NAME).onChange(event => { |
|||
console.log(event); |
|||
const result = Q.defer(); |
|||
|
|||
const filePath = event.data.name; |
|||
const filePathSplit = filePath.split('/'); |
|||
const fileName = filePathSplit.pop(); |
|||
const fileNameSplit = fileName.split('.'); |
|||
const fileExtension = fileNameSplit.pop(); |
|||
const baseFileName = fileNameSplit.join('.'); |
|||
const fileDir = filePathSplit.join('/') + (filePathSplit.length > 0 ? '/' : ''); |
|||
const JPEGFilePath = `${fileDir}${baseFileName}.${JPEG_EXTENSION}`; |
|||
const tempLocalDir = `/tmp/${fileDir}`; |
|||
const tempLocalFile = `${tempLocalDir}${fileName}`; |
|||
const tempLocalJPEGFile = `/tmp/${JPEGFilePath}`; |
|||
|
|||
// Exit if this is triggered on a file that is not an image.
|
|||
if (!event.data.contentType.startsWith('image/')) { |
|||
console.log('This is not an image.'); |
|||
return null; |
|||
} |
|||
|
|||
// Exit if this is triggered on a file that is not an image.
|
|||
if (event.data.contentType.startsWith('image/jpeg')) { |
|||
console.log('Already a JPEG.'); |
|||
return null; |
|||
} |
|||
|
|||
// Exit if this is a move or deletion event.
|
|||
if (event.data.resourceState === 'not_exists') { |
|||
console.log('This is a deletion event.'); |
|||
return null; |
|||
} |
|||
|
|||
// Create the temp directory where the storage file will be downloaded.
|
|||
mkdirp(tempLocalDir, err => { |
|||
if (err) return result.reject(err); |
|||
|
|||
// Download file from bucket.
|
|||
const bucket = gcs.bucket(event.data.bucket); |
|||
bucket.file(filePath).download({ |
|||
destination: tempLocalFile |
|||
}, err => { |
|||
if (err) return result.reject(err); |
|||
|
|||
console.log('The file has been downloaded to', tempLocalFile); |
|||
// Convert the image to JPEG using ImageMagick.
|
|||
im.convert([ |
|||
tempLocalFile, |
|||
tempLocalJPEGFile |
|||
], err => { |
|||
if (err) return result.reject(err); |
|||
|
|||
console.log('Created JPEG for', filePath); |
|||
// Uploading the JPEG.
|
|||
bucket.upload(tempLocalJPEGFile, { |
|||
destination: JPEGFilePath |
|||
}, err => { |
|||
if (err) return result.reject(err); |
|||
|
|||
console.log('JPEG uploaded to Storage at', JPEGFilePath); |
|||
result.resolve(); |
|||
}); |
|||
}); |
|||
}); |
|||
}); |
|||
return result.promise; |
|||
}); |
@ -0,0 +1,12 @@ |
|||
{ |
|||
"name": "functions", |
|||
"description": "Firebase Functions", |
|||
"dependencies": { |
|||
"@google-cloud/storage": "^0.2.0", |
|||
"firebase": "^3.4.1", |
|||
"firebase-functions": "https://storage.googleapis.com/firebase-preview-drop/node/firebase-functions/firebase-functions-preview.latest.tar.gz", |
|||
"imagemagick": "^0.1.3", |
|||
"mkdirp": "^0.5.1", |
|||
"q": "^1.4.1" |
|||
} |
|||
} |
Loading…
Reference in new issue