登录  /  注册
博主信息
博文 20
粉丝 0
评论 2
访问量 36663
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
PHP添加水印,压缩,剪切的封装操作图片类
会飞的码蚁的博客
原创
1093人浏览过
<?php
class Image
{
  private $info;
  private $image;
  public $type;
  public function __construct($src)
  {
    $this->info=getimagesize($src);
    $this->type=image_type_to_extension($this->info['2'],false);
    $fun="imagecreatefrom{$this->type}";
    $this->image=$fun($src);
  }
  /**
   * 文字水印
   * @param [type] $font   字体
   * @param [type] $content 内容
   * @param [type] $size   文字大小
   * @param [type] $col   文字颜色(四元数组)
   * @param array  $location 位置
   * @param integer $angle  倾斜角度
   * @return [type]
   */
  public function fontMark($font,$content,$size,$col,$location,$angle=0){
    $col=imagecolorallocatealpha($this->image, $col['0'], $col['1'], $col['2'],$col['3']);
    imagettftext($this->image, $size, $angle, $location['0'], $location['1'], $col,$font,$content);
  }
  /**
   * 图片水印
   * @param [type] $imageMark 水印图片地址
   * @param [type] $dst    水印图片在原图片中的位置
   * @param [type] $pct    透明度
   * @return [type]
   */
  public function imageMark($imageMark,$dst,$pct){
    $info2=getimagesize($imageMark);
    $type=image_type_to_extension($info2['2'],false);
    $func2="imagecreatefrom".$type;
    $water=$func2($imageMark);
    imagecopymerge($this->image, $water, $dst[0], $dst[1], 0, 0, $info2['0'], $info2['1'], $pct);
    imagedestroy($water);
  }
  /**
   * 压缩图片
   * @param [type] $thumbSize 压缩图片大小
   * @return [type]      [description]
   */
  public function thumb($thumbSize){
    $imageThumb=imagecreatetruecolor($thumbSize[0], $thumbSize[1]);
    imagecopyresampled($imageThumb, $this->image, 0, 0, 0, 0, $thumbSize[0], $thumbSize[1], $this->info['0'], $this->info['1']);
    imagedestroy($this->image);
    $this->image=$imageThumb;
  }
  /**
   * 裁剪图片
   * @param [type] $cutSize 裁剪大小
   * @param [type] $location 裁剪位置
   * @return [type]      [description]
   */
  public function cut($cutSize,$location){
    $imageCut=imagecreatetruecolor($cutSize[0],$cutSize[1]);
    imagecopyresampled($imageCut, $this->image, 0, 0, $location[0], $location[1],$cutSize[0],$cutSize[1],$cutSize[0],$cutSize[1]);
    imagedestroy($this->image);
    $this->image=$imageCut;
  }
  /**
   * 展现图片
   * @return [type] [description]
   */
  public function show(){
    header("content-type:".$this->info['mime']);
    $funn="image".$this->type;
    $funn($this->image);
  }
  /**
   * 保存图片
   * @param [type] $newname 新图片名
   * @return [type]     [description]
   */
  public function save($newname){
    header("content-type:".$this->info['mime']);
    $funn="image".$this->type;
    $funn($this->image,$newname.'.'.$this->type);
  }
  public function __destruct(){
    imagedestroy($this->image);
  }
}
?>

免责声明:转载自PHP中文网,非原创

本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学