PHP图像操作:3D图、缩放、旋转、裁剪、添加水印(一)

8/31/2015来源:PHP技巧人气:2041

php图像操作:3D图、缩放、旋转、裁剪、添加水印(一)

来源:http://www.ido321.com/875.html

1、利用php gd库的函数绘制3D扇形统计图

   1: <?php
   2: header("content-type","text/html;charset=utf-8");
   3: /*扇形统计图*/
   4: $image = imagecreatetruecolor(100, 100);    /*创建画布*/
   5:
   6: /*设置画布需要的颜色*/
   7: $white = imagecolorallocate($image,0xff,0xff,0xff);
   8: $gray = imagecolorallocate($image, 0xc0, 0xc0, 0xc0);
   9: $darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90);
  10: $navy = imagecolorallocate($image, 0x00, 0x00, 0x80);
  11: $darknavy = imagecolorallocate($image, 0x00, 0x00, 0x50);
  12: $red = imagecolorallocate($image, 0xff, 0x00, 0x00);
  13: $darkred = imagecolorallocate($image, 0x90, 0x00, 0x00);
  14:
  15: /*填充背景色*/
  16: imagefill($image, 0, 0, $white);
  17:
  18: /*3D制作*/
  19: for($i = 60; $i > 50; $i--)
  20: {
  21:     imagefilledarc($image, 50, $i, 100, 50, -160, 40, $darknavy, IMG_ARC_PIE);
  22:     imagefilledarc($image, 50, $i, 100, 50, 40, 75, $darkgray, IMG_ARC_PIE);
  23:     imagefilledarc($image, 50, $i, 100, 50, 75, 200, $darkred, IMG_ARC_PIE);
  24: }
  25: /*画椭圆弧并填充*/
  26: imagefilledarc($image, 50, 50, 100, 50, -160, 40, $darknavy, IMG_ARC_PIE);
  27: imagefilledarc($image, 50, 50, 100, 50, 40, 75, $darkgray, IMG_ARC_PIE);
  28: imagefilledarc($image, 50, 50, 100, 50, 75, 200, $darkred, IMG_ARC_PIE);
  29:
  30: /*画字符串*/
  31: imagestring($image, 3, 15, 55, "30%", $white);
  32: imagestring($image, 3, 45, 35, "60%", $white);
  33: imagestring($image, 3, 60, 60, "10%", $white);
  34:
  35: /*输出图像*/
  36: header("content-type:image/png");
  37: imagepng($image);
  38:
  39: /*释放资源*/
  40: imagedestroy($image);
  41: ?>

效果:

2、对图片进行缩放

   1: <div>
   2:     <h4>原图大小</h4>
   3:     <img src="1.png" style="border:1px solid red;">
   4: </div>
   5: <?php
   6: header("content-type","text/html;charset=utf-8");
   7:
   8: /*
   9: *图片缩放
  10: *@param string $filename   图片的url
  11: *@param int    $width      设置图片缩放的最大宽度
  12: *@param int    $height     设置图片缩放的最大高度
  13: */
  14: function thumb($filename,$width=130,$height=130)
  15: {
  16:     /*获取原图的大小*/
  17:     list($width_orig,$height_orig) = getimagesize($filename);
  18:
  19:     /*根据参数$width和$height,换算出等比例的高度和宽度*/
  20:     if($width && ($width_orig < $height_orig))
  21:     {
  22:         $width = ($height / $height_orig) * $width_orig;
  23:     }
  24:     else
  25:     {
  26:         $height = ($width / $width_orig) * $height_orig;
  27:     }
  28:
  29:     /*以新的大小创建画布*/
  30:     $image_p = imagecreatetruecolor($width, $height);
  31:
  32:     /*获取图像资源*/
  33:     $image = imagecreatefrompng($filename);
  34:
  35:     /*使用imagecopyresampled缩放*/
  36:     imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
  37:
  38:     /*保存缩放后的图片和命名*/
  39:     imagepng($image_p,'test.png');
  40:
  41:     /*释放资源*/
  42:     imagedestroy($image_p);
  43:     imagedestroy($image);
  44: }
  45: /*调用函数*/
  46: thumb('1.png');
  47: ?>
  48: <div>
  49:     <h4>缩放后的大小</h4>
  50:     <img src="test.png" style="border:1px solid red;">
  51: </div>

效果: