private function getLength()
 {
     $gifFilePath = $this->directory['full'] . $this->fileinfo['filename'];
     if (GifFrameExtractor::isAnimatedGif($gifFilePath)) {
         $gfe = new GifFrameExtractor();
         $gfe->extract($gifFilePath);
         $gifTotalDuration = 0;
         // Get the total duration of the gif; if a frame is 0s of a second long, automatically add 0.1s because browsers play it at that speed anyway.
         // Read more: http://humpy77.deviantart.com/journal/Frame-Delay-Times-for-Animated-GIFs-240992090
         foreach ($gfe->getFrameDurations() as $duration) {
             $gifTotalDuration += $duration == 0 ? 10 : $duration;
         }
         return $gifTotalDuration / 100;
     } else {
         // So it's a gif, but it's not animated. Return 0
         return 0;
     }
 }
Exemple #2
0
 /**
  * get image file
  * @param  [type] $imgFile [description]
  * @return [type]          [description]
  */
 public function get($imgFile)
 {
     $this->isAnimatedGif = GifFrameExtractor::isAnimatedGif($imgFile);
     if ($this->isAnimatedGif) {
         $this->frames = $this->_getAnimatedGif($imgFile);
     } else {
         $this->imgFileRes = $this->interImage->make($imgFile);
     }
     return $this;
 }
 /**
  * Execute the command.
  *
  * @return void
  */
 public function handle()
 {
     // Before handling the file resize
     // change the directory to public path of laravel
     // as many of the path will be used from public_path
     chdir(public_path());
     \Log::info('ImageResizer for queue: ' . $this->imageFile->fullpath);
     // Initialize InterImage Instance first
     $interConfig = config('image');
     $this->interImage = new InterImage($interConfig);
     $sizes = $this->type_config['sizes'];
     $compiled_path = $this->type_config['compiled'];
     $filename = $this->imageFile->filename;
     foreach ($sizes as $folder => $size) {
         $width = $size[0];
         $height = $size[1];
         $scaling = $size[2];
         $extension = $size[3];
         if ($extension == 'original') {
             $extension = $this->imageFile->extension;
         }
         $is_animated = false;
         if ($extension == 'gif' && $size[4] == 'animated') {
             // Check if animated is enabled for gif images
             if ($this->type_config['crop']['enabled']) {
                 throw new \TarunMangukiya\ImageResizer\Exception\InvalidConfigException('Crop function along with animated gif is not allowed. Please disable crop or animated gif resize in config.');
             }
             if (!class_exists('\\GifFrameExtractor\\GifFrameExtractor') || !class_exists('\\GifCreator\\GifCreator')) {
                 throw new \TarunMangukiya\ImageResizer\Exception\InvalidConfigException('You need to install "Sybio/GifFrameExtractor" and "Sybio/GifCreator" packages to resize animated gif files.');
             }
             $is_animated = \GifFrameExtractor\GifFrameExtractor::isAnimatedGif($this->imageFile->fullpath);
         }
         $target = "{$compiled_path}/{$folder}/{$filename}-{$width}x{$height}.{$extension}";
         if ($is_animated) {
             $this->resizeAnimatedImage($this->imageFile->fullpath, $target, $size);
         } else {
             // resize normal non-animated files
             $this->resizeImage($this->imageFile->fullpath, $target, $size, $folder);
         }
     }
     \Log::info('ImageResizer Resized: ' . $target);
 }
 public function automatic_reduction($file, $image_url)
 {
     $filetype = $this->getFileType($file);
     if ((!empty($this->maximum_picture_size['width']) || !empty($this->maximum_picture_size['height'])) && ($this->width > $this->maximum_picture_size['width'] || $this->height > $this->maximum_picture_size['height'])) {
         if ($this->width > $this->height) {
             $maximum_picture_size_width = empty($this->maximum_picture_size['width']) ? $this->width * $this->maximum_picture_size['height'] / $this->height : $this->maximum_picture_size['width'];
             $new_width = intval($maximum_picture_size_width);
             $new_height = intval($this->height * $maximum_picture_size_width / $this->width);
         } else {
             $maximum_picture_size_height = empty($this->maximum_picture_size['height']) ? $this->height * $this->maximum_picture_size['width'] / $this->width : $this->maximum_picture_size['height'];
             $new_width = intval($this->width * $maximum_picture_size_height / $this->height);
             $new_height = intval($maximum_picture_size_height);
         }
         $layer = ImageWorkshop::initFromString($file);
         if ($filetype == 'gif' && GifFrameExtractor\GifFrameExtractor::isAnimatedGif($this->temp)) {
             $gfe = new GifFrameExtractor\GifFrameExtractor();
             $frames = $gfe->extract($this->temp, true);
             $retouchedFrames = array();
             foreach ($frames as $frame) {
                 $frameLayer = ImageWorkshop::initFromResourceVar($frame['image']);
                 $frameLayer->resizeInPixel($new_width, $new_height);
                 $retouchedFrames[] = $frameLayer->getResult();
             }
             $gc = new GifCreator\GifCreator();
             $gc->create($retouchedFrames, $gfe->getFrameDurations(), 0);
             $file = $gc->getGif();
         } else {
             $layer->resizeInPixel($new_width, $new_height, true);
             ob_start();
             switch ($filetype) {
                 case 'jpg':
                 case 'jpeg':
                     ImageJpeg($layer->getResult(), null, 75);
                     break;
                 case 'png':
                     imagealphablending($layer->getResult(), false);
                     imagesavealpha($layer->getResult(), true);
                     ImagePng($layer->getResult());
                     break;
                 case 'gif':
                     ImageGif($layer->getResult(), null, 75);
                     break;
             }
             $file = ob_get_contents();
             ob_end_clean();
         }
         imagedestroy($layer->getResult());
         $this->width = $new_width;
         $this->height = $new_height;
     }
     return $file;
 }