PHP using the Gifencoder class to generate animated GIF animated captions _php tutorial

Source: Internet
Author: User
Today at the company, the manager lets make a scrolling subtitle. However, you are not allowed to generate GIF images. So the internet to find Gifencoder this class library. Really good, but, the application process also appeared some problems, now written here, for Yimeimei reference, less detours.

Text scrolling is divided into two situations. The first type is horizontal scrolling:
Copy CodeThe code is as follows:
<?php
Require_once ("GIFEncoder.class.php");
$count = 0; Set Default counters
while (true) {
$STR = $_request[' str ']? $_request[' str ']: "No input";
$length =strlen ($STR) *9; Calculate the number of degrees, this calculation is relatively simple, that is, each word number * 9 pixels
$im =imagecreatefromgif ("Hudongbg.gif"); Create images based on pictures
$white = Imagecolorallocate ($im, 255,255,255); Set a white
$str = Iconv ("GB2312", "UTF-8", $str); Special attention is given to the conversion code, since the Imagettftext used later can only be encoded with UTF-8
$now =220-$count; Horizontal position of current run
Imagettftext ($im, 13,0, $now, $white, "Ziti.ttf", $str); According to the font to write text on the picture, parameter meaning (image source, text size, tilt angle, horizontal position, vertical position, color, use of the font file, to write the content
Imagegif ($im);
Imagedestroy ($im);
$imagedata [] = Ob_get_contents (); Create image data for this frame
Ob_clean ();
$count + +;
if ($now + $length <0) {//If the last text moves to the head, then end
Break
}
}
$diy []=0;//Start delay time
$gif = new Gifencoder ($imagedata, $diy, 0,2,0,0,0, "bin");
Ob_start ();
Header (' content-type:image/gif ');
echo $gif->getanimation ();
?>

Then it's vertical:
Copy CodeThe code is as follows:
<?php
/*
Get the "str= first row, second row, third row" data from the URL
*/
Require_once ("GIFEncoder.class.php");
$array _str=array ();
$str =$_get[' str '];
/*
Convert $str to an array
*/
if ($str! = ") {
$array _str=explode (";", $str);
}else{
$array _str=array (Welcome to our store!) ");
}
/*
If the array element does not have a value, clear the last ";"
*/
foreach ($array _str as $k = + $v) {
if (! $v)
unset ($array _str[$k]);
}
For ($i =0, $length =count ($array _str); $i < $length; $i + +) {
for ($k =0; $k <20; $k + +) {
$im =imagecreatefromgif ("Hudongbg.gif"); Create images based on pictures
$white = Imagecolorallocate ($im, 255,255,255); Set a white
$test = Iconv ("GB2312", "UTF-8", $array _str[$i]); Special attention is the conversion code, because after the use of the Imagettftext can only be encoded with utf-8, note, must be received with a string, cannot use the array, otherwise it will be garbled
$heigth =40-$k; Current text height
Imagettftext ($im, 13,0,0, $heigth, $white, "Ziti.ttf", $test); Write text to a picture
Imagegif ($im);
$imagedata [] = Ob_get_contents ();
Ob_clean ();
}
}
/*
This is the time to set the delay for each picture, which is delayed by 2 seconds to the 20th frame. So as to appear the text to go to the top, pause for a while effect
*/
$delay =array ();
For ($i =1, $length =count ($array _str) *20; $i <= $length; $i + +) {
if ($i%20==0) {
$delay [$i -1]= "200";
}else{
$delay [$i -1]= "1";
}
}
$transparent =array (0);
$gif = new Gifencoder ($imagedata, $delay, 0,2,0,0, $transparent, "bin");
Ob_start ();
Header (' content-type:image/gif ');
echo $gif->getanimation ();
?>

Gifencoder class Source:
Copy CodeThe code is as follows:
<?php
Class Gifencoder {
var $GIF = "gif89a"; /* GIF Header 6 bytes */
var $VER = "Gifencoder V2.06"; /* Encoder version */
var $BUF = Array ();
var $LOP = 0;
var $DIS = 2;
var $COL =-1;
var $IMG =-1;
var $ERR = Array (
' ERR00 ' = ' Does not supported function for only one image! ',
' ERR01 ' = ' Source is not a GIF image! ',
' ERR02 ' = ' unintelligible flag ',
' ERR03 ' = ' Could not make animation from animated GIF source ',
);
/*
:::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: Gifencoder ...
::
*/
function Gifencoder (
$GIF _src, $GIF _dly, $GIF _lop, $GIF _dis,
$GIF _red, $GIF _grn, $GIF _blu, $GIF _mod
) {
if (! Is_array ($GIF _src) &&! Is_array ($GIF _tim)) {
printf ("%s:%s", $this->ver, $this->err [' ERR00 ']);
Exit (0);
}
$this->lop = ($GIF _lop >-1)? $GIF _lop:0;
$this->dis = ($GIF _dis >-1)? (($GIF _dis < 3) $GIF _dis:3): 2;
$this->col = ($GIF _red > 1 && $GIF _grn > 1 && $GIF _blu >-1)?
($GIF _red | ($GIF _grn << 8) | ($GIF _blu << 16)) :-1;
for ($i = 0; $i < count ($GIF _src); $i + +) {
if (Strtolower ($GIF _mod) = = "url") {
$this->buf [] = Fread (fopen ($GIF _src [$i], "RB"), FileSize ($GIF _src [$i]));
}
else if (Strtolower ($GIF _mod) = = "Bin") {
$this->buf [] = $GIF _src [$i];
}
else {
printf ("%s:%s (%s)!", $this->ver, $this->err [' ERR02 '], $GIF _mod);
Exit (0);
}
if (substr ($this->buf [$i], 0, 6)! = "gif87a" && substr ($this->buf [$i], 0, 6)! = "gif89a") {
printf ("%s:%d%s", $this->ver, $i, $this->err [' ERR01 ']);
Exit (0);
}
for ($j = (3 * (2 << (ord ($this->buf [$i] {ten}) & 0x07)), $k = TRUE; $k; $j + +) {
Switch ($this->buf [$i] {$j}) {
Case "!":
if ((substr ($this->buf [$i], ($j + 3), 8)) = = "NETSCAPE") {
printf ("%s:%s (%s source)!", $this->ver, $this->err [' ERR03 '], ($i + 1));
Exit (0);
}
Break
Case ";":
$k = FALSE;
Break
}
}
}
Gifencoder::gifaddheader ();
for ($i = 0; $i < count ($this->buf); $i + +) {
Gifencoder::gifaddframes ($i, $GIF _dly [$i]);
}
Gifencoder::gifaddfooter ();
}
/*
:::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: Gifaddheader ...
::
*/
function Gifaddheader () {
$cmap = 0;
if (Ord ($this->buf [0] {ten}) & 0x80) {
$cmap = 3 * (2 << (ord ($this->buf [0] {ten}) & 0x07));
$this->gif. = substr ($this->buf [0], 6, 7);
$this->gif. = substr ($this->buf [0], $cmap);
$this->gif. = "!/377/13NETSCAPE2.0/3/1". Gifencoder::gifword ($this->lop). "/0";
}
}
/*
:::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: Gifaddframes ...
::
*/
function Gifaddframes ($i, $d) {
$Locals _STR = 3 * (2 << (ord ($this->buf [$i] {ten}) & 0x07);
$Locals _end = strlen ($this->buf [$i])-$Locals _str-1;
$Locals _tmp = substr ($this->buf [$i], $Locals _str, $Locals _end);
$Global _len = 2 << (ord ($this->buf [0] {ten}) & 0x07);
$Locals _len = 2 << (ord ($this->buf [$i] {ten}) & 0x07);
$Global _rgb = substr ($this->buf [0], 13,
3 * (2 << (ord ($this->buf [0] {ten}) & 0x07));
$Locals _rgb = substr ($this->buf [$i], 13,
3 * (2 << (ord ($this->buf [$i] {ten}) & 0x07));
$Locals _ext = "!/xf9/x04". Chr (($this->dis << 2) + 0).
Chr (($d >> 0) & 0xFF). Chr (($d >> 8) & 0xFF). "/x0/x0";
if ($this->col >-1 && ord ($this->buf [$i] {ten}) & 0x80) {
for ($j = 0; $j < (2 << (ord ($this->buf [$i] {ten}) & 0x07), $j + +) {
if (
Ord ($Locals _rgb {3 * $j + 0}) = = ($this->col >> 0) & 0xFF &
Ord ($Locals _rgb {3 * $j + 1}) = = ($this->col >> 8) & 0xFF &
Ord ($Locals _rgb {3 * $j + 2}) = = ($this->col >>) & 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, strlen ($Locals _tmp)-18);
Break
Case ",":
$Locals _img = substr ($Locals _tmp, 0, 10);
$Locals _tmp = substr ($Locals _tmp, ten, strlen ($Locals _tmp)-10);
Break
}
if (Ord ($this->buf [$i] {ten}) & 0x80 && $this->img >-1) {
if ($Global _len = = $Locals _len) {
if (Gifencoder::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->buf [0] {ten}) & 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->buf [$i] {ten}) & 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->img = 1;
}
/*
:::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: Gifaddfooter ...
::
*/
function Gifaddfooter () {
$this->gif. = ";";
}
/*
:::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: Gifblockcompare ...
::
*/
function Gifblockcompare ($GlobalBlock, $LocalBlock, $Len) {
for ($i = 0; $i < $Len; $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);
}
/*
:::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: Gifword ...
::
*/
function Gifword ($int) {
Return (Chr ($int & 0xFF). Chr (($int >> 8) & 0xFF));
}
/*
:::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: Getanimation ...
::
*/
function Getanimation () {
Return ($this->gif);
}
}
?>

http://www.bkjia.com/PHPjc/824713.html www.bkjia.com true http://www.bkjia.com/PHPjc/824713.html techarticle today at the company, the manager lets make a scrolling subtitle. However, you are not allowed to generate GIF images. So the internet to find Gifencoder this class library. Really good, but the application process also appeared a ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.