2012年6月27日星期三

php图片处理:尺寸|复制|合并|缩放|旋转|灰度|模糊处理

图片处理-php 

  1.获取图片尺寸 
  $filename = "img/photo.jpg"; 
  list($width, $height, $type, $attrib) = getimagesize($filename); 
  echo ""; 

  2.剪切图片复制内容到剪贴板 
  代码:
  //原始图像 
  $src_img = imageCreateFromPNG("big.png"); 
  //创建图像 
  $dst_img = imageCreateTrueColor(200, 200); 
  //剪切图像 
  imageCopy ($dst_img, $src_img, 0, 0, 50, 50, 200, 200); 
  //生成剪切后的图像 
  imagePNG($dst_img, "small.png"); 
  //销毁内存中的图像 
  imageDestroy($dst_img); 
  imageDestroy($src_img); 

  3.合并图片 
  //绘制椭圆形 
  $dst_img = imageCreateTrueColor (200, 200); 
  imageFilledRectangle($dst_img, 0, 0, 200, 200, 
  imageColorAllocate($dst_img, 255, 255, 255)); 
  $dst_blue = imageColorAllocate($dst_img, 0, 0, 255); 
  imageFilledEllipse($dst_img, 100, 100, 180, 140, $dst_blue); 
  //绘制三角形 
  $src_img = imageCreateTrueColor (200, 200); 
  imageFilledRectangle($src_img, 0, 0, 200, 200, 
  imageColorAllocate($src_img, 255, 255, 255)); 
  $src_red = imageColorAllocate($src_img, 255, 0, 0); 
  imageFilledPolygon($src_img, array(100, 20, 180, 180, 20, 180), 3, $src_red); 
  //图像的合并 
  imageCopyMerge($dst_img, $src_img, 0, 0, 0, 0, 200, 200, 75); 
  //生成PNG图片 
  header("Content-type: image/png"); 
  imagePNG($dst_img); 
  //消除内存图像 
  imageDestroy($dst_img); 
  imageDestroy($src_img); 

  4.缩放图像
  //指定的图像 
  $filename = 'photo.png'; 
  //获得图像的尺寸 
  list($width, $height) = getImageSize($filename); 
  //新图像的大小 
  $new_width = $width * 0.5; 
  $new_height = $height * 0.5; 
  //生成缩略图 
  $image_d = imageCreateTrueColor ($new_width, $new_height); 
  $image_s = imageCreatefromPNG ($filename); 
  imageCopyResampled ($image_d, $image_s, 0, 0, 0, 0, 
  $new_width, $new_height, $width, $height); 
  //输出JPEG图像 
  header ('Content-type: image/jpeg'); 
  imageJPEG($image_d, null, 99); //实现99%压缩 

  5.旋转图像
  //载入图片 
  $filename = 'photo.png'; 
  $image = imageCreateFromPNG($filename); 
  //旋转60度,没有覆盖到的地方使用白色 
  $rotate = imageRotate($image, 60, imageColorAllocate($image, 255, 255, 255)); 
  //输出图像 
  header('Content-type: image/jpeg'); 
  imageJPEG($rotate); 
  imageDestroy($rotate); 
  imageDestroy($image); 

  6.灰度处理 
  //打开图像 
  $im = imageCreateFromPNG('dave.png'); 
  if ($im && imageFilter($im, IMG_FILTER_GRAYSCALE)) 
  { 
  echo '灰度处理成功!'; 
  imagePNG($im, 'dave.png'); 
  } else { 
  echo '灰度处理失败!'; 
  } 
  //消除内存图像 
  imageDestroy($im); 

  7.模糊处理
  
  //打开图像 
  $im = imageCreateFromPNG('sean.png'); 
  if ($im && imageFilter($im, IMG_FILTER_GAUSSIAN_BLUR)) 
  { 
  echo '亮度改变成功!'; 
  imagePNG($im, 'sean.png'); 
  }else{ 
  echo '亮度改变失败!'; 
  }
  //消除内存图像 
  imageDestroy($im); 

没有评论:

发表评论

上海松善实业有限公司

    上海松善实业有限公司是一家集多品牌销售于一体的电线电缆骨干企业,公司成立于2016年。 公司拥有国内各大品牌:起帆、远东、上上、江南、胜华等。     主要产品有:高低压电力电缆、橡套电缆、控制电缆、架空绝缘电缆、塑胶电缆、电子计算机电缆、通讯电缆、...