示例#1
0
 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;
     }
 }
 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;
 }