public function html(){ // 创建一个新的Word文档 $phpWord = new PhpWord(); // 添加一个新的空白页 $section = $phpWord->addSection(); // 添加文本到文档 $text = '标题:'.$value['title']; $section->addText($text); $section->addTextBreak(); if ($this->isImageLinkValid($value['img'])) { // 添加图片到文档 $imageSrc = $value['img']; // 图片路径 // 设置图片的宽度和高度 // 通过百分比设置大小 $section->addImage( $imageSrc, array( 'width' => 200, // 图片宽度百分比 'height' => 200, // 图片高度百分比 'alignment' => Image::POSITION_VERTICAL_CENTER, 'ratio' => false // 设置为false保持图片原始宽高比 ) ); } $title = trim($value['title']); $title = str_replace('|', '', $title); $wordName = $title.'_'.date('YmdHis').'_'.$value['id']; $content = strip_tags($value['content']); $content = html_entity_decode($content); // 将HTML内容添加到文档中 $section->addText($content); // 保存Word文档 $objWriter = IOFactory::createWriter($phpWord, 'HTML'); $savePath = "./word/{$wordName}.html"; // 指定保存路径 $objWriter->save($savePath); var_dump($wordName.'_文档已创建_'.$value['id']); }
protected function isImageLinkValid($url) { // 获取图片链接的所有响应头信息 $headers = @get_headers($url); // 如果无法获取响应头或者响应头第0位不是HTTP/1.1 200 OK,则认为链接无效 if (!is_array($headers) || strpos($headers[0], '200 OK') === false) { return false; } return true; }