一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

PHP图像图形处理入门教程

时间:2010-08-02 编辑:简简单单 来源:一聚教程网

1 生成一个简单图像。
2 设定图像的颜色。
3 在图像上绘制直线。
4 在图像上显示文字。
5 在图像中显示中文字符。
6 打开已存在的图片。
7 获取图片的相关属性。
8 函数getimagesize()的用法。
9 为上传图片添加水印效果。
10 生成已有图片的缩略图。
11 使用函数imagecopyresampled()。
12 生成带有底纹的数字验证码图片的php程序。

*/

//1 生成一个简单图像。

 代码如下 复制代码

$width = 200;
$height =200;

$img =  imagecreatetruecolor($width,$height) or die("不支持gd图像处理");
imagepng($img);
imagedestroy($img);

//2 设定图像的颜色。

 代码如下 复制代码

$width = 200;
$height =200;

$img =  imagecreatetruecolor($width,$height) or die("不支持gd图像处理");

$bg_color = imagecolorallocate($img, 255, 0, 0);
imagefill($img, 0, 0, $bg_color);

imagepng($img);
imagedestroy($img);

//3 在图像上绘制直线。

 代码如下 复制代码

$width = 200;
$height =300;

$img =  imagecreatetruecolor($width,$height) or die("不支持gd图像处理");

$line_color = imagecolorallocate($img, 255, 255, 255);
imageline($img,0,40,200,40,$line_color);
imageline($img,0,260,200,260,$line_color);

imagepng($img);
imagedestroy($img);

//4 在图像上显示文字。

 代码如下 复制代码

$width = 200;
$height =300;

$img =  imagecreatetruecolor($width,$height) or die("不支持gd图像处理");
$line_color = imagecolorallocate($img, 255, 255, 255);

imageline($img, 0, 40, 200, 40, $line_color);
imageline($img, 0, 260, 200, 260, $line_color);
imagestring($img, 5, 0, 60, "it's time to learn php!", $line_color);

imagepng($img);
imagedestroy($img);

//5 在图像中显示中文字符。

 代码如下 复制代码

$width = 200;
$height =300;

$img =  imagecreatetruecolor($width,$height) or die("不支持gd图像处理");
$line_color = imagecolorallocate($img, 255, 255, 255);
$font_type ="c://windows//fonts//simli.ttf";    //获取truetype字体,采用隶书字体

//“西游记”3个字16进制字符
$cn_char1 = chr(0xe8).chr(0xa5).chr(0xbf);
$cn_char2 = chr(0xe6).chr(0xb8).chr(0xb8);
$cn_char3 = chr(0xe8).chr(0xae).chr(0xb0);

//“吴承恩著”4个字16进制字符
$cn_str = chr(0xe5).chr(0x90).chr(0xb4).chr(0xe6).chr(0x89).chr(0xbf).chr(0xe6).chr(0x81).chr(0xa9);
$cn_str .= " ".chr(0xe8).chr(0x91).chr(0x97);

imageline($img, 0, 40, 200, 40, $line_color);
imageline($img, 0, 260, 200, 260, $line_color);

//竖排显示“西游记”3字
imagettftext($img, 30, 0, 10, 80, $line_color, $font_type,$cn_char1);
imagettftext($img, 30, 0, 10, 120, $line_color, $font_type,$cn_char2);
imagettftext($img, 30, 0, 10, 160, $line_color, $font_type,$cn_char3);

//横排显示“吴承恩著”4字
imagettftext($img, 15, 0, 90, 254, $line_color, $font_type,$cn_str);

imagepng($img);
imagedestroy($img);

//6 打开已存在的图片。
$img=imagecreatefromjpeg("tower.jpg");

imagejpeg($img);
imagedestroy($img);

//7 获取图片的相关属性。
$img=imagecreatefromjpeg("tower.jpg");

$x = imagesx($img);
$y = imagesy($img);
echo "图片tower.jpg的宽为:$x pixels";
echo "
";
echo "
";
echo "图片tower.jpg的高为:$y pixels";

//8 函数getimagesize()的用法。
$img_info=getimagesize("tower.jpg");

for($i=0; $i<4; ++$i)
{
    echo $img_info[$i];
    echo "
";
}

?>

 
//9 为上传图片添加水印效果。

 

 代码如下 复制代码

function makeimagewatermark($image,$pos,$water_text,$font_size,$color)
{
    $font_type ="c://windows//fonts//cour.ttf";
   
    if(!empty($image) && file_exists($image))
    {
        $img_info = getimagesize($image);
        $g_w    = $img_info[0];     //取得背景图片的宽
        $g_h    = $img_info[1];     //取得背景图片的高

        switch($img_info[2])        //取得背景图片的格式
        {
            case 1:
                $img = imagecreatefromgif($image);
                break;
            case 2:
                $img = imagecreatefromjpeg($image);
                break;
            case 3:
                $img = imagecreatefrompng($image);
                break;
            default:
                die("图片格式错误!");
        }
    }
    else
    {
        die("需要加水印的图片不存在!");
    }
   
    //取得使用 truetype 字体的文本的范围
    $temp = imagettfbbox(ceil($font_size*2.5),0,$font_type,$water_text);
    $w = $temp[2] - $temp[6];
    $h = $temp[3] - $temp[7];
    if(($g_w<$w) || ($g_h<$h))
    {
        echo "需要加水印的图片的大小比水印文字区域小,无法生成水印!";
        return;
    }
   
    //设置4种水印效果位置:0和默认是随机位置,1为顶端居左,2为中部居中,3为底端居右
    switch($pos)
    {
        case 0:
            $pos_x = rand(0,($g_w - $w));
            $pos_y = rand(0,($g_h - $h));
            break;
        case 1:
            $pos_x = 0;
            $pos_y = 0;
            break;
        case 2:
            $pos_x = ($g_w - $w)/2;
            $pos_y = ($g_h - $h)/2;
            break;
        case 3:
            $pos_x = $g_w - $w;
            $pos_y = $g_h - $h;
            break;
        default:
            $pos_x = rand(0,($g_w - $w));
            $pos_y = rand(0,($g_h - $h));
            break;
    }
   
    imagealphablending($img, true);    //设置图像混色模式
   
    if(!empty($color) && (strlen($color)==7))
    {
        $r = hexdec(substr($color,1,2));
        $g = hexdec(substr($color,3,2));
        $b = hexdec(substr($color,5));
    }
    else
    {
        die("水印文字颜色格式不正确!");
    }
    $text_color = imagecolorallocate($img, $r, $g, $b);
   
    imagettftext($img, $font_size, 0, $pos_x, $pos_y, $text_color, $font_type, $water_text);
    switch($img_info[2])                      //取得背景图片的格式
    {
        case 1:
            imagegif($img,$image);
            break;
        case 2:
            imagejpeg($img,$image);
            break;
        case 3:
            imagepng($img,$image);
            break;
        default:
            die("不被支持格式的图片!");
    }
   
    imagedestroy($img);
}

if(isset($_files) && !empty($_files['userfile']) && $_files['userfile']['size']>0)
{
    $uploadfile = "./".time()."_".$_files['userfile']['name'];
    if (copy($_files['userfile']['tmp_name'], $uploadfile))
    {
        makeimagewatermark($uploadfile,2,"photo by mac",16,"#43042a");
        echo "";
    }
    else
    {
        echo "文件上传错误!
";
    }
}
?>


19-9.php


选择上传图片:


 

 
//10 生成已有图片的缩略图。

 

 代码如下 复制代码

$img_name="tower.jpg";
$src_img=imagecreatefromjpeg($img_name);

$ow=imagesx($src_img);            //取得原图的宽
$oh=imagesy($src_img);            //取得原图的高

$nw=round($ow*200.0/$ow);         //计算新图的宽度
$nh=round($oh*200.0/$oh);         //计算新图的高度

$desc_img=imagecreate($nw, $nh);  //建立新图

imagecopyresized($desc_img, $src_img, 0, 0, 0, 0, $nw, $nh, $ow, $oh);
imagejpeg($desc_img);

imagedestroy($desc_img);
imagedestroy($src_img);

//11 使用函数imagecopyresampled()。

$img_name="tower.jpg";
$percent = 0.2;

$src_img=imagecreatefromjpeg($img_name);

$ow=imagesx($src_img);
$oh=imagesy($src_img);

$nw=$ow * $percent;
$nh=$oh * $percent;

$desc_img = imagecreatetruecolor($nw, $nh);
imagecopyresampled($desc_img,$src_img,0, 0, 0, 0, $nw, $nh, $ow, $oh);

imagejpeg($desc_img);
imagedestroy($desc_img);
imagedestroy($src_img);

//12 生成带有底纹的数字验证码图片的php程序。

$img_height=60;
$img_width=20;

for($tmpa=0; $tmpa<4; ++$tmpa)
{
    $nmsg[$tmpa]=dechex(rand(0,15));            //生成随机数,并转成十六进制,作为验证码
}

$aimg = imagecreate($img_height,$img_width);    //生成图片
imagecolorallocate($aimg, 255,255,255);         //图片底色
$black = imagecolorallocate($aimg, 0,0,0);      //定义需要的黑色

//用黑色的矩形把图片包围
imagerectangle($aimg,0,0,$img_height-1,$img_width-1,$black);

//下面的代码生成底纹,其实就是在图片上生成一些符号
for($i=0; $i<100; ++$i)
{
    //使用*行号作为底纹,为了使底纹看起来杂乱无章、五颜六色,需要一个个地生成,同时其位置、颜色,大小都用随机数
    imagestring($aimg,1,mt_rand(1,$img_height),mt_rand(1,$img_width),"*",
    imagecolorallocate($aimg,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)));
}

//生成验证码,同样的道理,验证码一个个地输出到图片上,同时其位置、颜色,大小都用随机数
for($i=0; $i {
    imagestring($aimg, mt_rand(3,5),$i*$img_height/4+mt_rand(1,10),mt_rand(1,$img_width/4),
    $nmsg[$i],imagecolorallocate($aimg,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)));
}

header("content-type: image/png");
imagepng($aimg);
imageiestroy($aimg);

热门栏目