2014-03-22 10:06:08 +04:00
|
|
|
<?php
|
|
|
|
|
/**
|
2014-03-26 16:33:20 +07:00
|
|
|
* PHPWord
|
2014-03-22 10:06:08 +04:00
|
|
|
*
|
2014-03-27 23:55:06 +07:00
|
|
|
* @link https://github.com/PHPOffice/PHPWord
|
|
|
|
|
* @copyright 2014 PHPWord
|
|
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
2014-03-22 10:06:08 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace PhpOffice\PhpWord;
|
|
|
|
|
|
2014-03-31 01:13:02 +07:00
|
|
|
use PhpOffice\PhpWord\Element\Image;
|
2014-03-22 10:06:08 +04:00
|
|
|
|
2014-03-24 00:26:10 +07:00
|
|
|
/**
|
|
|
|
|
* Media
|
|
|
|
|
*/
|
2014-03-22 10:06:08 +04:00
|
|
|
class Media
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Section Media Elements
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2014-04-01 15:01:30 +07:00
|
|
|
private static $sectionMedia = array(
|
2014-03-23 10:32:08 +04:00
|
|
|
'images' => array(),
|
2014-03-22 10:06:08 +04:00
|
|
|
'embeddings' => array(),
|
2014-03-23 10:32:08 +04:00
|
|
|
'links' => array()
|
2014-03-22 10:06:08 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Header Media Elements
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2014-04-01 15:01:30 +07:00
|
|
|
private static $headerMedia = array();
|
2014-03-22 10:06:08 +04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Footer Media Elements
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2014-04-01 15:01:30 +07:00
|
|
|
private static $footerMedia = array();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Media elements
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
private static $media = array();
|
2014-03-22 10:06:08 +04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ObjectID Counter
|
|
|
|
|
*
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
2014-04-01 15:01:30 +07:00
|
|
|
private static $objectId = 1325353440;
|
2014-03-22 10:06:08 +04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add new Section Media Element
|
|
|
|
|
*
|
2014-03-23 10:32:08 +04:00
|
|
|
* @param string $src
|
|
|
|
|
* @param string $type
|
2014-04-01 15:01:30 +07:00
|
|
|
* @param Image $image
|
2014-03-22 10:06:08 +04:00
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2014-03-24 16:24:50 +07:00
|
|
|
public static function addSectionMediaElement($src, $type, Image $image = null)
|
2014-03-22 10:06:08 +04:00
|
|
|
{
|
|
|
|
|
$mediaId = md5($src);
|
|
|
|
|
$key = ($type === 'image') ? 'images' : 'embeddings';
|
2014-04-01 15:01:30 +07:00
|
|
|
if (!array_key_exists($mediaId, self::$sectionMedia[$key])) {
|
2014-03-22 10:06:08 +04:00
|
|
|
$cImg = self::countSectionMediaElements('images');
|
|
|
|
|
$cObj = self::countSectionMediaElements('embeddings');
|
|
|
|
|
$rID = self::countSectionMediaElements() + 7;
|
|
|
|
|
$media = array();
|
|
|
|
|
$folder = null;
|
|
|
|
|
$file = null;
|
|
|
|
|
if ($type === 'image') {
|
|
|
|
|
$cImg++;
|
2014-03-28 13:50:53 +07:00
|
|
|
$isMemImage = false;
|
2014-03-24 16:24:50 +07:00
|
|
|
if (!is_null($image)) {
|
|
|
|
|
$isMemImage = $image->getIsMemImage();
|
|
|
|
|
$extension = $image->getImageExtension();
|
2014-03-22 10:06:08 +04:00
|
|
|
}
|
|
|
|
|
if ($isMemImage) {
|
|
|
|
|
$media['isMemImage'] = true;
|
2014-03-24 16:24:50 +07:00
|
|
|
$media['createfunction'] = $image->getImageCreateFunction();
|
|
|
|
|
$media['imagefunction'] = $image->getImageFunction();
|
2014-03-22 10:06:08 +04:00
|
|
|
}
|
|
|
|
|
$folder = 'media';
|
|
|
|
|
$file = $type . $cImg . '.' . strtolower($extension);
|
|
|
|
|
} elseif ($type === 'oleObject') {
|
|
|
|
|
$cObj++;
|
|
|
|
|
$folder = 'embedding';
|
|
|
|
|
$file = $type . $cObj . '.bin';
|
|
|
|
|
}
|
|
|
|
|
$media['source'] = $src;
|
|
|
|
|
$media['target'] = "$folder/section_$file";
|
|
|
|
|
$media['type'] = $type;
|
|
|
|
|
$media['rID'] = $rID;
|
2014-04-01 15:01:30 +07:00
|
|
|
self::$sectionMedia[$key][$mediaId] = $media;
|
2014-03-22 10:06:08 +04:00
|
|
|
if ($type === 'oleObject') {
|
2014-04-01 15:01:30 +07:00
|
|
|
return array($rID, ++self::$objectId);
|
2014-03-22 10:06:08 +04:00
|
|
|
}
|
|
|
|
|
return $rID;
|
2014-03-24 16:24:50 +07:00
|
|
|
} else {
|
|
|
|
|
if ($type === 'oleObject') {
|
2014-04-01 15:01:30 +07:00
|
|
|
$rID = self::$sectionMedia[$key][$mediaId]['rID'];
|
|
|
|
|
return array($rID, ++self::$objectId);
|
2014-03-24 16:24:50 +07:00
|
|
|
}
|
2014-04-01 15:01:30 +07:00
|
|
|
return self::$sectionMedia[$key][$mediaId]['rID'];
|
2014-03-22 10:06:08 +04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add new Section Link Element
|
|
|
|
|
*
|
|
|
|
|
* @param string $linkSrc
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public static function addSectionLinkElement($linkSrc)
|
|
|
|
|
{
|
|
|
|
|
$rID = self::countSectionMediaElements() + 7;
|
|
|
|
|
|
|
|
|
|
$link = array();
|
|
|
|
|
$link['target'] = $linkSrc;
|
|
|
|
|
$link['rID'] = $rID;
|
|
|
|
|
$link['type'] = 'hyperlink';
|
|
|
|
|
|
2014-04-01 15:01:30 +07:00
|
|
|
self::$sectionMedia['links'][] = $link;
|
2014-03-22 10:06:08 +04:00
|
|
|
|
|
|
|
|
return $rID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get Section Media Elements
|
|
|
|
|
*
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function getSectionMediaElements($key = null)
|
|
|
|
|
{
|
|
|
|
|
if (!is_null($key)) {
|
2014-04-01 15:01:30 +07:00
|
|
|
return self::$sectionMedia[$key];
|
2014-03-22 10:06:08 +04:00
|
|
|
}
|
|
|
|
|
|
2014-04-01 15:01:30 +07:00
|
|
|
$arrImages = self::$sectionMedia['images'];
|
|
|
|
|
$arrObjects = self::$sectionMedia['embeddings'];
|
|
|
|
|
$arrLinks = self::$sectionMedia['links'];
|
2014-03-22 10:06:08 +04:00
|
|
|
return array_merge($arrImages, $arrObjects, $arrLinks);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get Section Media Elements Count
|
|
|
|
|
*
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public static function countSectionMediaElements($key = null)
|
|
|
|
|
{
|
|
|
|
|
if (!is_null($key)) {
|
2014-04-01 15:01:30 +07:00
|
|
|
return count(self::$sectionMedia[$key]);
|
2014-03-22 10:06:08 +04:00
|
|
|
}
|
|
|
|
|
|
2014-04-01 15:01:30 +07:00
|
|
|
$cImages = count(self::$sectionMedia['images']);
|
|
|
|
|
$cObjects = count(self::$sectionMedia['embeddings']);
|
|
|
|
|
$cLinks = count(self::$sectionMedia['links']);
|
2014-03-22 10:06:08 +04:00
|
|
|
return ($cImages + $cObjects + $cLinks);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add new Header Media Element
|
|
|
|
|
*
|
2014-03-23 10:32:08 +04:00
|
|
|
* @param int $headerCount
|
|
|
|
|
* @param string $src
|
2014-04-01 15:01:30 +07:00
|
|
|
* @param Image $image
|
2014-03-22 10:06:08 +04:00
|
|
|
* @return int
|
|
|
|
|
*/
|
2014-03-24 16:24:50 +07:00
|
|
|
public static function addHeaderMediaElement($headerCount, $src, Image $image = null)
|
2014-03-22 10:06:08 +04:00
|
|
|
{
|
2014-04-01 15:01:30 +07:00
|
|
|
return self::addMediaElement("header{$headerCount}", 'image', $src, $image);
|
2014-03-22 10:06:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get Header Media Elements Count
|
|
|
|
|
*
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public static function countHeaderMediaElements($key)
|
|
|
|
|
{
|
2014-04-01 15:01:30 +07:00
|
|
|
return self::countMediaElements($key);
|
2014-03-22 10:06:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get Header Media Elements
|
|
|
|
|
*
|
2014-04-01 15:01:30 +07:00
|
|
|
* @return array
|
2014-03-22 10:06:08 +04:00
|
|
|
*/
|
2014-04-01 15:01:30 +07:00
|
|
|
public static function getHeaderMediaElements($prefix = 'header')
|
2014-03-22 10:06:08 +04:00
|
|
|
{
|
2014-04-01 15:01:30 +07:00
|
|
|
$mediaCollection = array();
|
|
|
|
|
if (!empty(self::$media)) {
|
|
|
|
|
foreach (self::$media as $key => $val) {
|
|
|
|
|
if (substr($key, 0, 6) == $prefix) {
|
|
|
|
|
$mediaCollection[$key] = $val;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $mediaCollection;
|
2014-03-22 10:06:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add new Footer Media Element
|
|
|
|
|
*
|
2014-03-23 10:32:08 +04:00
|
|
|
* @param int $footerCount
|
|
|
|
|
* @param string $src
|
2014-04-01 15:01:30 +07:00
|
|
|
* @param Image $image
|
2014-03-22 10:06:08 +04:00
|
|
|
* @return int
|
|
|
|
|
*/
|
2014-03-24 16:24:50 +07:00
|
|
|
public static function addFooterMediaElement($footerCount, $src, Image $image = null)
|
2014-03-22 10:06:08 +04:00
|
|
|
{
|
2014-04-01 15:01:30 +07:00
|
|
|
return self::addMediaElement("footer{$footerCount}", 'image', $src, $image);
|
2014-03-22 10:06:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get Footer Media Elements Count
|
|
|
|
|
*
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public static function countFooterMediaElements($key)
|
|
|
|
|
{
|
2014-04-01 15:01:30 +07:00
|
|
|
return self::countMediaElements($key);
|
2014-03-22 10:06:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get Footer Media Elements
|
|
|
|
|
*
|
2014-04-01 15:01:30 +07:00
|
|
|
* @return array
|
2014-03-22 10:06:08 +04:00
|
|
|
*/
|
|
|
|
|
public static function getFooterMediaElements()
|
|
|
|
|
{
|
2014-04-01 15:01:30 +07:00
|
|
|
return self::getHeaderMediaElements('footer');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add new media element
|
|
|
|
|
*
|
|
|
|
|
* @param string $container section|header|footer|footnotes
|
|
|
|
|
* @param string $mediaType image|embedding|hyperlink
|
|
|
|
|
* @param string $source
|
|
|
|
|
* @param Image $image
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public static function addMediaElement($container, $mediaType, $source, Image $image = null)
|
|
|
|
|
{
|
|
|
|
|
// Assign media Id and initiate media container if none exists
|
|
|
|
|
$mediaId = md5($source);
|
|
|
|
|
if (!array_key_exists($container, self::$media)) {
|
|
|
|
|
self::$media[$container]= array();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add media if not exists or point to existing media
|
|
|
|
|
if (!array_key_exists($mediaId, self::$media[$container])) {
|
|
|
|
|
$mediaCount = self::countMediaElements($container);
|
|
|
|
|
$mediaTypeCount = self::countMediaElements($container, $mediaType);
|
|
|
|
|
$mediaData = array();
|
|
|
|
|
$relId = $mediaCount + 1;
|
|
|
|
|
$mediaTypeCount++;
|
|
|
|
|
if ($mediaType == 'image') {
|
|
|
|
|
$isMemImage = false;
|
|
|
|
|
if (!is_null($image)) {
|
|
|
|
|
$isMemImage = $image->getIsMemImage();
|
|
|
|
|
$extension = $image->getImageExtension();
|
|
|
|
|
}
|
|
|
|
|
if ($isMemImage) {
|
|
|
|
|
$mediaData['isMemImage'] = true;
|
|
|
|
|
$mediaData['createfunction'] = $image->getImageCreateFunction();
|
|
|
|
|
$mediaData['imagefunction'] = $image->getImageFunction();
|
|
|
|
|
}
|
|
|
|
|
$file = 'image' . $mediaTypeCount . '.' . strtolower($extension);
|
|
|
|
|
if ($container != 'footnotes') {
|
|
|
|
|
$file = $container . '_' . $file;
|
|
|
|
|
}
|
|
|
|
|
$target = 'media/' . $file;
|
|
|
|
|
} elseif ($mediaType == 'hyperlink') {
|
|
|
|
|
$target = $source;
|
|
|
|
|
}
|
|
|
|
|
$mediaData['source'] = $source;
|
|
|
|
|
$mediaData['target'] = $target;
|
|
|
|
|
$mediaData['type'] = $mediaType;
|
|
|
|
|
$mediaData['rID'] = $relId;
|
|
|
|
|
self::$media[$container][$mediaId] = $mediaData;
|
|
|
|
|
|
|
|
|
|
return $relId;
|
|
|
|
|
} else {
|
|
|
|
|
return self::$media[$container][$mediaId]['rID'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get media elements count
|
|
|
|
|
*
|
|
|
|
|
* @param string $container
|
|
|
|
|
* @param string $mediaType
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public static function countMediaElements($container, $mediaType = null)
|
|
|
|
|
{
|
|
|
|
|
$mediaCount = 0;
|
|
|
|
|
foreach (self::$media[$container] as $mediaKey => $mediaData) {
|
|
|
|
|
if (!is_null($mediaType)) {
|
|
|
|
|
if ($mediaType == $mediaData['type']) {
|
|
|
|
|
$mediaCount++;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$mediaCount++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $mediaCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get media elements
|
|
|
|
|
*
|
|
|
|
|
* @param string $container
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public static function getMediaElements($container, $mediaType = null)
|
|
|
|
|
{
|
|
|
|
|
if (!array_key_exists($container, self::$media)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$mediaElements = array();
|
|
|
|
|
foreach (self::$media[$container] as $mediaKey => $mediaData) {
|
|
|
|
|
if (!is_null($mediaType)) {
|
|
|
|
|
if ($mediaType == $mediaData['type']) {
|
|
|
|
|
$mediaElements[$mediaKey] = $mediaData;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$mediaElements[$mediaKey] = $mediaData;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $mediaElements;
|
2014-03-22 10:06:08 +04:00
|
|
|
}
|
|
|
|
|
}
|