假设我有一个表
---------Image-------------
imageFile (File) | thumbnail (File) | Post (a pointer to Post)
知道如何编写云代码以在另一列中获取该图像的较小版本吗?
例如,如果用户上传了(2000x3500像素)图像,Parse会将其保存在imageFile列中,并将其缩略图保存在其他列中
感谢
答案 0 :(得分:2)
以下是我使用的当前解决方案(原始脚本由Parse.com发布,并在所有权转移到社区后删除):
main.js中的用法示例:
Private Sub UserForm_Initialize()
'Empty AssignToBox
AssignTo.Value = ""
'Empty Zones
Zones.Clear
'ZonesDatabase
With Zones
.AddItem "North"
.AddItem "South"
.AddItem "Central"
.AddItem "West"
.AddItem "East"
.AddItem "CPC"
End With
'Empty Department
Department.Value = ""
'Empty Designation
Designation.Clear
'Designation Database
With Designation
.AddItem "Assistant"
.AddItem "Senior Assistant"
.AddItem "Executive"
.AddItem "Senior Executive"
.AddItem "Assistant Manager"
.AddItem "Associate Manager"
.AddItem "Manager"
.AddItem "Senior Manager"
.AddItem "Chief Manager"
.AddItem "Assistant Vice President"
.AddItem "Associate Vice President"
.AddItem "Vice President"
.AddItem "Senior Vice President"
.AddItem "Executive Vice President"
End With
'Empty Grade
Grade.Clear
'Grade Database
With Grade
.AddItem "7B"
.AddItem "7A"
.AddItem "6B"
.AddItem "6A"
.AddItem "5B"
.AddItem "5A"
.AddItem "4B"
.AddItem "4A"
.AddItem "3B"
.AddItem "3A"
.AddItem "2B"
.AddItem "2A"
.AddItem "1B"
.AddItem "1A"
End With
'Empty Location
Location.Value = ""
'Empty ProfileShortlisted
ProfileShortlisted.Value = ""
'Empty ProfileLinedUp
ProfileLinedUp.Value = ""
'Empty ShortlistedForInterview
ShortListedforInterview.Value = ""
'Empty Status
Status.Clear
'StatusDatabase
With Status
.AddItem "Open"
.AddItem "Close"
.AddItem "WIP"
.AddItem "Joined"
End With
'Empty Remark
Remark.Value = ""
End Sub
Private Sub ok_Click()
Dim emptyRow As Long
'Checklist active
Checklist.Activate
'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
'Transfer information
Cells(emptyRow, 1).Value = AssignTo.Value
Cells(emptyRow, 2).Value = Zones.Value
Cells(emptyRow, 3).Value = Department.Value
Cells(emptyRow, 4).Value = Desigantion.Value
Cells(emptyRow, 5).Value = Grade.Value
Cells(emptyRow, 6).Value = RequestDate.Value
Cells(emptyRow, 7).Value = Location.Value
Cells(emptyRow, 8).Value = ProfileShortlisted.Value
Cells(emptyRow, 9).Value = ProfileLinedUp.Value
Cells(emptyRow, 10).Value = ShortListedforInterview.Value
Cells(emptyRow, 11).Value = OfferedDate.Value
Cells(emptyRow, 12).Value = DateOfJoining.Value
Cells(emptyRow, 13).Value = Status.Value
Cells(emptyRow, 14).Value = Remarks.Value
End Sub
'Empty Remarks
Remarks.Value = ""
End Sub
End Sub
resize-image-key.js(async-await)
var resizeImageKey = require('cloud/resize-image-key.js'),
THUMBNAIL_SIZE = 100,
Image = require("parse-image");
Parse.Cloud.afterSave("TableName", function(request) {
return resizeImageKey({
object: request.object, // ParseObject in which the thumbnail will be saved
url: objectImage.url(), // Full size image URL
toKey: "thumbnail", // thumbnail's attribute key
width: THUMBNAIL_SIZE, // width
crop: false // resize or crop ?
});
});
resize-image-key.js(vanilla JS)
const Image = require("parse-image");
/*
Original: https://github.com/ParsePlatform/Anyimg/blob/master/parse/cloud/resize-image-key.js
Resizes an image from one Parse Object key containing
a Parse File to a file object at a new key with a target width.
If the image is smaller than the target width, then it is simply
copied unaltered.
object: Parse Object
url: URL of the Parse File
toKey: Key to contain the target Parse File
width: Target width
crop: Center crop the square
*/
module.exports = function(options) {
let format, originalHeight, originalWidth, newHeight, newWidth;
// First get the image data
const response = await Parse.Cloud.httpRequest({
//url: options.object.get(options.fromKey).url()
url: options.url
})
let image = new Image();
await image.setData(response.buffer);
// set some metadata that will be on the object
format = image.format();
originalHeight = image.height();
originalWidth = image.width();
if (image.width() <= options.width) {
// No need to resize
// Remove from code
} else {
var newWidth = options.width;
var newHeight = options.width * image.height() / image.width();
// If we're cropping to a square, then we need to adjust height and
// width so that the greater length of the two fits the square
if (options.crop && (newWidth > newHeight)) {
var newHeight = options.width;
var newWidth = newHeight * image.width() / image.height();
}
// resize down to normal width size
image = await image.scale({
width: newWidth,
height: newHeight
});
}
// crop ?
if (options.crop) {
let left = 0;
let top = 0;
// Center crop
if (image.width() > image.height()) {
left = (image.width() - image.height()) / 2;
} else {
top = (image.height() - image.width()) / 2;
}
image = await image.crop({
left: left,
top: top,
width: options.width,
height: options.width
});
}
newHeight = image.height();
newWidth = image.width();
// Get the image data in a Buffer.
const buffer = await image.data();
// Save the image into a new file.
const base64 = buffer.toString("base64");
//var scaled = new Parse.File("thumbnail_" + options.object.get("name") + "." + format, {
const scaled = new Parse.File("thumbnail." + format, {
base64: base64
});
const savedImage = await scaled.save();
// Set metadata on the image object
options.object.set(options.toKey, savedImage);
//return options.object.save();
options.object.set("thumbnail_width", newWidth);
options.object.set("thumbnail_height", newHeight);
};
更新:找到原始脚本的克隆并将其存档以防万一:https://web.archive.org/web/20190107225015/https://github.com/eknight7/Anyimg/blob/master/parse/cloud/resize-image-key.js