Skip to content

Commit

Permalink
Cache: adding cacheData (see #39)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregwar committed Oct 21, 2013
1 parent c3b0d9d commit 930f57e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
16 changes: 15 additions & 1 deletion Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public function __call($methodName, $args)
return $this;
}

throw new \BadFunctionCallException('Invalid method: '.$func);
throw new \BadFunctionCallException('Invalid method: '.$methodName);
}

/**
Expand Down Expand Up @@ -340,6 +340,9 @@ public function getHash($type = 'guess', $quality = 80)
* Gets the cache file name and generate it if it does not exists.
* Note that if it exists, all the image computation process will
* not be done.
*
* @param $type the image type
* @param $quality the quality (for JPEG)
*/
public function cacheFile($type = 'jpg', $quality = 80)
{
Expand All @@ -366,6 +369,17 @@ public function cacheFile($type = 'jpg', $quality = 80)
return $this->getFilename($file);
}

/**
* Get cache data (to render the image)
*
* @param $type the image type
* @param $quality the quality (for JPEG)
*/
public function cacheData($type = 'jpg', $quality = 80)
{
return file_get_contents($this->cacheFile($type, $quality));
}

/**
* Hook to helps to extends and enhance this class
*/
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
],
"require": {
"gregwar/cache": "v1.0.2",
"gregwar/cache": "v1.0.3",
"php": ">=5.3.0",
"ext-gd": "*"
},
Expand Down
11 changes: 11 additions & 0 deletions tests/ImageTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@ public function testActualCache()
$this->assertTrue(file_exists($file));
}

public function testCacheData()
{
$output = $this->open('monalisa.jpg')
->resize(300, 200)
->cacheData();

$jpg = imagecreatefromstring($output);
$this->assertEquals(300, imagesx($jpg));
$this->assertEquals(200, imagesy($jpg));
}

/**
* Testing using cache
*/
Expand Down

0 comments on commit 930f57e

Please sign in to comment.