使用SuiteScript提取NetSuite文件柜中图片的宽高元数据
Absolutely! You can definitely get the width and height of images stored in NetSuite's File Cabinet using SuiteScript. Since NetSuite doesn't expose these dimensions as native file properties, you'll need to load the file and parse its binary metadata directly. Here's how to do it with SuiteScript 2.x (the modern, recommended approach):
Step 1: Load the Image File with the N/file Module
First, use the N/file module to load your image from the File Cabinet, and validate that it's a supported image type (JPG, PNG, GIF are the most common).
/** * @NApiVersion 2.x * @NScriptType UserEventScript */ define(['N/file'], function(file) { // Core function to get image dimensions function getImageDimensions(fileId) { try { // Load the target file from the File Cabinet const imageFile = file.load({ id: fileId }); // Validate we're dealing with a supported image type const validImageTypes = [file.Type.JPG, file.Type.PNG, file.Type.GIF]; if (!validImageTypes.includes(imageFile.fileType)) { throw new Error('The file is not a supported image type (JPG/PNG/GIF only).'); } // Get the file's binary content as a string const fileContent = imageFile.getContents(); let dimensions; // Parse dimensions based on image type switch(imageFile.fileType) { case file.Type.PNG: dimensions = parsePNGDimensions(fileContent); break; case file.Type.JPG: dimensions = parseJPGDimensions(fileContent); break; case file.Type.GIF: dimensions = parseGIFDimensions(fileContent); break; default: throw new Error('Unsupported image type for dimension parsing.'); } return dimensions; } catch (e) { log.error('Failed to get image dimensions', e.message); return null; } } // --- Add your parsing functions here --- // ... // Example usage in a user event script return { beforeLoad: function(context) { // Replace 1234 with your actual file ID const imageDims = getImageDimensions(1234); if (imageDims) { log.debug('Image Dimensions', `Width: ${imageDims.width}px | Height: ${imageDims.height}px`); } } }; });
Step 2: Parse Image Metadata
Each image format stores dimensions in different parts of its binary structure. Here are the parsing functions for the most common types:
Parse PNG Dimensions
PNG files store width and height as 4-byte big-endian integers starting at offset 16 (right after the 8-byte PNG signature).
function parsePNGDimensions(binaryContent) { // Extract width (bytes 16-19) const width = (binaryContent.charCodeAt(16) << 24) | (binaryContent.charCodeAt(17) << 16) | (binaryContent.charCodeAt(18) << 8) | binaryContent.charCodeAt(19); // Extract height (bytes 20-23) const height = (binaryContent.charCodeAt(20) << 24) | (binaryContent.charCodeAt(21) << 16) | (binaryContent.charCodeAt(22) << 8) | binaryContent.charCodeAt(23); return { width, height }; }
Parse JPG Dimensions
JPG files use segmented metadata. We need to find the "Start of Frame" (SOF) marker (0xFFC0 to 0xFFCF, excluding some non-image segments) which contains the height and width.
function parseJPGDimensions(binaryContent) { let offset = 2; // Skip the SOI marker (FFD8) while (offset < binaryContent.length) { const marker = (binaryContent.charCodeAt(offset) << 8) | binaryContent.charCodeAt(offset + 1); offset += 2; // Look for valid SOF markers (skip non-image segments like Huffman tables) if ((marker >= 0xFFC0 && marker <= 0xFFCF) && marker !== 0xFFC4 && marker !== 0xFFC8) { // Height is stored at offset +3 and +4 (2 bytes) const height = (binaryContent.charCodeAt(offset + 3) << 8) | binaryContent.charCodeAt(offset + 4); // Width is stored at offset +5 and +6 (2 bytes) const width = (binaryContent.charCodeAt(offset + 5) << 8) | binaryContent.charCodeAt(offset + 6); return { width, height }; } else { // Move past the segment's length field const segmentLength = (binaryContent.charCodeAt(offset) << 8) | binaryContent.charCodeAt(offset + 1); offset += segmentLength; } } throw new Error('Could not find valid JPG frame metadata.'); }
Parse GIF Dimensions
GIF files store width and height as 2-byte little-endian integers at offsets 6 and 8.
function parseGIFDimensions(binaryContent) { // Width is bytes 6-7 (little-endian) const width = (binaryContent.charCodeAt(7) << 8) | binaryContent.charCodeAt(6); // Height is bytes 8-9 (little-endian) const height = (binaryContent.charCodeAt(9) << 8) | binaryContent.charCodeAt(8); return { width, height }; }
Important Things to Keep in Mind
- Permissions: Ensure your script deployment has the
File Cabinetpermission set to at leastRead—otherwise, you'll get permission errors when loading files. - Governance: For very large images, parsing the binary content might hit NetSuite's script execution time or memory limits. Test with your typical image sizes to stay within governance rules.
- Edge Cases: Corrupted images or those with non-standard metadata might fail to parse. Add extra error handling if you expect these cases.
内容的提问来源于stack exchange,提问作者Steve Reeder

