Skip to content

Commit

Permalink
fixes #56 but need Imagick
Browse files Browse the repository at this point in the history
  • Loading branch information
comur committed Jun 2, 2019
1 parent 109d59b commit d430e81
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
43 changes: 40 additions & 3 deletions Controller/UploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,27 @@ public function getLibraryImagesAction(Request $request){
return new Response(json_encode($result));
}

private function isGifAnimated($filename) {
if(!($fh = @fopen($filename, 'rb')))
return false;
$count = 0;
//an animated gif contains multiple "frames", with each frame having a
//header made up of:
// * a static 4-byte sequence (\x00\x21\xF9\x04)
// * 4 variable bytes
// * a static 2-byte sequence (\x00\x2C) (some variants may use \x00\x21 ?)

// We read through the file til we reach the end of the file, or we've found
// at least 2 frame headers
while(!feof($fh) && $count < 2) {
$chunk = fread($fh, 1024 * 100); //read 100kb at a time
$count += preg_match_all('#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $chunk, $matches);
}

fclose($fh);
return $count > 1;
}

/**
* Crops or resizes image and writes it on disk
*/
Expand All @@ -345,9 +366,25 @@ private function resizeCropImage($destSrc, $imgSrc, $destX, $destY, $srcX, $srcY
$imageQuality = 100;
break;
case 'gif':
$srcFunc = 'imagecreatefromgif';
$writeFunc = 'imagegif';
$imageQuality = null;
if ($this->isGifAnimated($imgSrc) && extension_loaded('imagick')) {
$image = new \Imagick($imgSrc);

$image = $image->coalesceImages();

foreach ($image as $frame) {
$frame->cropImage($srcW, $srcH, $srcX, $srcY);
$frame->thumbnailImage($destW, $destH);
$frame->setImagePage($destW, $destH, $destX, $destY);
}

$image = $image->deconstructImages();
$image->writeImages($destSrc, true);
return false;
} else {
$srcFunc = 'imagecreatefromgif';
$writeFunc = 'imagegif';
$imageQuality = null;
}
break;
case 'png':
$srcFunc = 'imagecreatefrompng';
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ If this bundle helps you reduce time to develop, you can pay me a cup of coffee

:warning: **I won't maintain Symfony 2 anymore, I will only merge PR on 1.2 branch for compatibility with SF2. If you need something you can always create a PR and I will merge it.**

:warning: **Animated gif cropping is only available with imagick**

Screen shots
------------

Expand Down

0 comments on commit d430e81

Please sign in to comment.