{"record":{"id":"be82eee4a45a55ee","repo":"PHPOffice/PHPWord","slug":"unsupported-image-type-imagemimetype","errorCode":null,"errorMessage":"Unsupported image type $imageMimeType","messagePattern":"Unsupported image type \\$imageMimeType","errorType":"exception","errorClass":"PhpOffice\\PhpWord\\Exception\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpWord/TemplateProcessor.php","lineNumber":612,"sourceCode":"        $relationTpl = '<Relationship Id=\"{RID}\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image\" Target=\"media/{IMG}\"/>';\n        $newRelationsTpl = '<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>' . \"\\n\" . '<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\"></Relationships>';\n        $newRelationsTypeTpl = '<Override PartName=\"/{RELS}\" ContentType=\"application/vnd.openxmlformats-package.relationships+xml\"/>';\n        $extTransform = [\n            'image/jpeg' => 'jpeg',\n            'image/png' => 'png',\n            'image/bmp' => 'bmp',\n            'image/gif' => 'gif',\n        ];\n\n        // get image embed name\n        if (isset($this->tempDocumentNewImages[$imgPath])) {\n            $imgName = $this->tempDocumentNewImages[$imgPath];\n        } else {\n            // transform extension\n            if (isset($extTransform[$imageMimeType])) {\n                $imgExt = $extTransform[$imageMimeType];\n            } else {\n                throw new Exception(\"Unsupported image type $imageMimeType\");\n            }\n\n            // add image to document\n            $imgName = 'image_' . $rid . '_' . pathinfo($partFileName, PATHINFO_FILENAME) . '.' . $imgExt;\n            $this->zipClass->pclzipAddFile($imgPath, 'word/media/' . $imgName);\n            $this->tempDocumentNewImages[$imgPath] = $imgName;\n\n            // setup type for image\n            $xmlImageType = str_replace(['{IMG}', '{EXT}'], [$imgName, $imgExt], $typeTpl);\n            $this->tempDocumentContentTypes = str_replace('</Types>', $xmlImageType, $this->tempDocumentContentTypes) . '</Types>';\n        }\n\n        $xmlImageRelation = str_replace(['{RID}', '{IMG}'], [$rid, $imgName], $relationTpl);\n\n        if (!isset($this->tempDocumentRelations[$partFileName])) {\n            // create new relations file\n            $this->tempDocumentRelations[$partFileName] = $newRelationsTpl;\n            // and add it to content types","sourceCodeStart":594,"sourceCodeEnd":630,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/TemplateProcessor.php#L594-L630","documentation":"TemplateProcessor::addImageToRelations maps the detected image MIME type to a file extension via an $extTransform whitelist; if the MIME type is not in the map it throws \"Unsupported image type $imageMimeType\". Only image types that can be embedded in OOXML media with a known extension are supported.","triggerScenarios":"setImageValue with an image whose MIME type is not one of the supported set (e.g. image/webp, image/svg+xml, image/bmp, image/tiff depending on the transform map).","commonSituations":"Uploading modern WebP or SVG images and injecting them into a .docx template; images served/saved with unusual MIME types; camera images in TIFF format.","solutions":["Convert the image to JPEG or PNG before embedding (e.g. with GD or Imagick)","Use only jpg/png/gif sources for template images","Normalize the image's MIME type on upload/processing pipelines so it matches the whitelist"],"exampleFix":"// before\n$template->setImageValue('${photo}', ['path' => 'photo.webp']); // throws\n// after\n$im = imagecreatefromwebp('photo.webp');\nimagejpeg($im, 'photo.jpg', 90);\n$template->setImageValue('${photo}', ['path' => 'photo.jpg']);","handlingStrategy":"validation","validationCode":"$mime = (new finfo(FILEINFO_MIME_TYPE))->file($imgPath);\n$allowed = ['image/jpeg','image/png','image/gif'];\nif (!in_array($mime, $allowed, true)) {\n    $imgPath = convertToJpeg($imgPath); // GD/Imagick conversion\n}","typeGuard":"function isEmbeddableImageType(string $mime): bool {\n    return in_array($mime, ['image/jpeg','image/png','image/gif'], true);\n}","tryCatchPattern":"try {\n    $template->setImageValue('${photo}', ['path' => $imgPath]);\n} catch (\\PhpOffice\\PhpWord\\Exception\\Exception $e) {\n    if (str_contains($e->getMessage(), 'Unsupported image type')) {\n        // convert to JPEG/PNG and retry\n    }\n}","preventionTips":["Normalize uploaded images to JPEG or PNG during ingest","Detect MIME with finfo, not the file extension","Pre-convert WebP/SVG/TIFF/BMP before embedding in docx templates"],"tags":["image","unsupported-type","template"],"backgroundTag":"unsupported-operation","analyzedSha":"aef95c04151b5633cc505f672ddd6a71da900ee1","analyzedAt":"2026-09-14T10:53:58.933Z","contentChangedAt":"2026-09-14T10:53:58.933Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}