Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests-minimal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ jobs:

- name: Start SMTP Server
run: |
npm install -g maildev
npm install -g maildev@2.x
maildev &
# This will get a Stable Chrome version that is 2 releases back from latest, and install it
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ jobs:

- name: Start SMTP Server
run: |
npm install -g maildev
npm install -g maildev@2.x
maildev &

# This will get a Stable Chrome version that is 2 releases back from latest, and install it
Expand Down
51 changes: 47 additions & 4 deletions system/ee/ExpressionEngine/Model/File/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,61 @@
*/
class File extends FileSystemEntity
{
/**
* Get the stored original image width.
*
* @return string|null Original image width as a numeric string, or null when unavailable.
*/
public function get__width()
{
$dimensions = explode(" ", $this->getProperty('file_hw_original'));
$dimensions = $this->getOriginalDimensions();

return $dimensions[1];
return $dimensions === null ? null : $dimensions['width'];
}

/**
* Get the stored original image height.
*
* @return string|null Original image height as a numeric string, or null when unavailable.
*/
public function get__height()
{
$dimensions = explode(" ", $this->getProperty('file_hw_original'));
$dimensions = $this->getOriginalDimensions();

return $dimensions[0];
return $dimensions === null ? null : $dimensions['height'];
}

/**
* Parse the stored original image dimensions as an atomic pair.
*
* @return array|null Height and width as positive numeric strings, or null when invalid.
*/
private function getOriginalDimensions()
{
$value = $this->getProperty('file_hw_original');

if (! is_string($value)) {
return null;
}

$dimensions = explode(' ', $value);

if (count($dimensions) !== 2) {
return null;
}

list($height, $width) = $dimensions;

if (
! ctype_digit($height)
|| ! ctype_digit($width)
|| (int) $height <= 0
|| (int) $width <= 0
) {
return null;
}

return compact('height', 'width');
}

public function get__title()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,50 @@ public function testGetValuesDoesNotThrowWhenFileDimensionsCannotBeRead()
$this->assertArrayHasKey('file_hw_original', $values);
$this->assertNull($values['file_hw_original']);
}

/**
* Assert image dimensions are split into width and height accessors.
*
* @return void
*/
public function testDimensionsAreReadFromOriginalFileDimensions()
{
$file = new FileModel();
$file->setRawProperty('file_hw_original', '480 640');

$this->assertSame('640', $file->width);
$this->assertSame('480', $file->height);
}

/**
* Assert unavailable or invalid dimensions return null as an atomic pair.
*
* @dataProvider invalidOriginalDimensionsProvider
* @return void
*/
public function testInvalidOriginalFileDimensionsReturnNull($dimensions)
{
$file = new FileModel();
$file->setRawProperty('file_hw_original', $dimensions);

$this->assertNull($file->width);
$this->assertNull($file->height);
}

public static function invalidOriginalDimensionsProvider()
{
return array(
'empty' => array(''),
'null' => array(null),
'height only' => array('480'),
'non-numeric width' => array('480 wide'),
'extra component' => array('480 640 extra'),
'zero height' => array('0 640'),
'zero width' => array('480 0'),
'negative height' => array('-480 640'),
'negative width' => array('480 -640'),
);
}
}

class FileWithUnreadableRootStub extends FileModel
Expand Down
Loading