Skip to content

Commit

Permalink
Handle SVG file as a normal image
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno Vitorino committed Jan 7, 2019
1 parent 9b86fa3 commit 900d060
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
6 changes: 3 additions & 3 deletions spoon/form/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,13 @@ public function isAllowedMimeType(array $allowedTypes, $error = null)
if($this->isFilled())
{
// get image properties
$properties = @getimagesize($_FILES[$this->attributes['name']]['tmp_name']);
$mimeType = mime_content_type($_FILES[$this->attributes['name']]['tmp_name']);

// invalid properties
if($properties === false) $return = false;
if($mimeType === false) $return = false;

// search for mime-type
else $return = in_array($properties['mime'], $allowedTypes);
else $return = in_array($mimeType, $allowedTypes);

// add error if needed
if(!$return && $error !== null) $this->setError($error);
Expand Down
34 changes: 31 additions & 3 deletions spoon/form/image.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,17 @@ public function __construct($name, $class = 'inputFilefield', $classError = 'inp
// call the parent
parent::__construct($name, $class, $classError);

$this->properties = false;

// is the form submitted and the file is uploaded?
if($this->isSubmitted() && is_uploaded_file($this->getTempFileName())) $this->properties = @getimagesize($this->getTempFileName());
if($this->isSubmitted() && is_uploaded_file($this->getTempFileName())) {
$this->properties = @getimagesize($this->getTempFileName());

// fallback
else $this->properties = false;
// Handle the svg case
if ($this->properties === false && mime_content_type($this->getTempFileName()) === 'image/svg+xml') {
$this->properties = $this->getSvgProperties($this->getTempFileName());
}
}
}


Expand Down Expand Up @@ -92,6 +98,10 @@ public function getExtension($lowercase = true)
// get extension
$extension = image_type_to_extension($this->properties[2], false);

if ($extension === false && $this->properties['mime'] === 'image/svg+xml') {
$extension = 'svg';
}

// cleanup
if($extension == 'jpeg') $extension = 'jpg';

Expand Down Expand Up @@ -195,4 +205,22 @@ public function isSquare($error = null)
// return
return $isSquare;
}

/**
* @param string $file
*/
private function getSvgProperties($file)
{
$xmlget = simplexml_load_file($file);
$xmlattributes = $xmlget->attributes();
$width = preg_replace('/[a-zA-Z]+/', '', (string)$xmlattributes->width);
$height = preg_replace('/[a-zA-Z]+/', '', (string) $xmlattributes->height);

return [
0 => $width,
1 => $height,
2 => false,
'mime' => 'image/svg+xml',
];
}
}

0 comments on commit 900d060

Please sign in to comment.