【www.gdgbn.com--php入门】

本文章利用用大量的例子来讲解php教程代码对图片操作的讲解。下面来看看一个个实例教程吧.

$height = 300;
$width = 300;
//创建背景图
$im = imagecreatetruecolor($width, $height);
//分配颜色
$white = imagecolorallocate ($im, 255, 255, 255);
$blue = imagecolorallocate ($im, 0, 0, 64);
//绘制颜色至图像中
imagefill($im, 0, 0, $blue);
//绘制字符串:hello,php
imagestring($im, 10, 100, 120, "hello,php", $white);
//输出图像,定义头
header ("content-type: image/png");
//将图像发送至浏览器
imagepng($im);
//清除资源
imagedestroy($im);
?>

实例二生成缩略图片

header("content-type: image/jpeg");
// 载入图像
$imagen1 = imagecreatefromjpeg("imagen1.jpg");
$imagen2 = imagecreatefromjpeg("imagen2.jpg");

// 复制图像
imagecopy($imagen1,$imagen2,0,0,0,0,200,150);

// 输出jpeg图像
imagejpeg($imagen1);

//释放内存
imagedestroy($imagen2);
imagedestroy($imagen1);

?>

获取图片大小信息

$info = getimagesize("imagen2.jpg");
print_r($info);

?>

绘制png图片

//png格式图像处理函数
function loadpng ($imgname) {
    $im = @imagecreatefrompng ($imgname);
    if (!$im) {    //载入图像失败                     
        $im = imagecreate (400, 30);     
        $bgc = imagecolorallocate ($im, 255, 255, 255);
        $tc  = imagecolorallocate ($im, 0, 0, 0);
       imagefilledrectangle ($im, 0, 0, 150, 30, $bgc);
       imagestring($im, 4, 5, 5, "error loading: $imgname", $tc);
    }
    return $im;
 }
 $imgpng=loadpng("./karte.png");
   /* 输出图像到浏览器 */
 header("content-type: image/png");
 imagepng($imgpng);
 ?>


给图片加文字

  //创建 100*30 图像
  $im = imagecreate(100, 30);
  // white background and blue text
  $bg = imagecolorallocate($im, 200, 200, 200);
  $textcolor = imagecolorallocate($im, 0, 0, 255);
 
  // write the string at the top left
  imagestring($im, 5, 0, 0, "hello world!", $textcolor);
 
  // output the image
header ("content-type: image/jpeg");
imagejpeg ($im);
imagedestroy($im);

?>

本文来源:http://www.gdgbn.com/jiaocheng/27795/