跳到主要内容

压缩图像 API

此端点的功能:PDF4me 压缩图像 REST API 减小文件大小 JPGPNG以及使用智能压缩算法的 WebP 图像。请将您的图像以以下格式发送: Base64编码字符串设置输出格式和压缩级别,并接收压缩后的图像,所有操作都在一次同步过程中完成。 POST 调用此函数可以优化电子商务页面的产品图片,缩小 CMS 管道中上传的资源,生成缩略图,或降低任何处理图像文件的应用程序的带宽成本。

相关博客文章(1)

验证您的身份 API 要求

每一个 PDF4me REST API 通话必须包含您的 API 关键在于 Authorization 头部信息。从开发者控制面板获取密钥并妥善保管,切勿在客户端代码中泄露。

您不容错过的重要事实

All four parameters are required: none can be omitted

File Name, File Content, Image Type, and Compression Level must all be present in the request payload. A missing or empty field causes a 400 Bad Request error. Validate all four values in your code before sending the API call.

Image Type must match the actual binary format of File Content

The API uses Image Type to select the correct decompression codec before re-encoding. Sending PNG data with Image Type set to JPG causes a format mismatch error. Always read the actual MIME type or file extension from your source and set Image Type to match.

Max compresses the most but degrades quality: Low preserves quality with modest savings

Max reduces file size by 80-90%: ideal for thumbnails and social media previews where pixel-perfect detail is less important. Medium (60-70% reduction) suits most product and blog images. Low (30-40% reduction) is recommended when you need to preserve fine detail for print, zoom features, or professional photography archives.

REST API 端点

方法端点
POST/api/v2/CompressImage

基本网址: https://api.pdf4me.com

参数

⚠️ 四个参数均为必填项。API 如果缺少任何字段或字段为空,则返回错误。

范围必需的类型它的作用例子
docNameRequiredStringFilename of the source image including the file extension. Used to identify the format and name the output file. Use descriptive, extension-correct names.product-hero.jpg
docContentRequiredBase64The complete image file encoded as a Base64 string. Supported formats: JPG/JPEG, PNG, WebP. Maximum file size: 50 MB. Do not include a data URL prefix: pass the raw Base64 string only.JVBERi0xLjQK...
imageTypeRequiredEnumOutput image format. Must match the actual format of docContent. Accepted values: JPG · PNG · WebPJPG
compressionLevelRequiredEnumCompression intensity. Max: 80-90% size reduction, good quality (thumbnails, social media). Medium, 60-70% reduction, very good quality (product images, blogs). Low, 30-40% reduction, excellent quality (print, zoom, archives).Medium

输出字段

场地类型描述
文件内容Base64压缩图像文件编码为 Base64解码为二进制格式并保存为文件。
文件名String压缩后的输出图像文件名。

请求示例

标题

Content-Type: application/json
Authorization: YOUR_API_KEY

JSON 有效载荷

{
"docName": "product-hero.jpg",
"docContent": "JVBERi0xLjQK...",
"imageType": "JPG",
"compressionLevel": "Medium"
}

回复

{
"File Content": "BASE64_COMPRESSED_IMAGE_DATA",
"File Name": "product-hero.jpg"
}

代码示例

常见用例

E-commerce Product Images

Compress JPG product photos to Medium level before serving on listing pages. Reduces page load time by 60-70% without visible quality loss on standard displays: directly improving Core Web Vitals and conversion rates.

CMS Upload Pipeline

Intercept image uploads in your CMS (WordPress, Contentful, Strapi) and run them through Compress Image at Max level before storing. Thumbnails and hero banners benefit most: users see no quality difference at normal viewing sizes.

Print-to-Web Asset Conversion

High-resolution press images (300+ DPI, often 20-50 MB) can be compressed at Low level to maintain near-print quality while reducing file size by 30-40%. Ideal for photography portfolios, architectural galleries, and product zoom features.

常见问题解答

What is the maximum file size the Compress Image API accepts?

The API accepts images up to 50 MB. For best performance, keep images under 10 MB. Larger files increase processing time significantly and may exceed request timeouts on slow connections.

Does Image Type need to match the actual binary format of File Content?

Yes: always. The API uses imageType to select the correct decompression codec before re-encoding at the chosen quality level. Sending PNG data with imageType set to JPG causes a format mismatch error. Read the actual MIME type or file extension from your source and set imageType to match exactly.

Which Compression Level should I use for e-commerce product images?

Medium is the recommended level for most product images. It reduces file size by 60-70% while maintaining very good visual clarity for standard monitor and mobile display sizes. Use Low only when your product images include fine textures or details that customers zoom into.

Can I convert and compress an image (e.g. PNG to WebP) in the same API call?

No: Compress Image only compresses within the same format. To convert between formats, call the Convert Image Format endpoint first, then pass the result to Compress Image in a second call. Chain these two calls in your application for a compress-and-convert pipeline.

Is the output File Content returned as Base64 or binary?

The output File Content is Base64-encoded. Decode it to binary before writing to a file. In Python: base64.b64decode(response["File Content"]). In Node.js: Buffer.from(response["File Content"], "base64").

有关的 APIs

获取帮助