cdn/controllers/Crop.php
private function resizeAnimated(
        $filePath,
        $phpCropMethod
    ) {
        $hash       = md5(microtime(true) . uniqid()) . uniqid();
        $frames     = [];
        $cacheFiles = [];
        $durations  = [];
        $gfe        = new GifFrameExtractor\GifFrameExtractor();
        $gc         = new GifCreator\GifCreator();
        $width      = $this->width * $this->retinaMultiplier;
        $height     = $this->height * $this->retinaMultiplier;

        // --------------------------------------------------------------------------

        //  Extract all the frames, resize them and save to the cache
        $gfe->extract($filePath);

        $i = 0;
        foreach ($gfe->getFrames() as $frame) {

            //  Define the filename
            $filename     = $hash . '-' . $i . '.gif';
            $tempFilename = $hash . '-' . $i . '-original.gif';
            $i++;

            //  Set these for recompiling
            $frames[]     = $this->cdnCacheDir . $filename;
            $cacheFiles[] = $this->cdnCacheDir . $tempFilename;
            $durations[]  = $frame['duration'];

            // --------------------------------------------------------------------------

            //  Set some PHPThumb options
            $options = [
                'resizeUp' => true,
            ];

            // --------------------------------------------------------------------------

            //  Perform the resize; first save the original frame to disk
            imagegif($frame['image'], $this->cdnCacheDir . $tempFilename);

            $PHPThumb = new \PHPThumb\GD($this->cdnCacheDir . $tempFilename, $options);

            //  Prepare the parameters and call the method
            $aParams = [
                $width,
                $height,
                $this->cropQuadrant,
            ];

            call_user_func_array([$PHPThumb, $phpCropMethod], $aParams);

            // --------------------------------------------------------------------------

            //  Save cache version
            $PHPThumb->save($this->cdnCacheDir . $filename, strtoupper(substr($this->extension, 1)));
        }

        /**
         * Recompile the re-sized images back into an animated gif and save to the cache
         *
         * @todo: We assume the gif loops infinitely but we should really check.
         * Issue made on the library's GitHub asking for this feature.
         * View here: https://github.com/Sybio/GifFrameExtractor/issues/3
         */

        $gc->create($frames, $durations, 0);
        $data = $gc->getGif();

        // --------------------------------------------------------------------------

        //  Output to browser
        header('Content-Type: image/gif', true);
        echo $data;

        // --------------------------------------------------------------------------

        //  Save to cache
        Factory::helper('file');
        write_file($this->cdnCacheDir . $this->cdnCacheFile, $data);

        // --------------------------------------------------------------------------

        //  Remove cache frames
        foreach ($frames as $frame) {
            if (file_exists($frame)) {
                unlink($frame);
            }
        }

        foreach ($cacheFiles as $frame) {
            if (file_exists($frame)) {
                unlink($frame);
            }
        }

        // --------------------------------------------------------------------------

        /**
         * Kill script, th, th, that's all folks.
         * Stop the output class from hijacking our headers and
         * setting an incorrect Content-Type
         */

        exit(0);
    }
private function resizeAnimated(
        $filePath,
        $phpCropMethod
    ) {
        $hash       = md5(microtime(true) . uniqid()) . uniqid();
        $frames     = [];
        $cacheFiles = [];
        $durations  = [];
        $gfe        = new GifFrameExtractor\GifFrameExtractor();
        $gc         = new GifCreator\GifCreator();
        $width      = $this->width * $this->retinaMultiplier;
        $height     = $this->height * $this->retinaMultiplier;

        // --------------------------------------------------------------------------

        //  Extract all the frames, resize them and save to the cache
        $gfe->extract($filePath);

        $i = 0;
        foreach ($gfe->getFrames() as $frame) {

            //  Define the filename
            $filename     = $hash . '-' . $i . '.gif';
            $tempFilename = $hash . '-' . $i . '-original.gif';
            $i++;

            //  Set these for recompiling
            $frames[]     = $this->cdnCacheDir . $filename;
            $cacheFiles[] = $this->cdnCacheDir . $tempFilename;
            $durations[]  = $frame['duration'];

            // --------------------------------------------------------------------------

            //  Set some PHPThumb options
            $options = [
                'resizeUp' => true,
            ];

            // --------------------------------------------------------------------------

            //  Perform the resize; first save the original frame to disk
            imagegif($frame['image'], $this->cdnCacheDir . $tempFilename);

            $PHPThumb = new \PHPThumb\GD($this->cdnCacheDir . $tempFilename, $options);

            //  Prepare the parameters and call the method
            $aParams = [
                $width,
                $height,
                $this->cropQuadrant,
            ];

            call_user_func_array([$PHPThumb, $phpCropMethod], $aParams);

            // --------------------------------------------------------------------------

            //  Save cache version
            $PHPThumb->save($this->cdnCacheDir . $filename, strtoupper(substr($this->extension, 1)));
        }

        /**
         * Recompile the re-sized images back into an animated gif and save to the cache
         *
         * @todo: We assume the gif loops infinitely but we should really check.
         * Issue made on the library's GitHub asking for this feature.
         * View here: https://github.com/Sybio/GifFrameExtractor/issues/3
         */

        $gc->create($frames, $durations, 0);
        $data = $gc->getGif();

        // --------------------------------------------------------------------------

        //  Output to browser
        header('Content-Type: image/gif', true);
        echo $data;

        // --------------------------------------------------------------------------

        //  Save to cache
        Factory::helper('file');
        write_file($this->cdnCacheDir . $this->cdnCacheFile, $data);

        // --------------------------------------------------------------------------

        //  Remove cache frames
        foreach ($frames as $frame) {
            if (file_exists($frame)) {
                unlink($frame);
            }
        }

        foreach ($cacheFiles as $frame) {
            if (file_exists($frame)) {
                unlink($frame);
            }
        }

        // --------------------------------------------------------------------------

        /**
         * Kill script, th, th, that's all folks.
         * Stop the output class from hijacking our headers and
         * setting an incorrect Content-Type
         */

        exit(0);
    }
private function resizeAnimated ( $ filePath , $ phpCropMethod ) { $ hash = md5 ( microtime ( true ) . uniqid ( ) ) . uniqid ( ) ; $ frames = [ ] ; $ cacheFiles = [ ] ; $ durations = [ ] ; $ gfe = new GifFrameExtractor \ GifFrameExtractor ( ) ; $ gc = new GifCreator \ GifCreator ( ) ; $ width = $ this -> width * $ this -> retinaMultiplier ; $ height = $ this -> height * $ this -> retinaMultiplier ; // -------------------------------------------------------------------------- //  Extract all the frames, resize them and save to the cache $ gfe -> extract ( $ filePath ) ; $ i = 0 ; foreach ( $ gfe -> getFrames ( ) as $ frame ) { //  Define the filename $ filename = $ hash . '-' . $ i . '.gif' ; $ tempFilename = $ hash . '-' . $ i . '-original.gif' ; $ i ++ ; //  Set these for recompiling $ frames [ ] = $ this -> cdnCacheDir . $ filename ; $ cacheFiles [ ] = $ this -> cdnCacheDir . $ tempFilename ; $ durations [ ] = $ frame [ 'duration' ] ; // -------------------------------------------------------------------------- //  Set some PHPThumb options $ options = [ 'resizeUp' => true , ] ; // -------------------------------------------------------------------------- //  Perform the resize; first save the original frame to disk imagegif ( $ frame [ 'image' ] , $ this -> cdnCacheDir . $ tempFilename ) ; $ PHPThumb = new \ PHPThumb \ GD ( $ this -> cdnCacheDir . $ tempFilename , $ options ) ; //  Prepare the parameters and call the method $ aParams = [ $ width , $ height , $ this -> cropQuadrant , ] ; call_user_func_array ( [ $ PHPThumb , $ phpCropMethod ] , $ aParams ) ; // -------------------------------------------------------------------------- //  Save cache version $ PHPThumb -> save ( $ this -> cdnCacheDir . $ filename , strtoupper ( substr ( $ this -> extension , 1 ) ) ) ; } /**
         * Recompile the re-sized images back into an animated gif and save to the cache
         *
         * @todo: We assume the gif loops infinitely but we should really check.
         * Issue made on the library's GitHub asking for this feature.
         * View here: https://github.com/Sybio/GifFrameExtractor/issues/3
         */ $ gc -> create ( $ frames , $ durations , 0 ) ; $ data = $ gc -> getGif ( ) ; // -------------------------------------------------------------------------- //  Output to browser header ( 'Content-Type: image/gif' , true ) ; echo $ data ; // -------------------------------------------------------------------------- //  Save to cache Factory :: helper ( 'file' ) ; write_file ( $ this -> cdnCacheDir . $ this -> cdnCacheFile , $ data ) ; // -------------------------------------------------------------------------- //  Remove cache frames foreach ( $ frames as $ frame ) { if ( file_exists ( $ frame ) ) { unlink ( $ frame ) ; } } foreach ( $ cacheFiles as $ frame ) { if ( file_exists ( $ frame ) ) { unlink ( $ frame ) ; } } // -------------------------------------------------------------------------- /**
         * Kill script, th, th, that's all folks.
         * Stop the output class from hijacking our headers and
         * setting an incorrect Content-Type
         */ exit ( 0 ) ; }
Resize an animated image

@param  string $filePath      The file to resize
@param  string $phpCropMethod The PHPThumb method to use for resizing

@return void
Resize an animated image