2018年1月17日 星期三

用PHP產生gif動畫圖檔

這是一件很有趣的事情

我準備了兩個png檔和 gifencoder.class.php (下載)

$frames[] 裡是 gif檔案單層的內容
$frames[] 則是間隔


這是基本樣式

header ('Content-type:image/gif');
include('gifencoder.class.php');
$text = "Hello";

// Open the first source image and add the text.
$image = imagecreatefrompng('aa.png');
$text_color = imagecolorallocate($image, 200, 200, 200);
imagestring($image, 5, 5, 5,  $text, $text_color);

// Generate GIF from the $image
// We want to put the binary GIF data into an array to be used later,
//  so we use the output buffer.
ob_start();
imagegif($image);
$frames[]=ob_get_contents();
$framed[]=40;

// Delay in the animation.
ob_end_clean();

// And again..
// Open the first source image and add the text.
$image = imagecreatefrompng('xml.png');
$text_color = imagecolorallocate($image, 200, 200, 200);
imagestring($image, 5, 20, 20,  $text, $text_color);

// Generate GIF from the $image
// We want to put the binary GIF data into an array to be used later,
//  so we use the output buffer.
ob_start();
imagegif($image);
$frames[]=ob_get_contents();
$framed[]=40;

// Delay in the animation.
ob_end_clean();

// Generate the animated gif and output to screen.
$gif = new GIFEncoder($frames,$framed,0,2,0,0,0,'bin');
echo $gif->GetAnimation();



這是上傳圖檔和內建的小圖示動畫
imagealphablending($image_icon); 可以輕易的保留透明


if ($_POST['act']=='gif') {

header ('Content-type:image/gif');
include('gifencoder.class.php');
$text = "Hello";

// Open the first source image and add the text.
$new_width =640;
$new_height = 480;


$name_ext = strtolower(substr($_FILES["file"]["name"],-4));
$tmp_name = $_FILES["file"]["tmp_name"];
if ($name_ext=='.png')
  $image = imagecreatefrompng($tmp_name);
elseif ($name_ext=='.gif') 
  $image = imagecreatefromgif($tmp_name);
else
  $image = imagecreatefromjpeg($tmp_name);

$width = imagesx($image);
$height = imagesy($image);

$image_base = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_base, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$image_icon = imagecreatefrompng('sun1.png');
$icon_width = imagesx($image_icon);
$icon_height = imagesy($image_icon);

for($i=0;$i<6;$i++) {
  $px[$i]= rand(1, $new_width-$icon_width-1);
  $py[$i]= rand(1, $new_height-$icon_height-1);
}

for($i=0;$i<6;$i++){
$image_show = imagecreatetruecolor($new_width, $new_height);
imagecopy($image_show, $image_base, 0, 0, 0, 0, $new_width, $new_height);
//imagecopy($image_show, $image_icon, $px[$i], $px[$i], 0, 0, $icon_width, $icon_height);
imagealphablending($image_icon);
//imagecopymerge($image_show, $image_icon, $px[$i], $px[$i], 0, 0, $icon_width, $icon_height, 75);
imagecopy($image_show, $image_icon, $px[$i], $py[$i], 0, 0, $icon_width, $icon_height);

//imagecopyresampled($image_base, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
//$text_color = imagecolorallocate($image_base, 100, 200, 200);
//imagestring($image_base, 5, $px[$i], $py[$i],  $text, $text_color);

// Generate GIF from the $image
// We want to put the binary GIF data into an array to be used later,
//  so we use the output buffer.
ob_start();
imagegif($image_show);
$frames[]=ob_get_contents();
$framed[]=100;
ob_end_clean();

}

// Generate the animated gif and output to screen.
$gif = new GIFEncoder($frames,$framed,0,2,0,0,0,'bin');
echo $gif->GetAnimation();
} else {
echo "
<form action='' method='post' enctype='multipart/form-data'>
<input type='hidden' name='act' value='gif'>
背景<input type='file' name='file'  />
<input type='submit' name='preview' value='Preview!'>
</form>
";

}




沒有留言:

張貼留言