转换图像格式 API
此端点的功能: 这 PDF4me 转换图像格式 REST API 将图像在 JPG, PNGWebP、BMP、GIF 和 TIFF 单一格式 POST 致电。请将您的源图像作为附件发送。 Base64-编码字符串,指定当前格式和目标格式,并接收转换后的图像,也可以作为 Base64使用它来标准化处理流程中的图像格式,转换传统的 BMP 或其他格式。 TIFF 用于 Web 交付的文件,或者在通过“压缩图像”端点运行图像之前准备图像。
验证您的身份 API 要求
每一个 PDF4me REST API 通话必须包含您的 API 关键在于 Authorization 头部信息。从开发者控制面板获取密钥,并避免将其用于客户端代码。
您不容错过的重要事实
docName, docContent, CurrentImageFormat, and NewImageFormat must all be present. A missing format parameter causes the API to return a 400 error. Always provide both format fields explicitly: do not assume the API will infer the source format from the file extension in docName.
The API uses CurrentImageFormat to select the correct decoding codec. If you send a PNG file but set CurrentImageFormat to JPG, decoding fails with a format error. Read the real format from your source file MIME type or binary header: never derive it solely from the filename extension, which can be wrong.
JPG does not support an alpha channel. Transparent areas in a PNG or GIF source are filled with white (or a default background) during JPG conversion. If your image has a transparent background: logos, cut-out product photos, icons, convert to PNG, GIF, or BMP instead of JPG to preserve the transparency.
REST API 端点
| 方法 | 端点 |
|---|---|
| POST | /api/v2/ConvertImageFormat |
基本网址: https://api.pdf4me.com
参数
⚠️ 四个参数均为必填项。 这 API 如果缺少任何字段,则返回错误。
| 范围 | 必需的 | 类型 | 它的作用 | 例子 |
|---|---|---|---|---|
| docName | Required | String | Filename of the source image including the file extension. Used for output file naming. Should reflect the actual format of the source file. | company-logo.png |
| docContent | Required | Base64 | The complete source image file encoded as a Base64 string. Maximum file size: 50 MB. Do not include a data URL prefix: pass the raw Base64 string only. | iVBORw0KGgoAAAANSUhEUg... |
| CurrentImageFormat | Required | Enum | The actual format of the source image in docContent. Must match exactly. Accepted values: BMP · GIF · JPG · PNG · TIFF | PNG |
| NewImageFormat | Required | Enum | The target format for the converted output image. Accepted values: BMP · GIF · JPG · PNG · TIFF | JPG |
输出字段
| 场地 | 类型 | 描述 |
|---|---|---|
| 文件内容 | Base64 | 转换后的图像编码为 Base64解码为二进制文件并以正确的新文件扩展名保存。 |
| 文件名 | String | 转换后的输出图像的文件名。 |
请求示例
标题
Content-Type: application/json
Authorization: YOUR_API_KEY
JSON 有效载荷
{
"docName": "company-logo.png",
"docContent": "iVBORw0KGgoAAAANSUhEUg...",
"CurrentImageFormat": "PNG",
"NewImageFormat": "JPG"
}
回复
{
"File Content": "BASE64_CONVERTED_IMAGE_DATA",
"File Name": "company-logo.jpg"
}
代码示例
常见用例
Marketing teams often accumulate mixed BMP, TIFF, and GIF files from various sources. Convert them all to PNG in a batch processing loop: PNG gives lossless quality and wide browser support, making it the safest default web format for logos and graphics.
Studio photographs are typically delivered as TIFF (50-200 MB each). Convert to JPG before uploading to your e-commerce platform or CDN to reduce storage costs by 80-90%. Pair with the Compress Image endpoint for a complete optimisation pipeline.
WebP images are 25-35% smaller than equivalent JPGs and supported by all modern browsers. Convert JPG or PNG assets to WebP to improve Core Web Vitals scores. Serve WebP with a JPG fallback using HTML picture elements.
常见问题解答
▶ What image formats does the Convert Image Format API support?
The API supports BMP, GIF, JPG, PNG, and TIFF as both input (CurrentImageFormat) and output (NewImageFormat). These five formats cover the most common image types used in enterprise and web workflows.
▶ Does CurrentImageFormat need to match the actual binary format of docContent?
Yes: always. The API uses CurrentImageFormat to select the correct decoding codec. A mismatch between the actual binary format and CurrentImageFormat causes a decoding failure. Read the real format from your file's MIME type or binary signature, not just the filename extension.
▶ How does PNG-to-JPG conversion handle transparency?
JPG does not support an alpha channel. Transparent areas in a PNG source are replaced with a solid background colour (typically white) during conversion. If preserving transparency matters: logos, product cut-outs, icons, convert to PNG, GIF, or BMP instead of JPG.
▶ Can I convert and compress an image in the same API call?
No: Convert Image Format only changes the format. To also compress, call Convert Image Format first to get the target format, then pass the result's File Content to the Compress Image endpoint with your chosen compression level. Chain both calls in your application.
▶ What is the maximum file size accepted by this endpoint?
Up to 50 MB. For best performance keep images under 10 MB. Note that format conversion can change file size: converting a PNG to JPG typically reduces size, while converting JPG to TIFF may increase it significantly.