PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php 生成gif 动图,可控制每张图时间大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
<?PHP

//namespace gifCreator;

/**
 * Create an animated GIF from multiple images
 */
class gifcreator
{
    /**
     * @var String The gif String source (old: this->GIF)
     */
    private $gif;

    /**
     * @var String Encoder version (old: this->VER)
     */
    private $version;

    /**
     * @var Boolean check the image is build or not (old: this->IMG)
     */
    private $imgBuilt;

    /**
     * @var array Frames String sources (old: this->BUF)
     */
    private $framesources;

    /**
     * @var Integer Gif loop (old: this->LOp)
     */
    private $loop;

    /**
     * @var Integer Gif Dis (old: this->DIS)
     */
    private $dis;

    /**
     * @var Integer Gif color (old: this->COL)
     */
    private $colour;

    /**
     * @var array (old: this->ERR)
     */
    private $errors;

    // Methods
    // ===================================================================================

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->reset();

        // Static data
        $this->version = 'GifCreator: Under development';
        $this->errors = array(
            'ERR00' => 'Does not supported function for only one image.',
            'ERR01' => 'source is not a GIF image.',
            'ERR02' => 'You have to give resource image variables, image URL or image binary sources in $frames array.',
            'ERR03' => 'Does not make animation from animated GIF source.',
        );
    }

    /**
     * Create the GIF String (old: GIFEncoder)
     *
     * @param array $frames An array of frame: can be file paths, resource image variables, binary sources or image URLs
     * @param array $durations An array containing the duration of each frame
     * @param Integer $loop number of GIF loops before stopping animation (Set 0 to get an infinite loop)
     *
     * @return String The GIF String source
     */
    public function create($frames = array(), $durations = array(), $loop = 0)
    {
        if (!is_array($frames) && !is_array($durations)) {

            throw new \Exception($this->version.': '.$this->errors['ERR00']);
        }

        $this->loop = ($loop > -1) ? $loop : 0;
        $this->dis = 2;

        for ($i = 0; $i < count($frames); $i++) {

            if (is_resource($frames[$i])) { // resource var

                $resourceImg = $frames[$i];

                ob_start();
                imagegif($frames[$i]);
                $this->framesources[] = ob_get_contents();
                ob_end_clean();

            } elseif (is_String($frames[$i])) { // File path or URL or Binary source code

                if (file_exists($frames[$i]) || filter_var($frames[$i], FILTER_VALIDATE_URL)) { // File path

                    $frames[$i] = file_get_contents($frames[$i]);
                }

                $resourceImg = imagecreatefromString($frames[$i]);

                ob_start();
                imagegif($resourceImg);
                $this->framesources[] = ob_get_contents();
                ob_end_clean();

            } else { // Fail

                throw new \Exception($this->version.': '.$this->errors['ERR02']);
            }

            if ($i == 0) {

                $colour = imagecolortransparent($resourceImg);
            }

            if (substr($this->framesources[$i], 0, 6) != 'GIF87a' && substr($this->framesources[$i], 0, 6) != 'GIF89a') {

                throw new \Exception($this->version.': '.$i.' '.$this->errors['ERR01']);
            }

            for ($j = (13 + 3 * (2 << (ord($this->framesources[$i] { 10 }) & 0x07))), $k = TRUE; $k; $j++) {

                switch ($this->framesources[$i] { $j }) {

                    case '!':

                        if ((substr($this->framesources[$i], ($j + 3), 8)) == 'netscape') {

                            throw new \Exception($this->version.': '.$this->errors['ERR03'].' ('.($i + 1).' sourcE).');
                        }

                        break;

                    case ';':

                        $k = false;
                        break;
                }
            }

            unset($resourceImg);
        }

        if (isset($colour)) {

            $this->colour = $colour;

        } else {

            $red = $green = $blue = 0;
            $this->colour = ($red > -1 && $green > -1 && $blue > -1) ? ($red | ($green << 8) | ($blue << 16)) : -1;
        }

        $this->gifAddHeader();
        //d(count($this->framesources));
        for ($i = 0; $i < count($this->framesources); $i++) {
            $this->addGifFrames($i, $durations[$i]);
        }

        $this->gifAddFooter();

        return $this->gif;
    }

    // Internals
    // ===================================================================================

    /**
     * Add the header gif String in its source (old: GIFAddHeader)
     */
    public function gifAddHeader()
    {
        $cmap = 0;

        if (ord($this->framesources[0] { 10 }) & 0x80) {

            $cmap = 3 * (2 << (ord($this->framesources[0] { 10 }) & 0x07));

            $this->gif .= substr($this->framesources[0], 6, 7);
            $this->gif .= substr($this->framesources[0], 13, $cmap);
            $this->gif .= "!\377\13netscape2.0\3\1".$this->encodeAsciitochar($this->loop)."\0";
        }
    }

    /**
     * Add the frame sources to the GIF String (old: GIFAddFrames)
     *
     * @param Integer $i
     * @param Integer $d
     */
    public function addGifFrames($i, $d)
    {

        $Locals_str = 13 + 3 * (2 << (ord($this->framesources[ $i ] { 10 }) & 0x07));

        $Locals_end = strlen($this->framesources[$i]) - $Locals_str - 1;
        $Locals_tmp = substr($this->framesources[$i], $Locals_str, $Locals_end);

        $Global_len = 2 << (ord($this->framesources[0 ] { 10 }) & 0x07);
        $Locals_len = 2 << (ord($this->framesources[$i] { 10 }) & 0x07);

        $Global_rgb = substr($this->framesources[0], 13, 3 * (2 << (ord($this->framesources[0] { 10 }) & 0x07)));
        $Locals_rgb = substr($this->framesources[$i], 13, 3 * (2 << (ord($this->framesources[$i] { 10 }) & 0x07)));

        $Locals_ext = "!\xF9\x04".chr(($this->dis << 2) + 0).chr(($d >> 0 ) & 0xFF).chr(($d >> 8) & 0xFF)."\x0\x0";

        if ($this->colour > -1 && ord($this->framesources[$i] { 10 }) & 0x80) {

            for ($j = 0; $j < (2 << (ord($this->framesources[$i] { 10 } ) & 0x07)); $j++) {

                if (ord($Locals_rgb { 3 * $j + 0 }) == (($this->colour >> 16) & 0xFF) &&
                    ord($Locals_rgb { 3 * $j + 1 }) == (($this->colour >> 8) & 0xFF) &&
                    ord($Locals_rgb { 3 * $j + 2 }) == (($this->colour >> 0) & 0xFF)
                ) {
                    $Locals_ext = "!\xF9\x04".chr(($this->dis << 2) + 1).chr(($d >> 0) & 0xFF).chr(($d >> 8) & 0xFF).chr($j)."\x0";
                    break;
                }
            }
        }

        switch ($Locals_tmp { 0 }) {

            case '!':

                $Locals_img = substr($Locals_tmp, 8, 10);
                $Locals_tmp = substr($Locals_tmp, 18, strlen($Locals_tmp) - 18);

                break;

            case ',':

                $Locals_img = substr($Locals_tmp, 0, 10);
                $Locals_tmp = substr($Locals_tmp, 10, strlen($Locals_tmp) - 10);

                break;
        }

        if (ord($this->framesources[$i] { 10 }) & 0x80 && $this->imgBuilt) {

            if ($Global_len == $Locals_len) {

                if ($this->gifBlockCompare($Global_rgb, $Locals_rgb, $Global_len)) {

                    $this->gif .= $Locals_ext.$Locals_img.$Locals_tmp;

                } else {

                    $byte = ord($Locals_img { 9 });
                    $byte |= 0x80;
                    $byte &= 0xF8;
                    $byte |= (ord($this->framesources[0] { 10 }) & 0x07);
                    $Locals_img { 9 } = chr($bytE);
                    $this->gif .= $Locals_ext.$Locals_img.$Locals_rgb.$Locals_tmp;
                }

            } else {

                $byte = ord($Locals_img { 9 });
                $byte |= 0x80;
                $byte &= 0xF8;
                $byte |= (ord($this->framesources[$i] { 10 }) & 0x07);
                $Locals_img { 9 } = chr($bytE);
                $this->gif .= $Locals_ext.$Locals_img.$Locals_rgb.$Locals_tmp;
            }

        } else {

            $this->gif .= $Locals_ext.$Locals_img.$Locals_tmp;
        }

        $this->imgBuilt = true;
    }

    /**
     * Add the gif String footer char (old: GIFAddFooter)
     */
    public function gifAddFooter()
    {
        $this->gif .= ';';
    }

    /**
     * Compare two block and return the version (old: GIFBlockComparE)
     *
     * @param String $globalBlock
     * @param String $localBlock
     * @param Integer $length
     *
     * @return Integer
     */
    public function gifBlockCompare($globalBlock, $localBlock, $length)
    {
        for ($i = 0; $i < $length; $i++) {

            if ($globalBlock { 3 * $i + 0 } != $localBlock { 3 * $i + 0 } ||
                $globalBlock { 3 * $i + 1 } != $localBlock { 3 * $i + 1 } ||
                $globalBlock { 3 * $i + 2 } != $localBlock { 3 * $i + 2 }) {

                return 0;
            }
        }

        return 1;
    }

    /**
     * Encode an ASCII char into a String char (old: GIFWord)
     *
     * $param Integer $char ASCII char
     *
     * @return String
     */
    public function encodeAsciitochar($char)
    {
        return (chr($char & 0xFF).chr(($char >> 8) & 0xFF));
    }

    /**
     * Reset and clean the current object
     */
    public function reset()
    {
        $this->framesources;
        $this->gif = 'GIF89a'; // the GIF header
        $this->imgBuilt = false;
        $this->loop = 0;
        $this->dis = 2;
        $this->colour = -1;
    }

    // Getter / Setter
    // ===================================================================================

    /**
     * Get the final GIF image String (old: GetAnimation)
     *
     * @return String
     */
    public function getGif()
    {
        return $this->gif;
    }
}

//图片资源写入数组,支持如下图片资源。
$frames = array(
    "http://pic27.nipic.com/20130313/9252150_092049419327_2.jpg",
    "http://pic27.nipic.com/20130324/9252150_152129329000_2.jpg",
    "http://pic44.nipic.com/20140723/18505720_094503373000_2.jpg",
    "http://pic18.nipic.com/20120103/8993051_170340691334_2.jpg"
);

// 设置图片转换快慢,数值越小越快,数组个数和frames对应。
$durations = array(40, 80, 40, 20);

$gc = new GifCreator();
$gifBinary = $gc->create($frames, $durations, 0);

file_put_contents('./ceshi.gif',$gifBinary);

 

大佬总结

以上是大佬教程为你收集整理的php 生成gif 动图,可控制每张图时间全部内容,希望文章能够帮你解决php 生成gif 动图,可控制每张图时间所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: