Skip to content

Commit

Permalink
New method to test if a File Object is a PHP temporary file buffer
Browse files Browse the repository at this point in the history
Also, changing name of ImageObject's "getSize()" to "getImageSize()" so
that it doesn't collide with SplFileInfo's "getSize()" method (so we can
still use it)
  • Loading branch information
Rican7 committed May 29, 2013
1 parent ddf329a commit b51de1b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 22 deletions.
7 changes: 7 additions & 0 deletions FileManager/FileObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,13 @@ public function testIsWrappedHex($file_object)
$this->assertTrue($file_object->isWrappedHex());
}

public function testIsTemp()
{
$file_object = new FileObject('php://temp', 'r+');

$this->assertTrue($file_object->isTemp());
}

public function testGetRaw()
{
$test_file_jpg = $this->getTestFileByBaseName('photo.jpg');
Expand Down
4 changes: 2 additions & 2 deletions FileManager/ImageObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public function testGetSize()
{
$image_obj = new ImageObject($this->getTestFile());

$this->assertCount(7, $image_obj->getSize());
$this->assertCount(7, $image_obj->getImageSize());

return $image_obj->getSize();
return $image_obj->getImageSize();
}

/**
Expand Down
25 changes: 24 additions & 1 deletion FileObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ class FileObject extends SplFileObject
*/
const DATA_WRAPPER_REGEX = '/^([A-Za-z0-9]+):[\/]*(.*?)(?:;(.*))?,/';

/**
* The regular expression used to check if a wrapper is a PHP temporary file
*
* @const string
*/
const TEMP_WRAPPER_REGEX = '/^(php):(\/\/)?(memory|temp)/';

/**
* The default hashing algorithm to use for hashing methods
*
Expand Down Expand Up @@ -456,7 +463,7 @@ public function detectMimeType()

$finfo = new finfo(FILEINFO_MIME_TYPE);

return ($this->isWrapped() ?
return (($this->isWrapped() || $this->isTemp()) ?
$finfo->buffer($this->getRaw())
: $finfo->file($this->getPathname())
);
Expand Down Expand Up @@ -579,6 +586,22 @@ public function isWrappedHex()
return false;
}

/**
* Quick check to see if the file object is a PHP
* temporary file
*
* @access public
* @return boolean
*/
public function isTemp()
{
// Only get the first 100 characters of the string,
// as it could be a full hex representation of a file
$string = substr($this->getPathname(), 0, 100);

return (bool) preg_match(static::TEMP_WRAPPER_REGEX, $string);
}

/**
* Get the file's data as a string
*
Expand Down
38 changes: 19 additions & 19 deletions ImageObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct($filename, $open_mode = 'r', $use_include_path = fal
{
parent::__construct($filename, $open_mode, $use_include_path, $context);

if (!$this->isImage()) {
if (!$this->isImage() && !$this->isTemp()) {
throw new UnexpectedValueException('File is not an image');
}
}
Expand Down Expand Up @@ -65,9 +65,9 @@ public function getImageResource()
* @access public
* @return array
*/
public function getSize()
public function getImageSize()
{
return ($this->isWrapped() ?
return (($this->isWrapped() || $this->isTemp()) ?
getimagesizefromstring($this->getRaw())
: getimagesize($this->getPathname())
);
Expand All @@ -79,47 +79,47 @@ public function getSize()
*/

/**
* Alias of $this->getSize();
* Alias of $this->getImageSize();
*
* @see ImageObject::getSize()
* @access public
* @return array
*/
public function getMeta()
{
return $this->getSize();
return $this->getImageSize();
}

/**
* Alias of $this->getSize()[0];
* Alias of $this->getImageSize()[0];
*
* @see ImageObject::getSize()
* @access public
* @return int
*/
public function getWidth()
{
$size = $this->getSize();
$size = $this->getImageSize();

return $size[0];
}

/**
* Alias of $this->getSize()[1];
* Alias of $this->getImageSize()[1];
*
* @see ImageObject::getSize()
* @access public
* @return int
*/
public function getHeight()
{
$size = $this->getSize();
$size = $this->getImageSize();

return $size[1];
}

/**
* Alias of $this->getSize()[2];
* Alias of $this->getImageSize()[2];
*
* Get the PHP IMAGETYPE_XXX constant
*
Expand All @@ -129,13 +129,13 @@ public function getHeight()
*/
public function getType()
{
$size = $this->getSize();
$size = $this->getImageSize();

return $size[2];
}

/**
* Alias of $this->getSize()[3];
* Alias of $this->getImageSize()[3];
*
* Get the dimensions as an HTML compatible string
* ex: `width="120" height="120"`
Expand All @@ -146,13 +146,13 @@ public function getType()
*/
public function getDimensionsString()
{
$size = $this->getSize();
$size = $this->getImageSize();

return $size[3];
}

/**
* Alias of $this->getSize()['mime'];
* Alias of $this->getImageSize()['mime'];
*
* Get the MIME-type of the image based on PHP's detection
*
Expand All @@ -162,13 +162,13 @@ public function getDimensionsString()
*/
public function getMime()
{
$size = $this->getSize();
$size = $this->getImageSize();

return $size['mime'];
}

/**
* Alias of $this->getSize()['channels'];
* Alias of $this->getImageSize()['channels'];
*
* Get the RGB/CMYK channels of the image
*
Expand All @@ -178,13 +178,13 @@ public function getMime()
*/
public function getChannels()
{
$size = $this->getSize();
$size = $this->getImageSize();

return $size['channels'];
}

/**
* Alias of $this->getSize()['bits'];
* Alias of $this->getImageSize()['bits'];
*
* Get the bit depth of the image
*
Expand All @@ -194,7 +194,7 @@ public function getChannels()
*/
public function getBits()
{
$size = $this->getSize();
$size = $this->getImageSize();

return $size['bits'];
}
Expand Down

0 comments on commit b51de1b

Please sign in to comment.