-
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
addHtml() Img tag support #601
Comments
I would like to see this as well. |
Edit the html.php and go to the parseNode Function. Add to the nodes array: 'img' => array('Image', $node, $element, $styles, $data, null, null), And add the parseImage Function /**
* Parse image node
*
* @param \DOMNode $node
* @param \PhpOffice\PhpWord\Element\AbstractContainer $element
* @param array &$styles
* @return \PhpOffice\PhpWord\Element\TextRun
*
**/
private static function parseImage($node, $element, &$styles, $data)
{
$style=array();
foreach ($node->attributes as $attribute) {
switch ($attribute->name) {
case 'src':
$src=$attribute->value;
break;
case 'width':
$width=$attribute->value;
$style['width']=$width;
break;
case 'height':
$height=$attribute->value;
$style['height']=$height;
break;
case 'style':
$styleattr=explode(';', $attribute->value);
foreach ($styleattr as $attr) {
if (strpos($attr, ':')) {
list($k, $v) = explode(':', $attr);
switch ($k) {
case 'float':
if (trim($v)=='right') {
$style['hPos']=\PhpOffice\PhpWord\Style\Image::POS_RIGHT;
$style['hPosRelTo']=\PhpOffice\PhpWord\Style\Image::POS_RELTO_PAGE;
$style['pos']=\PhpOffice\PhpWord\Style\Image::POS_RELATIVE;
$style['wrap']=\PhpOffice\PhpWord\Style\Image::WRAP_TIGHT;
$style['overlap']=true;
}
if (trim($v)=='left') {
$style['hPos']=\PhpOffice\PhpWord\Style\Image::POS_LEFT;
$style['hPosRelTo']=\PhpOffice\PhpWord\Style\Image::POS_RELTO_PAGE;
$style['pos']=\PhpOffice\PhpWord\Style\Image::POS_RELATIVE;
$style['wrap']=\PhpOffice\PhpWord\Style\Image::WRAP_TIGHT;
$style['overlap']=true;
}
break;
}
}
}
break;
}
}
$newElement = $element->addImage($src, $style);
return $newElement;
} |
@Ryuzakix3 Your solution worked for me. Thank you very much |
@Ryuzakix3 Is there any way to make this work with images that are already saved in base64 format? I have a big HTML block containing various elements and the base64 images are not rendered not matter what I do. |
@Ryuzakix3 super your solution |
It throws an error for external images. eg. https://via.placeholder.com/150
I suspected the cause in Html.php, regex is used to verify source.
Can you help me in this. I want to write html having similar images, but it throws an error. I did small change as below. Will it be valid?
|
it is possible that the addHtml function supports img tags and when find a img tag then add the image ?
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
The text was updated successfully, but these errors were encountered: