Image.php 11.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
<?php
/*
    PHPRtfLite
    Copyright 2007-2008 Denis Slaveckij <sinedas@gmail.com>
    Copyright 2010-2012 Steffen Zeidler <sigma_z@sigma-scripts.de>

    This file is part of PHPRtfLite.

    PHPRtfLite is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    PHPRtfLite is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with PHPRtfLite.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Class for displaying images in rtf documents.
 * @version     1.2
 * @author      Denis Slaveckij <sinedas@gmail.com>
 * @author      Steffen Zeidler <sigma_z@sigma-scripts.de>
 * @copyright   2007-2008 Denis Slaveckij, 2010-2012 Steffen Zeidler
 * @package     PHPRtfLite
 */
abstract class PHPRtfLite_Image
{

    /**
     * class constants for image type
     */
    const TYPE_JPEG = 'jpeg';
    const TYPE_PNG  = 'png';
    const TYPE_WMF  = 'wmf';

    /**
     * rtf document
     * @var PHPRtfLite
     */
    protected $_rtf;
    /**
     * stream as php resource
     * @var resource
     */
    protected $_stream;
    /**
     * par format
     * @var PHPRtfLite_ParFormat
     */
    protected $_parFormat;

    /**
     * rtf border
     * @var PHPRtfLite_Border
     */
    protected $_border;

    /**
     * resize to width
     * @var float
     */
    protected $_width;
    /**
     * resize to height
     * @var float
     */
    protected $_height;
    /**
     * original image width
     * @var float
     */
    protected $_imageWidth;
    /**
     * original image height
     * @var integer
     */
    protected $_imageHeight;
    /**
     * image rtf type
     * @var string
     */
    protected $_imageRtfType;
    /**
     * flag, true if image file is missing
     * @var type
     */
    protected $_isMissing = false;


    /**
     * constructor
     *
     * @param PHPRtfLite            $rtf
     * @param resource              $stream
     * @param float                 $width  optional
     * @param float                 $height optional
     */
    public function __construct(PHPRtfLite $rtf, $stream, $width = null, $height = null)
    {
        $this->_rtf         = $rtf;
        $this->_stream      = $stream;
        $this->_width       = $width;
        $this->_height      = $height;
    }


    /**
     * destructor - closes stream
     */
    public function __destruct()
    {
        fclose($this->_stream);
    }


    /**
     * sets paragraph format for image
     *
     * @param PHPRtfLite_ParFormat $parFormat
     */
    public function setParFormat(PHPRtfLite_ParFormat $parFormat)
    {
        $this->_parFormat = $parFormat;
        $parFormat->setColorTable($this->_rtf->getColorTable());
    }


    /**
     * gets paragraph format for image
     *
     * @return PHPRtfLite_ParFormat
     */
    public function getParFormat()
    {
        return $this->_parFormat;
    }


    /**
     * sets image width
     *
     * @param   float   $width  if not defined image is displayed by it's height.
     */
    public function setWidth($width)
    {
        $this->_width = $width;
    }


    /**
     * gets image width
     *
     * @return float
     */
    public function getWidth()
    {
        return $this->_width;
    }


    /**
     * sets image height
     *
     * @param   float   $height if not defined image is displayed by it's width.
     */
    public function setHeight($height)
    {
        $this->_height = $height;
    }


    /**
     * gets image height
     *
     * @return float
     */
    public function getHeight()
    {
        return $this->_height;
    }


    /**
     * sets border
     *
     * @param PHPRtfLite_Border $border
     */
    public function setBorder(PHPRtfLite_Border $border)
    {
        $this->_border = $border;
    }


    /**
     * gets border
     *
     * @return PHPRtfLite_Border
     */
    public function getBorder()
    {
        return $this->_border;
    }


    public function isMissing()
    {
        return $this->_isMissing;
    }


    public function setIsMissing()
    {
        $this->_isMissing = true;
    }


    /**
     * gets rtf image width
     *
     * @return integer
     */
    private function getImageRtfWidth()
    {
        if ($this->_width > 0) {
            return PHPRtfLite_Unit::getUnitInTwips($this->_width);
        }

        $imageWidth = $this->_imageWidth ? $this->_imageWidth : 100;
        if ($this->_height > 0) {
            $imageHeight = $this->_imageHeight ? $this->_imageHeight : 100;
            $width = ($imageWidth / $imageHeight) * $this->_height;
            return PHPRtfLite_Unit::getUnitInTwips($width);
        }

        return PHPRtfLite_Unit::getPointsInTwips($imageWidth);
    }


    /**
     * gets rtf image height
     *
     * @return integer
     */
    private function getImageRtfHeight()
    {
        if ($this->_height > 0) {
            return PHPRtfLite_Unit::getUnitInTwips($this->_height);
        }

        $imageHeight = $this->_imageHeight ? $this->_imageHeight : 100;
        if ($this->_width > 0) {
            $imageWidth = $this->_imageWidth ? $this->_imageWidth : 100;
            $height = ($imageHeight /$imageWidth) * $this->_width;
            return PHPRtfLite_Unit::getUnitInTwips($height);
        }

        return PHPRtfLite_Unit::getPointsInTwips($imageHeight);
    }


    /**
     * adds rtf image code to show that the file is missing
     */
    private static function getMissingImage()
    {
        $string = 'iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAIAAAAC64paAAAAAXNSR0IArs4c6QAAAAlwSFlzAAALEwAA'
                . 'CxMBAJqcGAAAAAd0SU1FB9gMGgoaGog16G8AAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBX'
                . 'gQ4XAAAAfUlEQVQ4y62TsQ3AIAwEnRcSc2T/eVIzR7p0yAn+N4K4xD49cHBctl7FzM57hWzVsBHs4Fat'
                . '1TzNz2BsC5Im95OHfF/0F/RKZnxIBtseeUYG8IcXZAyPo+wh/ORZWGH+oK1of0h9Ch4zPhmPlBQ8tCTd'
                . 'KjMYm9nyXPQ31PUAECcxtbZxAkYAAAAASUVORK5CYII=';

        return $string;
    }


    /**
     * sets image original width
     *
     * @param float $width
     */
    protected function setImageWidth($width)
    {
        $this->_imageWidth = $width;
    }

    /**
     * sets image original height
     *
     * @param float $height
     */
    protected function setImageHeight($height)
    {
        $this->_imageHeight = $height;
    }


    /**
     * creates rtf image from file
     *
     * @param   PHPRtfLite              $rtf
     * @param   string                  $file
     * @param   float                   $width  optional
     * @param   float                   $height optional
     * @return  PHPRtfLite_Image
     */
    public static function createFromFile(PHPRtfLite $rtf, $file, $width = null, $height = null)
    {
        if (file_exists($file) && is_readable($file)) {
            $stream = fopen($file, 'rb');
            $pathInfo = pathinfo($file);
            $type = isset($pathInfo['extension']) ? strtolower($pathInfo['extension']) : 'jpeg';
            $image = self::create($rtf, $stream, $type, $width, $height);

            if ($type != self::TYPE_WMF) {
                list($width, $height, $imageType) = getimagesize($file);
                $imageType = image_type_to_extension($imageType, false);
                $image->setImageWidth($width);
                $image->setImageHeight($height);
                if ($type != $imageType) {
                    $image->setImageType($imageType);
                }
            }
            return $image;
        }

        return self::createMissingImage($rtf, $width, $height);
    }


    /**
     * creates rtf image from string
     *
     * @param   PHPRtfLite              $rtf
     * @param   string                  $string
     * @param   string                  $type   (represented by class constants)
     * @param   float                   $width  optional
     * @param   float                   $height optional
     * @return  PHPRtfLite_Image
     */
    public static function createFromString(PHPRtfLite $rtf, $string, $type, $width = null, $height = null)
    {
        $stream = fopen('data://text/plain;base64,' . base64_encode($string), 'rb');
        $image = self::create($rtf, $stream, $type, $width, $height);
        if ($type != self::TYPE_WMF) {
            $imageResource = imagecreatefromstring($string);
            $image->setImageWidth(imagesx($imageResource));
            $image->setImageHeight(imagesy($imageResource));
        }
        return $image;
    }


    /**
     * factory method
     * creates rtf image from stream
     *
     * @param   PHPRtfLite              $rtf
     * @param   resource                $stream
     * @param   string                  $type   (represented by class constants)
     * @param   float                   $width  optional
     * @param   float                   $height optional
     * @return  PHPRtfLite_Image
     */
    private static function create(PHPRtfLite $rtf, $stream, $type, $width, $height)
    {
        switch ($type) {
            case self::TYPE_WMF:
                return new PHPRtfLite_Image_Wmf($rtf, $stream, $width, $height);
            default:
                $image = new PHPRtfLite_Image_Gd($rtf, $stream, $width, $height);
                $image->setImageType($type);
                return $image;
        }
    }


    /**
     * creates a missing image instance
     *
     * @param   PHPRtfLite  $rtf
     * @param   float       $width
     * @param   float       $height
     * @return  PHPRtfLite_Image_Gd
     */
    private static function createMissingImage($rtf, $width, $height)
    {
        $stream = fopen('data://text/plain;base64,' . self::getMissingImage(), 'rb');
        $image = new PHPRtfLite_Image_Gd($rtf, $stream, $width, $height);
        $image->setImageType(self::TYPE_PNG);
        $image->setIsMissing();
        return $image;
    }


    /**
     * renders rtf image for rtf document
     */
    public function render()
    {
        $this->writeIntoRtfStream();
    }


    /**
     * writes image into rtf stream
     *
     * @param integer $startFrom
     */
    protected function writeIntoRtfStream($startFrom = 0)
    {
        fseek($this->_stream, $startFrom);
        $rtfImageType = $this->getImageTypeAsRtf();

        $rtfStream = $this->_rtf->getWriter();
        $rtfStream->write('{\*\shppict {\pict');

        if ($this->_border) {
            $rtfStream->write($this->_border->getContent());
        }

        $rtfStream->write($rtfImageType . '\picscalex100\picscaley100');
        $rtfStream->write('\picwgoal' . $this->getImageRtfWidth());
        $rtfStream->write('\pichgoal' . $this->getImageRtfHeight());
        $rtfStream->write(' ');

        while (!feof($this->_stream)) {
            $stringBuffer = fread($this->_stream, 1024);
            $stringHex = bin2hex($stringBuffer);
            $rtfStream->write($stringHex);
        }

        $rtfStream->write('}}');
    }


    /**
     * gets image rtf type
     *
     * @return string
     */
    protected function getImageTypeAsRtf()
    {
        return $this->_imageRtfType;
    }

}