Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/getgrav/grav into 1.5
Browse files Browse the repository at this point in the history
# Conflicts:
#	composer.lock
  • Loading branch information
mahagr committed Jun 15, 2018
2 parents bbfbdec + bd21b7f commit 9816b53
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 97 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
1. [](#bugfix)
* Handle `errors.display` system property better in admin plugin [admin#1452](https://github.com/getgrav/grav-plugin-admin/issues/1452)
* Fix classes on non-http based protocol links [#2034](https://github.com/getgrav/grav/issues/2034)
* Fixed crash on IIS (Windows) with open_basedir in effect [#2053](https://github.com/getgrav/grav/issues/2053)
* Fixed incorrect routing with setup.php based base [#1892](https://github.com/getgrav/grav/issues/1892)
* Fixed image resource memory deallocation [#2045](https://github.com/getgrav/grav/pull/2045)

# v1.4.5
## 05/15/2018
Expand Down
17 changes: 15 additions & 2 deletions system/src/Grav/Common/Page/Medium/AbstractMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ public function __invoke($filename)
return $this->offsetGet($filename);
}

/**
* @param mixed $offset
*
* @return mixed
*/
public function offsetGet($offset)
{
$object = parent::offsetGet($offset);
$object = $object ? clone($object) : null;

return $object;
}

/**
* Get a list of all media.
*
Expand Down Expand Up @@ -177,8 +190,8 @@ protected function getFileParts($filename)
$type = 'base';

while (($part = array_shift($fileParts)) !== null) {
if ($part != 'meta' && $part != 'thumb') {
if (isset($extension)) {
if ($part !== 'meta' && $part !== 'thumb') {
if (null !== $extension) {
$name .= '.' . $extension;
}
$extension = $part;
Expand Down
27 changes: 18 additions & 9 deletions system/src/Grav/Common/Page/Medium/ImageFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@
use Grav\Common\Grav;
use Gregwar\Image\Exceptions\GenerationError;
use Gregwar\Image\Image;
use Gregwar\Image\Source;
use RocketTheme\Toolbox\Event\Event;

class ImageFile extends Image
{
public function __destruct()
{
$this->getAdapter()->deinit();
}

/**
* Clear previously applied operations
*/
Expand All @@ -30,15 +36,15 @@ public function clearOperations()
* @param int $quality the quality (for JPEG)
* @param bool $actual
*
* @return mixed|string
* @return string
*/
public function cacheFile($type = 'jpg', $quality = 80, $actual = false)
{
if ($type == 'guess') {
if ($type === 'guess') {
$type = $this->guessType();
}

if (!count($this->operations) && $type == $this->guessType() && !$this->forceCache) {
if (!$this->forceCache && !count($this->operations) && $type === $this->guessType()) {
return $this->getFilename($this->getFilePath());
}

Expand All @@ -60,8 +66,7 @@ public function cacheFile($type = 'jpg', $quality = 80, $actual = false)
$cacheFile .= $this->prettyName;
}


$cacheFile .= '.'.$type;
$cacheFile .= '.' . $type;

// If the files does not exists, save it
$image = $this;
Expand All @@ -76,7 +81,7 @@ public function cacheFile($type = 'jpg', $quality = 80, $actual = false)
$generate = function ($target) use ($image, $type, $quality) {
$result = $image->save($target, $type, $quality);

if ($result != $target) {
if ($result !== $target) {
throw new GenerationError($result);
}

Expand All @@ -87,15 +92,19 @@ public function cacheFile($type = 'jpg', $quality = 80, $actual = false)
try {
$perms = Grav::instance()['config']->get('system.images.cache_perms', '0755');
$perms = octdec($perms);
$file = $this->cache->setDirectoryMode($perms)->getOrCreateFile($cacheFile, $conditions, $generate, $actual);
$file = $this->getCacheSystem()->setDirectoryMode($perms)->getOrCreateFile($cacheFile, $conditions, $generate, $actual);
} catch (GenerationError $e) {
$file = $e->getNewFile();
}

// Nulling the resource
$this->getAdapter()->setSource(new Source\File($file));
$this->getAdapter()->deinit();

if ($actual) {
return $file;
} else {
return $this->getFilename($file);
}

return $this->getFilename($file);
}
}
66 changes: 34 additions & 32 deletions system/src/Grav/Common/Page/Medium/ImageMedium.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ public function __construct($items = [], Blueprint $blueprint = null)
}
}

public function __destruct()
{
unset($this->image);
}

public function __clone()
{
$this->image = $this->image ? clone $this->image : null;

parent::__clone();
}

/**
* Add meta file for the medium.
*
Expand Down Expand Up @@ -159,14 +171,14 @@ public function url($reset = true)
$image_dir = $locator->findResource('cache://images', false);
$saved_image_path = $this->saveImage();

$output = preg_replace('|^' . preg_quote(GRAV_ROOT) . '|', '', $saved_image_path);
$output = preg_replace('|^' . preg_quote(GRAV_ROOT, '|') . '|', '', $saved_image_path);

if ($locator->isStream($output)) {
$output = $locator->findResource($output, false);
}

if (Utils::startsWith($output, $image_path)) {
$output = '/' . $image_dir . preg_replace('|^' . preg_quote($image_path) . '|', '', $output);
$output = '/' . $image_dir . preg_replace('|^' . preg_quote($image_path, '|') . '|', '', $output);
}

if ($reset) {
Expand Down Expand Up @@ -233,13 +245,13 @@ public function getImagePrettyName()
{
if ($this->get('prettyname')) {
return $this->get('prettyname');
} else {
$basename = $this->get('basename');
if (preg_match('/[a-z0-9]{40}-(.*)/', $basename, $matches)) {
$basename = $matches[1];
}
return $basename;
}

$basename = $this->get('basename');
if (preg_match('/[a-z0-9]{40}-(.*)/', $basename, $matches)) {
$basename = $matches[1];
}
return $basename;
}

/**
Expand Down Expand Up @@ -287,7 +299,7 @@ public function derivatives($min_width, $max_width = 2500, $step = 200) {
// It's possible that MediumFactory::fromFile returns null if the
// original image file no longer exists and this class instance was
// retrieved from the page cache
if (isset($derivative)) {
if (null !== $derivative) {
$index = 2;
$alt_widths = array_keys($this->alternatives);
sort($alt_widths);
Expand All @@ -308,8 +320,6 @@ public function derivatives($min_width, $max_width = 2500, $step = 200) {
$derivative->set('width', $width);
$derivative->set('height', $height);

$derivative->saveImage();
$derivative->resetImage();
$this->addAlternative($ratio, $derivative);
}
}
Expand Down Expand Up @@ -348,7 +358,6 @@ public function reset()

if ($this->image) {
$this->image();
$this->image->clearOperations(); // Clear previously applied operations
$this->querystring('');
$this->filter();
$this->clearAlternatives();
Expand Down Expand Up @@ -441,7 +450,7 @@ public function format($format)
* Set or get sizes parameter for srcset media action
*
* @param string $sizes
* @return $this
* @return string
*/
public function sizes($sizes = null)
{
Expand All @@ -468,7 +477,7 @@ public function sizes($sizes = null)
*/
public function width($value = 'auto')
{
if (!$value || $value == 'auto')
if (!$value || $value === 'auto')
$this->attributes['width'] = $this->get('width');
else
$this->attributes['width'] = $value;
Expand All @@ -489,7 +498,7 @@ public function width($value = 'auto')
*/
public function height($value = 'auto')
{
if (!$value || $value == 'auto')
if (!$value || $value === 'auto')
$this->attributes['height'] = $this->get('height');
else
$this->attributes['height'] = $value;
Expand All @@ -505,11 +514,11 @@ public function height($value = 'auto')
*/
public function __call($method, $args)
{
if ($method == 'cropZoom') {
if ($method === 'cropZoom') {
$method = 'zoomCrop';
}

if (!in_array($method, self::$magic_actions)) {
if (!\in_array($method, self::$magic_actions, true)) {
return parent::__call($method, $args);
}

Expand Down Expand Up @@ -560,6 +569,9 @@ protected function image()
// Use existing cache folder or if it doesn't exist, create it.
$cacheDir = $locator->findResource('cache://images', true) ?: $locator->findResource('cache://images', true, true);

// Make sure we free previous image.
unset($this->image);

$this->image = ImageFile::open($file)
->setCacheDir($cacheDir)
->setActualCacheDir($cacheDir)
Expand All @@ -568,18 +580,10 @@ protected function image()
return $this;
}

/**
* Frees the cached image file.
*/
protected function resetImage()
{
$this->image = null;
}

/**
* Save the image with cache.
*
* @return mixed|string
* @return string
*/
protected function saveImage()
{
Expand All @@ -593,7 +597,7 @@ protected function saveImage()
return $this->result;
}

if ($this->get('debug') && !$this->debug_watermarked) {
if (!$this->debug_watermarked && $this->get('debug')) {
$ratio = $this->get('ratio');
if (!$ratio) {
$ratio = 1;
Expand All @@ -604,9 +608,7 @@ protected function saveImage()
$this->image->merge(ImageFile::open($overlay));
}

$cachedPath = $this->image->cacheFile($this->format, $this->quality);
$this->set('filepath', $cachedPath);
return $cachedPath;
return $this->image->cacheFile($this->format, $this->quality);
}

/**
Expand Down Expand Up @@ -642,9 +644,9 @@ public function higherQualityAlternative()
}

return $max;
} else {
return $this;
}

return $this;
}

}
7 changes: 6 additions & 1 deletion system/src/Grav/Common/Page/Medium/Medium.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,19 @@ public function __construct($items = [], Blueprint $blueprint = null)
$this->reset();
}

public function __clone()
{
// Allows future compatibility as parent::__clone() works.
}

/**
* Create a copy of this media object
*
* @return Medium
*/
public function copy()
{
return clone($this);
return clone $this;
}

/**
Expand Down
Loading

0 comments on commit 9816b53

Please sign in to comment.