本文整理汇总了PHP中PHPWord_Media类的典型用法代码示例。如果您正苦于以下问题:PHP PHPWord_Media类的具体用法?PHP PHPWord_Media怎么用?PHP PHPWord_Media使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PHPWord_Media类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: addWatermark
/**
* Add a Watermark Element
*
* @param string $src
* @param mixed $style
* @return PHPWord_Section_Image
*/
public function addWatermark($src, $style = null)
{
$image = new PHPWord_Section_Image($src, $style, true);
if (!is_null($image->getSource())) {
$rID = PHPWord_Media::addHeaderMediaElement($this->_headerCount, $src);
$image->setRelationId($rID);
$this->_elementCollection[] = $image;
return $image;
} else {
trigger_error('Src does not exist or invalid image type.', E_ERROR);
}
}
示例2: save
public function save($pFilename = null)
{
if (!is_null($this->_document)) {
// If $pFilename is php://output or php://stdout, make it a temporary file...
$originalFilename = $pFilename;
if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
$pFilename = @tempnam('./', 'phppttmp');
if ($pFilename == '') {
$pFilename = $originalFilename;
}
}
// Create new ZIP file and open it for writing
$objZip = new ZipArchive();
// Try opening the ZIP file
if ($objZip->open($pFilename, ZIPARCHIVE::OVERWRITE) !== true) {
if ($objZip->open($pFilename, ZIPARCHIVE::CREATE) !== true) {
throw new Exception("Could not open " . $pFilename . " for writing.");
}
}
$sectionElements = array();
$_secElements = PHPWord_Media::getSectionMediaElements();
foreach ($_secElements as $element) {
// loop through section media elements
if ($element['type'] != 'hyperlink') {
$this->_addFileToPackage($objZip, $element);
}
$sectionElements[] = $element;
}
$_hdrElements = PHPWord_Media::getHeaderMediaElements();
foreach ($_hdrElements as $_headerFile => $_hdrMedia) {
// loop through headers
if (count($_hdrMedia) > 0) {
$objZip->addFromString('word/_rels/' . $_headerFile . '.xml.rels', $this->getWriterPart('documentrels')->writeHeaderFooterRels($_hdrMedia));
foreach ($_hdrMedia as $element) {
// loop through header media elements
$this->_addFileToPackage($objZip, $element);
}
}
}
$_ftrElements = PHPWord_Media::getFooterMediaElements();
foreach ($_ftrElements as $_footerFile => $_ftrMedia) {
// loop through footers
if (count($_ftrMedia) > 0) {
$objZip->addFromString('word/_rels/' . $_footerFile . '.xml.rels', $this->getWriterPart('documentrels')->writeHeaderFooterRels($_ftrMedia));
foreach ($_ftrMedia as $element) {
// loop through footers media elements
$this->_addFileToPackage($objZip, $element);
}
}
}
$_cHdrs = 0;
$_cFtrs = 0;
$rID = PHPWord_Media::countSectionMediaElements() + 6;
$_sections = $this->_document->getSections();
foreach ($_sections as $section) {
$_header = $section->getHeader();
if (!is_null($_header)) {
$_cHdrs++;
$_header->setRelationId(++$rID);
$_headerCount = $_header->getHeaderCount();
$_headerFile = 'header' . $_headerCount . '.xml';
$sectionElements[] = array('target' => $_headerFile, 'type' => 'header', 'rID' => $rID);
$objZip->addFromString('word/' . $_headerFile, $this->getWriterPart('header')->writeHeader($_header));
}
$_footer = $section->getFooter();
if (!is_null($_footer)) {
$_cFtrs++;
$_footer->setRelationId(++$rID);
$_footerCount = $_footer->getFooterCount();
$_footerFile = 'footer' . $_footerCount . '.xml';
$sectionElements[] = array('target' => $_footerFile, 'type' => 'footer', 'rID' => $rID);
$objZip->addFromString('word/' . $_footerFile, $this->getWriterPart('footer')->writeFooter($_footer));
}
}
// build docx file
// Write dynamic files
$objZip->addFromString('[Content_Types].xml', $this->getWriterPart('contenttypes')->writeContentTypes($this->_imageTypes, $this->_objectTypes, $_cHdrs, $_cFtrs));
$objZip->addFromString('_rels/.rels', $this->getWriterPart('rels')->writeRelationships($this->_document));
$objZip->addFromString('docProps/app.xml', $this->getWriterPart('docprops')->writeDocPropsApp($this->_document));
$objZip->addFromString('docProps/core.xml', $this->getWriterPart('docprops')->writeDocPropsCore($this->_document));
$objZip->addFromString('word/document.xml', $this->getWriterPart('document')->writeDocument($this->_document));
$objZip->addFromString('word/_rels/document.xml.rels', $this->getWriterPart('documentrels')->writeDocumentRels($sectionElements));
$objZip->addFromString('word/styles.xml', $this->getWriterPart('styles')->writeStyles($this->_document));
// Write static files
$objZip->addFile(PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/numbering.xml', 'word/numbering.xml');
$objZip->addFile(PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/settings.xml', 'word/settings.xml');
$objZip->addFile(PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/theme1.xml', 'word/theme/theme1.xml');
$objZip->addFile(PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/webSettings.xml', 'word/webSettings.xml');
$objZip->addFile(PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/fontTable.xml', 'word/fontTable.xml');
// Close file
if ($objZip->close() === false) {
throw new Exception("Could not close zip file {$pFilename}.");
}
// If a temporary file was used, copy it to the correct file stream
if ($originalFilename != $pFilename) {
if (copy($pFilename, $originalFilename) === false) {
throw new Exception("Could not copy temporary zip file {$pFilename} to {$originalFilename}.");
}
@unlink($pFilename);
}
//.........这里部分代码省略.........
示例3: addMemoryImage
/**
* Add a by PHP created Image Element
*
* @param string $link
* @param mixed $style
* @return PHPWord_Section_MemoryImage
*/
public function addMemoryImage($link, $style = null)
{
$memoryImage = new PHPWord_Section_MemoryImage($link, $style);
if (!is_null($memoryImage->getSource())) {
$rID = PHPWord_Media::addSectionMediaElement($link, 'image', $memoryImage);
$memoryImage->setRelationId($rID);
$this->_elementCollection[] = $memoryImage;
return $memoryImage;
} else {
trigger_error('Unsupported image type.');
}
}
示例4: addLink
/**
* Add a Link Element
*
* @param string $linkSrc
* @param string $linkName
* @param mixed $styleFont
* @return PHPWord_Section_Link
*/
public function addLink($linkSrc, $linkName = null, $styleFont = null)
{
$linkSrc = utf8_encode($linkSrc);
if (!is_null($linkName)) {
$linkName = utf8_encode($linkName);
}
$link = new PHPWord_Section_Link($linkSrc, $linkName, $styleFont);
$rID = PHPWord_Media::addSectionLinkElement($linkSrc);
$link->setRelationId($rID);
$this->_elementCollection[] = $link;
return $link;
}
示例5: addObject
/**
* Add a OLE-Object Element
*
* @param string $src
* @param mixed $style
* @return PHPWord_Section_Object
*/
public function addObject($src, $style = null)
{
$object = new PHPWord_Section_Object($src, $style);
if (!is_null($object->getSource())) {
$inf = pathinfo($src);
$ext = $inf['extension'];
if (strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') {
$ext = substr($ext, 0, -1);
}
$iconSrc = PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/';
if (!file_exists($iconSrc . '_' . $ext . '.png')) {
$iconSrc = $iconSrc . '_default.png';
} else {
$iconSrc .= '_' . $ext . '.png';
}
$rIDimg = PHPWord_Media::addSectionMediaElement($iconSrc, 'image');
$data = PHPWord_Media::addSectionMediaElement($src, 'oleObject');
$rID = $data[0];
$objectId = $data[1];
$object->setRelationId($rID);
$object->setObjectId($objectId);
$object->setImageRelationId($rIDimg);
$this->_elementCollection[] = $object;
return $object;
} else {
trigger_error('Source does not exist or unsupported object type.');
}
}
示例6: offlinequiz_create_docx_question
/**
* Generates the DOCX question/correction form for an offlinequiz group.
*
* @param question_usage_by_activity $templateusage the template question usage for this offline group
* @param object $offlinequiz The offlinequiz object
* @param object $group the offline group object
* @param int $courseid the ID of the Moodle course
* @param object $context the context of the offline quiz.
* @param boolean correction if true the correction form is generated.
* @return stored_file instance, the generated DOCX file.
*/
function offlinequiz_create_docx_question(question_usage_by_activity $templateusage, $offlinequiz, $group, $courseid, $context, $correction = false)
{
global $CFG, $DB, $OUTPUT;
$letterstr = 'abcdefghijklmnopqrstuvwxyz';
$groupletter = strtoupper($letterstr[$group->number - 1]);
$coursecontext = context_course::instance($courseid);
PHPWord_Media::resetMedia();
$docx = new PHPWord();
$trans = new offlinequiz_html_translator();
// Define cell style arrays.
$cellstyle = array('valign' => 'center');
// Add text styles.
// Normal style.
$docx->addFontStyle('nStyle', array('size' => $offlinequiz->fontsize));
// Italic style.
$docx->addFontStyle('iStyle', array('italic' => true, 'size' => $offlinequiz->fontsize));
// Bold style.
$docx->addFontStyle('bStyle', array('bold' => true, 'size' => $offlinequiz->fontsize));
$docx->addFontStyle('brStyle', array('bold' => true, 'align' => 'right', 'size' => $offlinequiz->fontsize));
// Underline style.
$docx->addFontStyle('uStyle', array('underline' => PHPWord_Style_Font::UNDERLINE_SINGLE, 'size' => $offlinequiz->fontsize));
$docx->addFontStyle('ibStyle', array('italic' => true, 'bold' => true, 'size' => $offlinequiz->fontsize));
$docx->addFontStyle('iuStyle', array('italic' => true, 'underline' => PHPWord_Style_Font::UNDERLINE_SINGLE, 'size' => $offlinequiz->fontsize));
$docx->addFontStyle('buStyle', array('bold' => true, 'underline' => PHPWord_Style_Font::UNDERLINE_SINGLE, 'size' => $offlinequiz->fontsize));
$docx->addFontStyle('ibuStyle', array('italic' => true, 'bold' => true, 'underline' => PHPWord_Style_Font::UNDERLINE_SINGLE, 'size' => $offlinequiz->fontsize));
// Header style.
$docx->addFontStyle('hStyle', array('bold' => true, 'size' => $offlinequiz->fontsize + 4));
// Center style.
$docx->addParagraphStyle('cStyle', array('align' => 'center', 'spaceAfter' => 100));
$docx->addParagraphStyle('cStyle', array('align' => 'center', 'spaceAfter' => 100));
$docx->addParagraphStyle('questionTab', array('tabs' => array(new PHPWord_Style_Tab("left", 360))));
// Define table style arrays.
$tablestyle = array('borderSize' => 0, 'borderColor' => 'FFFFFF', 'cellMargin' => 20, 'align' => 'center');
$firstrowstyle = array('borderBottomSize' => 0, 'borderBottomColor' => 'FFFFFF', 'bgColor' => 'FFFFFF');
$docx->addTableStyle('tableStyle', $tablestyle, $firstrowstyle);
$boldfont = new PHPWord_Style_Font();
$boldfont->setBold(true);
$boldfont->setSize($offlinequiz->fontsize);
$normalfont = new PHPWord_Style_Font();
$normalfont->setSize($offlinequiz->fontsize);
// Define custom list item style for question answers.
$level1 = new PHPWord_Style_Paragraph();
$level1->setTabs(new PHPWord_Style_Tabs(array(new PHPWord_Style_Tab('clear', 720), new PHPWord_Style_Tab('num', 360))));
$level1->setIndentions(new PHPWord_Style_Indentation(array('left' => 360, 'hanging' => 360)));
$level2 = new PHPWord_Style_Paragraph();
$level2->setTabs(new PHPWord_Style_Tabs(array(new PHPWord_Style_Tab('left', 720), new PHPWord_Style_Tab('num', 720))));
$level2->setIndentions(new PHPWord_Style_Indentation(array('left' => 720, 'hanging' => 360)));
// Create the section that will be used for all outputs.
$section = $docx->createSection();
$title = offlinequiz_str_html_docx($offlinequiz->name);
if (!empty($offlinequiz->time)) {
if (strlen($title) > 35) {
$title = substr($title, 0, 33) . ' ...';
}
$title .= ": " . offlinequiz_str_html_docx(userdate($offlinequiz->time));
} else {
if (strlen($title) > 40) {
$title = substr($title, 0, 37) . ' ...';
}
}
$title .= ", " . offlinequiz_str_html_docx(get_string('group') . " {$groupletter}");
// Add a header.
$header = $section->createHeader();
$header->addText($title, array('size' => 10), 'cStyle');
$header->addImage($CFG->dirroot . '/mod/offlinequiz/pix/line.png', array('width' => 600, 'height' => 5, 'align' => 'center'));
// Add a footer.
$footer = $section->createFooter();
$footer->addImage($CFG->dirroot . '/mod/offlinequiz/pix/line.png', array('width' => 600, 'height' => 5, 'align' => 'center'));
$footer->addPreserveText($title . ' | ' . get_string('page') . ' ' . '{PAGE} / {NUMPAGES}', null, array('align' => 'left'));
// Print title page.
if (!$correction) {
$section->addText(offlinequiz_str_html_docx(get_string('questionsheet', 'offlinequiz') . ' - ' . get_string('group') . " {$groupletter}"), 'hStyle', 'cStyle');
$section->addTextBreak(2);
$table = $section->addTable('tableStyle');
$table->addRow();
$cell = $table->addCell(200, $cellstyle)->addText(offlinequiz_str_html_docx(get_string('name')) . ': ', 'brStyle');
$table->addRow();
$cell = $table->addCell(200, $cellstyle)->addText(offlinequiz_str_html_docx(get_string('idnumber', 'offlinequiz')) . ': ', 'brStyle');
$table->addRow();
$cell = $table->addCell(200, $cellstyle)->addText(offlinequiz_str_html_docx(get_string('studycode', 'offlinequiz')) . ': ', 'brStyle');
$table->addRow();
$cell = $table->addCell(200, $cellstyle)->addText(offlinequiz_str_html_docx(get_string('signature', 'offlinequiz')) . ': ', 'brStyle');
$section->addTextBreak(2);
// The DOCX intro text can be arbitrarily long so we have to catch page overflows.
if (!empty($offlinequiz->pdfintro)) {
$blocks = offlinequiz_convert_image_docx($offlinequiz->pdfintro);
offlinequiz_print_blocks_docx($section, $blocks);
}
$section->addPageBreak();
//.........这里部分代码省略.........
示例7: resetMedia
public static function resetMedia()
{
self::$_sectionMedia = array('images' => array(), 'embeddings' => array(), 'links' => array());
self::$_headerMedia = array();
self::$_footerMedia = array();
}