macframe.php_imagesetpixel

[#12]

Scott Evernden (doctor3d at gmail) [2008-04-25 18:25:28]

here's my version of imagecreatefromtga() that's been tested to work for targa 16 .. adapted from offering by zehao dot chang at gmail dot com

function imagecreatefromtga($filename)

{

$data = file_get_contents($filename);

// Extract header information

$string_length = fileint($data, 1, 1);

$data_type = fileint($data, 2, 1);

$width = fileint($data, 12, 2);

$height = fileint($data, 14, 2);

$bits_per_pixel = fileint($data, 16, 1);

$bytes_per_pixel = (int) $bits_per_pixel / 8;

// Currently only supporting RGB Data type

switch ($data_type)        // Header information taken from http://astronomy.swin.edu.au/~pbourke/dataformats/tga/

{

case 2:     // Uncompressed RGB image

break;

case 0:     // No attached image data

case 1:     // Uncompressed color-mapped image

case 3:     // Uncompressed black and white image

case 9:     // Runlength encoded color-mapped image

case 10:    // Runlength encoded RGB image

case 11:    // Compressed black and white image

case 32:    // Compressed color-mapped data, using Huffman, Delta, and runlength encoding

case 33:    // Compressed color-mapped data, using Huffman, Delta, and runlength encoding.  4-pass quadtree-type process

default:

return NULL;    // No support for any of these types

}

// Compute things we need from the header information

$pointer = 18 + $string_length;

$x = 0;  $y = $height - 1;

$image = imagecreatetruecolor($width, $height);

while ($pointer 

{

if ($bytes_per_pixel == 2)// TARGA 16 - ARRRRRGG GGGBBBBB

{

$word = fileint($data, $pointer, 2);

$r = ($word & 0x7C00) >> 10;

$g = ($word & 0x03E0) >> 5;

$b = ($word & 0x001F);

imagesetpixel($image, $x, $y, $r <

}

else if ($bytes_per_pixel == 3)// TARGA 24 - RRRRRRRR GGGGGGGG BBBBBBBB

{

imagesetpixel($image, $x, $y, fileint($data, $pointer, 3));

}

else if ($bytes_per_pixel == 4)// TARGA 32 - AAAAAAAA RRRRRRRR GGGGGGGG BBBBBBBB

{

imagesetpixel($image, $x, $y, fileint($data, $pointer + 1, 3));

}

if (++$x == $width)

{

$y--;

$x = 0;

}

$pointer += $bytes_per_pixel;

}

return $image;

}

function fileint($data, $pos, $len)

{

return base_convert(bin2hex(strrev(substr($data, $pos, $len))), 16, 10);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
验证码可以用 PHP GD 库来生成图像,然后把图像输出到浏览器上。以下是一个简单的 PHP 验证码实现: ```php <?php // 开始会话 session_start(); // 验证码中使用的字符集 $charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; // 创建一个随机字符串 $code = ''; for ($i = 0; $i < 4; $i++) { $code .= $charset[mt_rand(0, strlen($charset) - 1)]; } // 将随机字符串存储到会话中 $_SESSION['code'] = $code; // 创建一个 100x30 像素的图像 $image = imagecreatetruecolor(100, 30); // 设置背景色为白色 $bg_color = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $bg_color); // 生成干扰线 for ($i = 0; $i < 5; $i++) { $line_color = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); imageline($image, mt_rand(0, 100), mt_rand(0, 30), mt_rand(0, 100), mt_rand(0, 30), $line_color); } // 生成干扰点 for ($i = 0; $i < 50; $i++) { $pixel_color = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); imagesetpixel($image, mt_rand(0, 100), mt_rand(0, 30), $pixel_color); } // 将随机字符串写入图像 $text_color = imagecolorallocate($image, 0, 0, 0); imagestring($image, 5, 20, 8, $code, $text_color); // 发送图像到浏览器 header('Content-type: image/png'); imagepng($image); // 销毁图像 imagedestroy($image); ?> ``` 在 HTML 表单中使用验证码时,需要将用户输入的验证码和会话中存储的验证码进行比较,以验证用户输入的是否正确。比较的代码如下: ```php <?php // 开始会话 session_start(); if ($_POST['code'] == $_SESSION['code']) { // 验证码输入正确 } else { // 验证码输入错误 } ?> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值