Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for progessive jpeg encoding
- Loading branch information
1 parent
907df86
commit 4b2891f
Showing
3 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Intervention\Image\Tests\Traits; | ||
|
||
use Intervention\Image\Traits\CanBuildFilePointer; | ||
|
||
trait CanDetectProgressiveJpeg | ||
{ | ||
use CanBuildFilePointer; | ||
|
||
/** | ||
* Checks if the given image data is progressive encoded Jpeg format | ||
* | ||
* @param string $imagedata | ||
* @return bool | ||
*/ | ||
private function isProgressiveJpeg(string $imagedata): bool | ||
{ | ||
$f = $this->buildFilePointer($imagedata); | ||
|
||
while (!feof($f)) { | ||
if (unpack('C', fread($f, 1))[1] !== 0xff) { | ||
return false; | ||
} | ||
|
||
$blockType = unpack('C', fread($f, 1))[1]; | ||
|
||
switch (true) { | ||
case $blockType == 0xd8: | ||
case $blockType >= 0xd0 && $blockType <= 0xd7: | ||
break; | ||
|
||
case $blockType == 0xc0: | ||
fclose($f); | ||
return false; | ||
|
||
case $blockType == 0xc2: | ||
fclose($f); | ||
return true; | ||
|
||
case $blockType == 0xd9: | ||
break 2; | ||
|
||
default: | ||
$blockSize = unpack('n', fread($f, 2))[1]; | ||
fseek($f, $blockSize - 2, SEEK_CUR); | ||
break; | ||
} | ||
} | ||
|
||
fclose($f); | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters