Refactor Element\Image and some samples
This commit is contained in:
parent
04b14ea42d
commit
9c738f7eae
|
|
@ -60,4 +60,8 @@ echo date('H:i:s'), " Write to Word2007 format", EOL;
|
||||||
$document->saveAs($name);
|
$document->saveAs($name);
|
||||||
rename($name, "results/{$name}");
|
rename($name, "results/{$name}");
|
||||||
|
|
||||||
include_once 'Sample_Footer.php';
|
$writers = array('Word2007' => 'docx');
|
||||||
|
echo getEndingNotes($writers);
|
||||||
|
if (!CLI) {
|
||||||
|
include_once 'Sample_Footer.php';
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,4 +18,8 @@ echo date('H:i:s'), " Write to Word2007 format", EOL;
|
||||||
$document->saveAs($name);
|
$document->saveAs($name);
|
||||||
rename($name, "results/{$name}");
|
rename($name, "results/{$name}");
|
||||||
|
|
||||||
include_once 'Sample_Footer.php';
|
$writers = array('Word2007' => 'docx');
|
||||||
|
echo getEndingNotes($writers);
|
||||||
|
if (!CLI) {
|
||||||
|
include_once 'Sample_Footer.php';
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -46,18 +46,17 @@ if ($handle = opendir('.')) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get results
|
* Write documents
|
||||||
*
|
*
|
||||||
* @param \PhpOffice\PhpWord\PhpWord $phpWord
|
* @param \PhpOffice\PhpWord\PhpWord $phpWord
|
||||||
* @param string $filename
|
* @param string $filename
|
||||||
* @param array $writers
|
* @param array $writers
|
||||||
* @return string
|
|
||||||
*/
|
*/
|
||||||
function write($phpWord, $filename, $writers)
|
function write($phpWord, $filename, $writers)
|
||||||
{
|
{
|
||||||
$result = '';
|
$result = '';
|
||||||
|
|
||||||
// Write
|
// Write documents
|
||||||
foreach ($writers as $writer => $extension) {
|
foreach ($writers as $writer => $extension) {
|
||||||
$result .= date('H:i:s') . " Write to {$writer} format";
|
$result .= date('H:i:s') . " Write to {$writer} format";
|
||||||
if (!is_null($extension)) {
|
if (!is_null($extension)) {
|
||||||
|
|
@ -70,6 +69,20 @@ function write($phpWord, $filename, $writers)
|
||||||
$result .= EOL;
|
$result .= EOL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$result .= getEndingNotes($writers);
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get ending notes
|
||||||
|
*
|
||||||
|
* @param array $writers
|
||||||
|
*/
|
||||||
|
function getEndingNotes($writers)
|
||||||
|
{
|
||||||
|
$result = '';
|
||||||
|
|
||||||
// Do not show execution time for index
|
// Do not show execution time for index
|
||||||
if (!IS_INDEX) {
|
if (!IS_INDEX) {
|
||||||
$result .= date('H:i:s') . " Done writing file(s)" . EOL;
|
$result .= date('H:i:s') . " Done writing file(s)" . EOL;
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,13 @@ use PhpOffice\PhpWord\Style\Image as ImageStyle;
|
||||||
*/
|
*/
|
||||||
class Image extends AbstractElement
|
class Image extends AbstractElement
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Image source type constants
|
||||||
|
*/
|
||||||
|
const SOURCE_LOCAL = 'local'; // Local images
|
||||||
|
const SOURCE_GD = 'gd'; // Generated using GD
|
||||||
|
const SOURCE_ARCHIVE = 'archive'; // Image in archives zip://$archive#$image
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Image source
|
* Image source
|
||||||
*
|
*
|
||||||
|
|
@ -25,6 +32,13 @@ class Image extends AbstractElement
|
||||||
*/
|
*/
|
||||||
private $source;
|
private $source;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Source type: local|gd|archive
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $sourceType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Image style
|
* Image style
|
||||||
*
|
*
|
||||||
|
|
@ -199,89 +213,51 @@ class Image extends AbstractElement
|
||||||
*/
|
*/
|
||||||
private function checkImage($source)
|
private function checkImage($source)
|
||||||
{
|
{
|
||||||
$isArchive = strpos($source, 'zip://') !== false;
|
$this->setSourceType($source);
|
||||||
|
|
||||||
// Check is memory image
|
// Check image data
|
||||||
if (stripos(strrev($source), strrev('.php')) === 0) {
|
if ($this->sourceType == self::SOURCE_ARCHIVE) {
|
||||||
$this->isMemImage = true;
|
$imageData = $this->getArchiveImageSize($source);
|
||||||
} elseif ($isArchive) {
|
|
||||||
$this->isMemImage = false;
|
|
||||||
} else {
|
|
||||||
$this->isMemImage = (filter_var($source, FILTER_VALIDATE_URL) !== false);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Define supported types
|
|
||||||
if ($this->isMemImage) {
|
|
||||||
$supportedTypes = array(
|
|
||||||
IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_PNG
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
$supportedTypes = array(
|
|
||||||
IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_PNG,
|
|
||||||
IMAGETYPE_BMP, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check from zip file or actual file
|
|
||||||
if ($isArchive) {
|
|
||||||
$imageData = $this->getArchivedImageSize($source);
|
|
||||||
} else {
|
} else {
|
||||||
$imageData = @getimagesize($source);
|
$imageData = @getimagesize($source);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if image exists by detecting image data
|
|
||||||
if (!is_array($imageData)) {
|
if (!is_array($imageData)) {
|
||||||
throw new InvalidImageException();
|
throw new InvalidImageException();
|
||||||
}
|
}
|
||||||
// Put image data into variables
|
|
||||||
list($actualWidth, $actualHeight, $imageType) = $imageData;
|
list($actualWidth, $actualHeight, $imageType) = $imageData;
|
||||||
// Check if image type is supported
|
|
||||||
|
// Check image type support
|
||||||
|
$supportedTypes = array(IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_PNG);
|
||||||
|
if ($this->sourceType != self::SOURCE_GD) {
|
||||||
|
$supportedTypes = array_merge($supportedTypes, array(IMAGETYPE_BMP, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM));
|
||||||
|
}
|
||||||
if (!in_array($imageType, $supportedTypes)) {
|
if (!in_array($imageType, $supportedTypes)) {
|
||||||
throw new UnsupportedImageTypeException();
|
throw new UnsupportedImageTypeException();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define image functions
|
// Define image functions
|
||||||
$this->imageType = image_type_to_mime_type($imageType);
|
$this->imageType = image_type_to_mime_type($imageType);
|
||||||
switch ($this->imageType) {
|
$this->setFunctions();
|
||||||
case 'image/png':
|
$this->setProportionalSize($actualWidth, $actualHeight);
|
||||||
$this->imageCreateFunc = 'imagecreatefrompng';
|
}
|
||||||
$this->imageFunc = 'imagepng';
|
|
||||||
$this->imageExtension = 'png';
|
|
||||||
break;
|
|
||||||
case 'image/gif':
|
|
||||||
$this->imageCreateFunc = 'imagecreatefromgif';
|
|
||||||
$this->imageFunc = 'imagegif';
|
|
||||||
$this->imageExtension = 'gif';
|
|
||||||
break;
|
|
||||||
case 'image/jpeg':
|
|
||||||
$this->imageCreateFunc = 'imagecreatefromjpeg';
|
|
||||||
$this->imageFunc = 'imagejpeg';
|
|
||||||
$this->imageExtension = 'jpg';
|
|
||||||
break;
|
|
||||||
case 'image/bmp':
|
|
||||||
case 'image/x-ms-bmp':
|
|
||||||
$this->imageType = 'image/bmp';
|
|
||||||
$this->imageExtension = 'bmp';
|
|
||||||
break;
|
|
||||||
case 'image/tiff':
|
|
||||||
$this->imageExtension = 'tif';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check image width & height
|
/**
|
||||||
$styleWidth = $this->style->getWidth();
|
* Set source type
|
||||||
$styleHeight = $this->style->getHeight();
|
*
|
||||||
if (!($styleWidth && $styleHeight)) {
|
* @param string $source
|
||||||
if ($styleWidth == null && $styleHeight == null) {
|
*/
|
||||||
$this->style->setWidth($actualWidth);
|
private function setSourceType($source)
|
||||||
$this->style->setHeight($actualHeight);
|
{
|
||||||
} elseif ($styleWidth) {
|
if (stripos(strrev($source), strrev('.php')) === 0) {
|
||||||
$this->style->setHeight($actualHeight * ($styleWidth / $actualWidth));
|
$this->isMemImage = true;
|
||||||
} else {
|
$this->sourceType = self::SOURCE_GD;
|
||||||
$this->style->setWidth($actualWidth * ($styleHeight / $actualHeight));
|
} elseif (strpos($source, 'zip://') !== false) {
|
||||||
}
|
$this->isMemImage = false;
|
||||||
|
$this->sourceType = self::SOURCE_ARCHIVE;
|
||||||
|
} else {
|
||||||
|
$this->isMemImage = (filter_var($source, FILTER_VALIDATE_URL) !== false);
|
||||||
|
$this->sourceType = $this->isMemImage ? self::SOURCE_GD : self::SOURCE_LOCAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -290,7 +266,7 @@ class Image extends AbstractElement
|
||||||
* @param string $source
|
* @param string $source
|
||||||
* @return array|null
|
* @return array|null
|
||||||
*/
|
*/
|
||||||
private function getArchivedImageSize($source)
|
private function getArchiveImageSize($source)
|
||||||
{
|
{
|
||||||
$imageData = null;
|
$imageData = null;
|
||||||
$source = substr($source, 6);
|
$source = substr($source, 6);
|
||||||
|
|
@ -312,4 +288,56 @@ class Image extends AbstractElement
|
||||||
|
|
||||||
return $imageData;
|
return $imageData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set image functions and extensions
|
||||||
|
*/
|
||||||
|
private function setFunctions()
|
||||||
|
{
|
||||||
|
switch ($this->imageType) {
|
||||||
|
case 'image/png':
|
||||||
|
$this->imageCreateFunc = 'imagecreatefrompng';
|
||||||
|
$this->imageFunc = 'imagepng';
|
||||||
|
$this->imageExtension = 'png';
|
||||||
|
break;
|
||||||
|
case 'image/gif':
|
||||||
|
$this->imageCreateFunc = 'imagecreatefromgif';
|
||||||
|
$this->imageFunc = 'imagegif';
|
||||||
|
$this->imageExtension = 'gif';
|
||||||
|
break;
|
||||||
|
case 'image/jpeg':
|
||||||
|
case 'image/jpg':
|
||||||
|
$this->imageCreateFunc = 'imagecreatefromjpeg';
|
||||||
|
$this->imageFunc = 'imagejpeg';
|
||||||
|
$this->imageExtension = 'jpg';
|
||||||
|
break;
|
||||||
|
case 'image/bmp':
|
||||||
|
case 'image/x-ms-bmp':
|
||||||
|
$this->imageType = 'image/bmp';
|
||||||
|
$this->imageExtension = 'bmp';
|
||||||
|
break;
|
||||||
|
case 'image/tiff':
|
||||||
|
$this->imageExtension = 'tif';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set proportional width/height if one dimension not available
|
||||||
|
*/
|
||||||
|
private function setProportionalSize($actualWidth, $actualHeight)
|
||||||
|
{
|
||||||
|
$styleWidth = $this->style->getWidth();
|
||||||
|
$styleHeight = $this->style->getHeight();
|
||||||
|
if (!($styleWidth && $styleHeight)) {
|
||||||
|
if ($styleWidth == null && $styleHeight == null) {
|
||||||
|
$this->style->setWidth($actualWidth);
|
||||||
|
$this->style->setHeight($actualHeight);
|
||||||
|
} elseif ($styleWidth) {
|
||||||
|
$this->style->setHeight($actualHeight * ($styleWidth / $actualWidth));
|
||||||
|
} else {
|
||||||
|
$this->style->setWidth($actualWidth * ($styleHeight / $actualHeight));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue