GD
ImageConvolution update
Posted July 14th, 2007 by MgcclAdrian Philipp fixed a transparency patch for the ImageConvolution function I fixed up earlier. thx to him, cheers to PHP4 users(or PHP5 user without bundled GD)~
function ImageConvolution($src, $filter, $filter_div, $offset){ if ($src==NULL) { return 0; } $sx = imagesx($src); $sy = imagesy($src); $srcback = ImageCreateTrueColor ($sx, $sy); ImageAlphaBlending($srcback, false); ImageAlphaBlending($src, false); ImageCopy($srcback, $src,0,0,0,0,$sx,$sy); if($srcback==NULL){ return 0; } for ($y=0; $y<$sy; ++$y){ for($x=0; $x<$sx; ++$x){ $new_r = $new_g = $new_b = 0; $alpha = imagecolorat($srcback, @$pxl[0], @$pxl[1]); $new_a = ($alpha >> 24); for ($j=0; $j<3; ++$j) { $yv = min(max($y - 1 + $j, 0), $sy - 1); for ($i=0; $i<3; ++$i) { $pxl = array(min(max($x - 1 + $i, 0), $sx - 1), $yv); $rgb = imagecolorat($srcback, $pxl[0], $pxl[1]); $new_r += (($rgb >> 16) & 0xFF) * $filter[$j][$i]; $new_g += (($rgb >> 8) & 0xFF) * $filter[$j][$i]; $new_b += ($rgb & 0xFF) * $filter[$j][$i]; $new_a += ((0x7F000000 & $rgb) >> 24) * $filter[$j][$i]; } } $new_r = ($new_r/$filter_div)+$offset; $new_g = ($new_g/$filter_div)+$offset; $new_b = ($new_b/$filter_div)+$offset; $new_a = ($new_a/$filter_div)+$offset; $new_r = ($new_r > 255)? 255 : (($new_r < 0)? 0:$new_r); $new_g = ($new_g > 255)? 255 : (($new_g < 0)? 0:$new_g); $new_b = ($new_b > 255)? 255 : (($new_b < 0)? 0:$new_b); $new_a = ($new_a > 127)? 127 : (($new_a < 0)? 0:$new_a); $new_pxl = ImageColorAllocateAlpha($src, (int)$new_r, (int)$new_g, (int)$new_b, $new_a); if ($new_pxl == -1) { $new_pxl = ImageColorClosestAlpha($src, (int)$new_r, (int)$new_g, (int)$new_b, $new_a); } if (($y >= 0) && ($y < $sy)) { imagesetpixel($src, $x, $y, $new_pxl); } } } imagedestroy($srcback); return 1; }
Pretty soon, this function will be forgotten. According to PHP's official website, PHP4 is going to die in 2007-12-31 and the funeral of the security updates' will take place in 2008-08-08. PHP5, the newer PHP version that support imageconvolution natively, will take a place in most decent hosting companies.
- Mgccl's blog
- Add new comment
- 402 reads
imagedestroy VS unset
Posted May 23rd, 2007 by MgcclBoring days drives the sanest people do craziest jobs.
Glad I'm not one of them. Still, I benchmarked imagedestroy() and unset() function with a little script:
<?php $cool = imagecreatefrompng('imagefile.png'); echo memory_get_usage(),'<br />'; $timeparts = explode(' ',microtime()); $starttime = $timeparts[1].substr($timeparts[0],1); $cool = imagecreatefrompng('primespiral2000.png'); //imagedestroy($cool); unset($cool); $timeparts = explode(' ',microtime()); $endtime = $timeparts[1].substr($timeparts[0],1); echo bcsub($endtime,$starttime,6),'<br />'; echo memory_get_usage(),'<br />'; ?>
This script proves unset() uses less memory and it's the better choice. unset() result less memory is most likely because unset() actually delete the variable, while imagedestroy() only clean up the image structure inside GD.
- Mgccl's blog
- Add new comment
- 374 reads
Pong
Posted April 9th, 2007 by MgcclNever ending Pong animation... Just a entry for the PHP GD contest in June.

ob_start(); include('GIFEncoder.class.php'); $board_width = 60; $board_height = 60; $pad_width = 5; $pad_height = 15; $ball_size = 5; $game_width = $board_width - $pad_width*2 - $ball_size; $game_height = $board_height-$ball_size; //FIND ALL Points //Initial point $x = 0; $y = rand(0,$game_height); $xv = rand(1,10); $yv = rand(1,10); $pt[] = array($x,$y); do{ $x += $xv; $y += $yv;
- Mgccl's blog
- Add new comment
- Read more
- 287 reads
(Simple version)Matrix like animated dropping character effect in PHP
Posted March 30th, 2007 by MgcclThis code is only for monospaced typefaces, else it will look strange. This is a simplified version of the dropping down characters(or Matrix Digital Rain), I will create more complex ones later.
The end result is something like this

Like most animated effects I will ever make, it needs GIFEncoder class.
The basic idea:
On a screen, when one pixel is activated, it lights up. Here, instead of a pixel, it is a character, so I just have to construct a character grid and light them when it is activated.
Although I did not use any code from Nazmul Hassan's PHP Matrix, he is the one who inspired me to make a animated version, and he is especially kindly to give the source code to me. 
include('GIFEncoder.class.php'); ob_start(); $msg = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~!@#$%^&*()-_=+`[]{};:?/.,<>\\|"'; $length = strlen($msg); $max_x = 400; //image x size $max_y = 400; //image y size $font_file = 'cour.ttf';//font, best monospace $font_size = 10;//font site $grid = 10;//gridient of the fonts $measuer = imagettfbbox(10,0,$font_file,'1'); $space = 5;//space between letters $char_height = $measuer[1] - $measuer[7] + $space; $char_width = $measuer[2] - $measuer[0] + $space; $i = 1; $colorimage = imagecreatetruecolor(1,1); $color[1] = imagecolorallocate($colorimage, 255, 255, 255); while($i<$grid){ $color[] = imagecolorallocate($colorimage, 0, 255-20*$i, 0); ++$i; } imagedestroy($colorimage); //ENOUGH TEXT TO FILL THE PAGE. $width = ceil($max_x/$char_width); $height = ceil($max_y/$char_height); $x = 0; while($x < $width){ $y = 0; while($y < $height){ $text[$x][$y] = $msg[rand(0,$length)]; ++$y; } $drop[$x] = rand(0, $width); ++$x; } $i = 0; while($i<$height+$grid){ $image = imagecreatetruecolor($max_x, $max_y); $x = 0; while($x < $width){ $y = 0; while($y < $drop[$x]){ if($drop[$x]-$y<=$grid){ imagettftext($image, $font_size, 0, $x*$char_width, ($y+1)*$char_height-$space, $color[$drop[$x]-$y], $font_file, $text[$x][$y]); } ++$y; } if($drop[$x]<$height+$grid){ ++$drop[$x]; }else{ $drop[$x] = 0; } ++$x; } imagegif($image); $imagedata[$i] = ob_get_contents(); imagedestroy($image); ob_clean(); ++$i; } $gif = new GIFEncoder( $imagedata, 100, 0, 2, 0, 0, 0, "bin" ); Header ( 'Content-type:image/gif' ); echo $gif->GetAnimation ();
Please download your own true type fonts.
- Mgccl's blog
- 10 comments
- 26508 reads
Matrix like effect in PHP(Animated)
Posted March 29th, 2007 by MgcclThis is an slow(give it a set_time_limit(10)) and dirty animated matrix ever changing data grid effect:

You have to have the GIFEncoder class and GD library in order to use make this effect work
include('GIFEncoder.class.php'); $max_x = 468; $max_y = 60; $step = 10; $font_file = 'cour.ttf'; $font_size = 10; $color = pow(255,2);
- Mgccl's blog
- Add new comment
- Read more
- 718 reads







Recent comments
2 days 22 hours ago
3 days 7 hours ago
3 days 15 hours ago
3 days 15 hours ago
3 days 17 hours ago
3 days 18 hours ago
3 days 18 hours ago
4 days 36 min ago
5 days 10 hours ago
5 days 17 hours ago