-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: Support EMF image #1480
Comments
PHPWord uses getimagesize() function to get image info, getimagesize() doesn't support emf format. 😂😂 |
I using phpword: dev-master and see error
|
Any news on this issue? Will this be addressed sooner or later? |
I encountered this error just now. I guess EMF format is becoming more commonly used in modern docx files |
The same problem for me today. Any news about this issue ? |
There isn't any support for .emf file but there is a workaround
|
Workaround by code : include 'vendor/autoload.php';
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('test2.docx');
$templateProcessor->setValue('name', 'myvar');
$templateProcessor->saveAs('./xx.docx'); https://phpword.readthedocs.io/en/latest/templates-processing.html You can avoid using TemplateProcessing as your need is only to replace .emf referencesYou may write a Use PHP ZipArchive to extract "YOURDOC.docx\word_rels\document.xml.rels" Replace EMF references in file Use PHP ZipArchive to zip document.xml.rels back Use PHP ZipArchive to extract emf file Use ImageMagick to convert the EMF FILE Use PHP ZipArchive to zip jpeg file back |
Workaround that worked for me private function removeImageReferences($zip, $placeholderImagePath)
{
$relsPath = 'word/_rels/document.xml.rels';
$relsContent = $zip->getFromName($relsPath);
$relsXml = new SimpleXMLElement($relsContent);
$imagePaths = [];
foreach ($relsXml->Relationship as $relationship) {
if (strpos($relationship['Type'], 'image') !== false) {
// Store the original image path
$imagePaths[] = 'word/' . $relationship['Target'];
// Replace the image target with a placeholder image reference
$placeholderImageTarget = 'media/placeholder.png';
$relationship['Target'] = $placeholderImageTarget;
}
}
// Update the relationships file
$zip->deleteName($relsPath);
$zip->addFromString($relsPath, $relsXml->asXML());
// Delete the original image files
foreach ($imagePaths as $imagePath) {
$zip->deleteName($imagePath);
}
// Add the placeholder image to the zip archive
$zip->addFile($placeholderImagePath, 'word/' . $placeholderImageTarget);
}
private function getPlaceholderImage()
{
$placeholderImagePath = 'placeholder.png';
if (!Storage::disk('local')->exists($placeholderImagePath)) {
$width = 1;
$height = 1;
$color = [255, 255, 255]; // RGB value for white color
$image = imagecreatetruecolor($width, $height);
$color = imagecolorallocate($image, $color[0], $color[1], $color[2]);
imagefilledrectangle($image, 0, 0, $width - 1, $height - 1, $color);
ob_start();
imagepng($image);
$imageData = ob_get_contents();
ob_end_clean();
Storage::disk('local')->put($placeholderImagePath, $imageData);
}
return storage_path('app/' . $placeholderImagePath);
} Then $tempFilePath = tempnam(sys_get_temp_dir(), 'doc');
file_put_contents($tempFilePath, $response->getBody()->getContents());
$zip = new ZipArchive();
$placeholderImagePath = $this->getPlaceholderImage();
$zip->open($tempFilePath);
$this->removeImageReferences($zip, $placeholderImagePath);
$zip->close();
$phpWord = IOFactory::load($tempFilePath); |
In the unlikely event that this is going to be fixed at anytime soon due to what seems to be poor support of EMF images with PHP, is it worth catching this error and replacing the image with a placeholder 'can't be found image/message'? Then, at least the library can be used for any documents which use an EMF image. |
So, PHP getimagesize and getimagesizefromstring accept the following formats It is not including emf file (neither svg...). So this could be a PHP Feature Request, but in the meantime, we could try to implement it "PHP like" on PHPWord. In Php code: /* {{{ Get the size of an image as 4-element array */ It then get the stream, and call To know which kind of file it is, it call then php_getimagesize_from_stream For each kind of defined type, it check a specific number of bytes, and then the corresponding content. For example, for jpeg, the 3 first bytes should be Then it apply a image type specific function to get the related image size. For example, for PSD image type; "static struct gfxinfo *php_handle_psd (php_stream * stream)
}" Or for BMP file
}" So, we could implement a glue, that can rely on the file name (.xxx) or on the first byte definition for EMF, and then retrieve the related content from the specification. More precisely And then Then in 2.2.9 Which get us in |
Hi Progi1984, I hadn't the time to install the whole environment to be able to test looking to the project standards, but i wrote a glue for getimagesize that is working on my environment. As the specification is a little bit painful, i copy below the function, hoping it could help you in managing this ticket. "/** |
But this only solve the CheckImage Problem. There is also another problem on parseImage on PhpWord/Shared/Html.php on line 960 |
My Bad, the image type should also be modified |
I got around this a year ago, this never bothered me again. |
Well, EMF to JPEG is not a lossless conversion. That's why i updated PHPWord to manage emf image. But you're right that if you don't mind about image quality, your solution is a good workaround. |
Someone has a file with EMF/WMF file, please ? |
I have one, but it is my customer one, so it can't be used like that. So i used the trial version of the Metafile Companion Software, and then produce a random image that i inserted on a random docx file. |
Hope it helps |
This is:
Expected Behavior
Support EMF image.
Failure Information
Throws PhpOffice\PhpWord\Exception\InvalidImageException exception.
Exception message :
Invalid image: zip:///Users/xxx/Downloads/xxxx.docx#word/media/image.emf
#0 /works/shared/laravel/vendor/phpoffice/phpword/src/PhpWord/Element/Image.php(149): PhpOffice\PhpWord\Element\Image->checkImage()
#1 [internal function]: PhpOffice\PhpWord\Element\Image->__construct('zip:///Users/hu...', NULL, false, 'Picture 18')
How to Reproduce
Document file contains emf format images.
Google emf I got this page: https://fileinfo.com/extension/emf
Context
The text was updated successfully, but these errors were encountered: