Skip to content
Open
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
27 changes: 22 additions & 5 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

class Image
{
const SEPARATOR_COMMA = ',';

/**
* @var string
Expand Down Expand Up @@ -64,6 +65,11 @@ class Image
*/
private $orientation;

/**
* @var string
*/
private $base64Header;

public function __construct(\G4\Storage\Storage $storage = null, \G4\Image\StorageConfig $storageConfig = null, $photoId = null, $mimeType = null, $driver = Consts::DEFAULT_DRIVER)
{
$this->storage = $storage;
Expand All @@ -88,9 +94,7 @@ public function createImageResourceFromBase64Encoded($extractExifData = false)
throw new \Exception('Image has unavailable MIME type.', Consts::HTTP_CODE_415);
}

if ($extractExifData) {
$this->extractExifDataAndSetOrientation();
}
$this->setBase64Header();

$this->base64Encoded = str_replace(Consts::getAllowedHeaders(), '', $this->base64Encoded);

Expand All @@ -100,6 +104,10 @@ public function createImageResourceFromBase64Encoded($extractExifData = false)

$this->checkIfImageIsProperlyDecoded($decoded);

if ($extractExifData) {
$this->extractExifDataAndSetOrientation();
}

$f = finfo_open();

$this->setMimeType( finfo_buffer($f, $decoded, FILEINFO_MIME_TYPE) );
Expand Down Expand Up @@ -642,7 +650,7 @@ public function orientateAndSaveToOriginalPath()
private function extractExifDataAndSetOrientation()
{
if ($this->isJpeg()) {
$exifData = exif_read_data($this->base64Encoded);
$exifData = exif_read_data($this->base64Header . $this->base64Encoded);
$this->orientation = !empty($exifData['Orientation'])
? (int) $exifData['Orientation']
: 0;
Expand All @@ -666,7 +674,7 @@ private function getMimeTypeFromBase64()
*/
private function isJpeg()
{
return $this->getMimeTypeFromBase64() === Consts::IMAGE_JPEG;
return strpos($this->base64Header, Consts::IMAGE_JPEG) !== false;
}

/**
Expand Down Expand Up @@ -718,4 +726,13 @@ private function orientateImage($inputImage)

return $image;
}

/**
* @return void
*/
private function setBase64Header()
{
$this->base64Header = explode(self::SEPARATOR_COMMA, $this->base64Encoded, 2)[0];
$this->base64Header .= self::SEPARATOR_COMMA;
}
}