Skip to content

Commit

Permalink
Added ability to work with image data streams. Added new example as w…
Browse files Browse the repository at this point in the history
…ell. This closes masterexploder#2
  • Loading branch information
masterexploder committed Oct 27, 2009
1 parent c0f780b commit d9d1a1f
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 15 deletions.
64 changes: 53 additions & 11 deletions GdThumb.inc.php
Expand Up @@ -91,12 +91,16 @@ class GdThumb extends ThumbBase
* @return GdThumb
* @param string $fileName
*/
public function __construct ($fileName, $options = array())
public function __construct ($fileName, $options = array(), $isDataStream = false)
{
parent::__construct($fileName);
parent::__construct($fileName, $isDataStream);

$this->determineFormat();
$this->verifyFormatCompatiblity();

if ($this->isDataStream === false)
{
$this->verifyFormatCompatiblity();
}

switch ($this->format)
{
Expand All @@ -109,13 +113,15 @@ public function __construct ($fileName, $options = array())
case 'PNG':
$this->oldImage = imagecreatefrompng($this->fileName);
break;
case 'STRING':
$this->oldImage = imagecreatefromstring($this->fileName);
break;
}

$size = getimagesize($this->fileName);

$this->currentDimensions = array
(
'width' => $size[0],
'height' => $size[1]
'width' => imagesx($this->oldImage),
'height' => imagesy($this->oldImage)
);

$this->setOptions($options);
Expand Down Expand Up @@ -553,9 +559,10 @@ public function rotateImageNDegrees ($degrees)
* for the format, and then outputting the image data. If headers have already been sent,
* a runtime exception will be thrown
*
* @param bool $rawData Whether or not the raw image stream should be output
* @return GdThumb
*/
public function show ()
public function show ($rawData = false)
{
if (headers_sent())
{
Expand All @@ -565,22 +572,51 @@ public function show ()
switch ($this->format)
{
case 'GIF':
header('Content-type: image/gif');
if ($rawData === false)
{
header('Content-type: image/gif');
}
imagegif($this->oldImage);
break;
case 'JPG':
header('Content-type: image/jpeg');
if ($rawData === false)
{
header('Content-type: image/jpeg');
}
imagejpeg($this->oldImage, null, $this->options['jpegQuality']);
break;
case 'PNG':
header('Content-type: image/png');
case 'STRING':
if ($rawData === false)
{
header('Content-type: image/png');
}
imagepng($this->oldImage);
break;
}

return $this;
}

/**
* Returns the Working Image as a String
*
* This function is useful for getting the raw image data as a string for storage in
* a database, or other similar things.
*
* @return string
*/
public function getImageAsString ()
{
$data = null;
ob_start();
$this->show(true);
$data = ob_get_contents();
ob_end_clean();

return $data;
}

/**
* Saves an image
*
Expand Down Expand Up @@ -1025,6 +1061,12 @@ protected function calcImageSizePercent ($width, $height)
*/
protected function determineFormat ()
{
if ($this->isDataStream === true)
{
$this->format = 'STRING';
return;
}

$formatInfo = getimagesize($this->fileName);

// non-image files will return false
Expand Down
17 changes: 16 additions & 1 deletion ThumbBase.inc.php
Expand Up @@ -82,20 +82,30 @@ abstract class ThumbBase
* @var bool
*/
protected $remoteImage;
/**
* Whether or not the current image is an actual file, or the raw file data
*
* By "raw file data" it's meant that we're actually passing the result of something
* like file_get_contents() or perhaps from a database blob
*
* @var bool
*/
protected $isDataStream;

/**
* Class constructor
*
* @return ThumbBase
*/
public function __construct ($fileName)
public function __construct ($fileName, $isDataStream = false)
{
$this->imported = array();
$this->importedFunctions = array();
$this->errorMessage = null;
$this->hasError = false;
$this->fileName = $fileName;
$this->remoteImage = false;
$this->isDataStream = $isDataStream;

$this->fileExistsAndReadable();
}
Expand Down Expand Up @@ -146,6 +156,11 @@ protected function imports ($object)
*/
protected function fileExistsAndReadable ()
{
if ($this->isDataStream === true)
{
return;
}

if (stristr($this->fileName, 'http://') !== false)
{
$this->remoteImage = true;
Expand Down
6 changes: 3 additions & 3 deletions ThumbLib.inc.php
Expand Up @@ -103,7 +103,7 @@ class PhpThumbFactory
* @uses PhpThumb
* @param string $filename The path and file to load [optional]
*/
public static function create ($filename = null, $options = array())
public static function create ($filename = null, $options = array(), $isDataStream = false)
{
// map our implementation to their class names
$implementationMap = array
Expand All @@ -124,14 +124,14 @@ public static function create ($filename = null, $options = array())
if ($pt->isValidImplementation(self::$defaultImplemenation))
{
$imp = $implementationMap[self::$defaultImplemenation];
$toReturn = new $imp($filename, $options);
$toReturn = new $imp($filename, $options, $isDataStream);
}
// load the gd implementation if default failed
else if ($pt->isValidImplementation('gd'))
{
$imp = $implementationMap['gd'];
$implementation = 'gd';
$toReturn = new $imp($filename, $options);
$toReturn = new $imp($filename, $options, $isDataStream);
}
// throw an exception if we can't load
else
Expand Down
42 changes: 42 additions & 0 deletions examples/crop_basic_fromstring.php
@@ -0,0 +1,42 @@
<?php
/**
* PhpThumb Library Example File
*
* This file contains example usage for the PHP Thumb Library
*
* PHP Version 5 with GD 2.0+
* PhpThumb : PHP Thumb Library <http://phpthumb.gxdlabs.com>
* Copyright (c) 2009, Ian Selby/Gen X Design
*
* Author(s): Ian Selby <ian@gen-x-design.com>
*
* Licensed under the MIT License
* Redistributions of files must retain the above copyright notice.
*
* @author Ian Selby <ian@gen-x-design.com>
* @copyright Copyright (c) 2009 Gen X Design
* @link http://phpthumb.gxdlabs.com
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @version 3.0
* @package PhpThumb
* @subpackage Examples
* @filesource
*/

require_once '../ThumbLib.inc.php';

$fileData = file_get_contents('test.jpg');

$thumb = PhpThumbFactory::create($fileData, array(), true);
$thumb->crop(100, 100, 300, 200);

// $imageAsString will contain the image data suitable for saving in a database.
$imageAsString = $thumb->getImageAsString();

?>
<h2>Here's the Image Data:</h2>
<strong>Note:</strong> This should be a bunch of gibberish<br />
<div style="overflow: auto; width: 500px; height: 400px; border: 1px solid #e4e4e4; padding: 5px;"><?php echo htmlentities($imageAsString); ?></div>

<h2>Here's that data as an image:</h2>
<img src="data:image/png;base64,<?php echo base64_encode($imageAsString); ?>" />

0 comments on commit d9d1a1f

Please sign in to comment.