Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jefer94/gcloud-resize
https://github.com/jefer94/gcloud-resize
Last synced: 11 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/jefer94/gcloud-resize
- Owner: jefer94
- Created: 2023-11-07T06:33:06.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-11-07T06:36:29.000Z (about 1 year ago)
- Last Synced: 2024-04-13T23:02:15.731Z (7 months ago)
- Language: Go
- Size: 59.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Google Cloud Function for Image Resizing
This Google Cloud Function is designed to resize images from one Google Cloud Storage bucket and save the resized image to the same bucket with the desired dimensions. It allows you to specify the source filename, bucket, and the dimensions (width and height) for the resized image.
## Execute locally
```bash
FUNCTION_TARGET=TransferFile LOCAL_ONLY=true go run cmd/main.go
```## Prerequisites
Before using this function, make sure you have the following prerequisites in place:
1. **Google Cloud Storage Bucket**: You need a Google Cloud Storage bucket where the source image is stored and where the resized image will be saved. Ensure you have the appropriate access permissions for the bucket.
2. **Google Cloud Functions**: You should have a Google Cloud Functions project set up.
3. **MessagePack Package**: This function uses the `github.com/vmihailenco/msgpack` package for working with MessagePack data.
## Function Overview
The `TransferImage` function allows you to resize an image stored in a Google Cloud Storage bucket and save the resized image back to the same bucket. Here's how it works:
1. The function parses the incoming request data using MessagePack. The request should include the source filename, the source bucket, and the desired dimensions for the resized image.
2. It checks for the correctness of the provided data and ensures that both the filename and bucket are specified, and at least one of the dimensions (width or height) is provided.
3. If the source filename ends with "-thumbnail," the function returns a response indicating that it can't resize a thumbnail.
4. The function initializes the Google Cloud Storage client and gets a handle to the source bucket and the source image object.
5. It reads the image content and determines the MIME type of the image.
6. If the MIME type is allowed (based on the predefined list of allowed types), the function extracts the file extension based on the MIME type.
7. It decodes the image, calculates the new dimensions while maintaining the aspect ratio, and resizes the image.
8. The function creates a .meta file in the same bucket to store the MIME type information.
9. The resized image is saved to the same bucket with a filename indicating the new dimensions.
10. The function responds with a success message and the dimensions of the resized image.
## Usage
You can trigger the `TransferImage` function by making an HTTP request with a MessagePack-encoded JSON payload containing the source filename, source bucket, and the desired dimensions. For example:
```json
{
"filename": "source-image.jpg",
"bucket": "your-bucket-name",
"width": 300,
"height": 200
}