首页 后端开发 php教程 php 图片处理类_PHP教程

php 图片处理类_PHP教程

Jul 13, 2016 pm 05:49 PM
class image path php private 位置 图片 处理 方法

    class Image { 
        private $path; 
        //构造方法用来对图片所在位置进行初使化  
        function __construct($path="./"){ 
            $this->path=rtrim($path, "/")."/"; 
        } 
        /* 对图片进行缩放
         *
         * 参数$name: 是需要处理的图片名称
         * 参数$width:是缩放后的宽度
         * 参数$height:是缩放后的高度
         * 参数$qz: 是新图片的名称前缀
         * 返回值:就是缩放后的图片名称,失败则返回false
         *
         */ 
        function thumb($name, $width, $height, $qz="th_"){ 
            //获取图片信息  
            $imgInfo=$this->getInfo($name); //图片的宽度,高度,类型  
            //获取图片资源, 各种类型的图片都可以创建资源 jpg, gif, png  
            $srcImg=$this->getImg($name, $imgInfo); 
            //获取计算图片等比例之后的大小, $size["width"], $size["height"]  
            $size=$this->getNewSize($name, $width, $height, $imgInfo); 
            //获取新的图片资源, 处理一下gif透明背景  
            $newImg=$this->kidOfImage($srcImg, $size, $imgInfo); 
            //另存为一个新的图片,返回新的缩放后的图片名称      
            return $this->createNewImage($newImg, $qz.$name, $imgInfo);   
        } 
 
        private function createNewImage($newImg, $newName, $imgInfo){ 
            switch($imgInfo["type"]){ 
                case 1://gif  
                    $result=imageGif($newImg, $this->path.$newName); 
                    break; 
                case 2://jpg  
                    $result=imageJPEG($newImg, $this->path.$newName); 
                    break; 
                case 3://png  
                    $return=imagepng($newImg, $this->path.$newName); 
                    break; 
            } 
            imagedestroy($newImg); 
            return $newName; 
        } 
 
        private function kidOfImage($srcImg, $size, $imgInfo){ 
            $newImg=imagecreatetruecolor($size["width"], $size["height"]); 
             
            $otsc=imagecolortransparent($srcImg); 
 
            if($otsc >=0 && $otsc                 $tran=imagecolorsforindex($srcImg, $otsc); 
 
                $newt=imagecolorallocate($newImg, $tran["red"], $tran["green"], $tran["blue"]); 
 
                imagefill($newImg, 0, 0, $newt); 
 
                imagecolortransparent($newImg, $newt); 
            } 
 
            imagecopyresized($newImg, $srcImg, 0, 0, 0, 0, $size["width"], $size["height"], $imgInfo["width"], $imgInfo["height"]); 
 
            imagedestroy($srcImg); 
 
            return $newImg; 
        } 
 
        private function getNewSize($name, $width, $height, $imgInfo){ 
            $size["width"]=$imgInfo["width"]; 
            $size["height"]=$imgInfo["height"]; 
 
            //缩放的宽度如果比原图小才重新设置宽度  
            if($width                 $size["width"]=$width; 
            } 
            //缩放的高度如果比原图小才重新设置高度  
            if($height                 $size["height"]=$height; 
            } 
 
            //图片等比例缩放的算法  
            if($imgInfo["width"]*$size["width"] > $imgInfo["height"] * $size["height"]){ 
                $size["height"]=round($imgInfo["height"]*$size["width"]/$imgInfo["width"]); 
            }else{ 
                $size["width"]=round($imgInfo["width"]*$size["height"]/$imgInfo["height"]); 
            } 
 
 
            return $size; 
 
        } 
 
        private function getInfo($name){ 
            $data=getImageSize($this->path.$name); 
 
            $imageInfo["width"]=$data[0]; 
            $imageInfo["height"]=$data[1]; 
            $imageInfo["type"]=$data[2]; 
 
            return $imageInfo; 
        } 
 
        private function getImg($name, $imgInfo){ 
            $srcPic=$this->path.$name; 
 
            switch($imgInfo["type"]){ 
                case 1: //gif  
                    $img=imagecreatefromgif($srcPic); 
                    break; 
                case 2: //jpg  
                    $img=imageCreatefromjpeg($srcPic); 
                    break; 
                case 3: //png  
                    $img=imageCreatefrompng($srcPic); 
                    break; 
                default: 
                    return false; 
                 
            } 
 
            return $img; 
        } 
        /* 功能:为图片加水印图片
         * 参数$groundName: 背景图片,即需要加水印的图片
         * 参数$waterName: 水钱图片
         * 参数#aterPost:水印位置, 10种状态, 
         *  0为随机位置
         *
         *  1. 为顶端居左  2. 为顶端居中  3 为顶端居右
         *  4  为中部居左  5. 为中部居中  6 为中部居右
         *  7 . 为底端居左 8. 为底端居中, 9. 为底端居右
         *
         * 参数$qz : 是加水印后的图片名称前缀
         * 返回值:就是处理后图片的名称
         *
         */ 
        function waterMark($groundName, $waterName, $waterPos=0, $qz="wa_"){ 
         
            if(file_exists($this->path.$groundName) && file_exists($this->path.$waterName)){ 
                $groundInfo=$this->getInfo($groundName); 
                $waterInfo=$this->getInfo($waterName); 
                //水印的位置  
                if(!$pos=$this->position($groundInfo, $waterInfo, $waterPos)){ 
                    echo "水印不应该比背景图片小!"; 
                    return; 
                } 
 
                $groundImg=$this->getImg($groundName, $groundInfo); 
                $waterImg=$this->getImg($waterName, $waterInfo); 
 
                $groundImg=$this->copyImage($groundImg, $waterImg, $pos, $waterInfo); 
 
                return $this->createNewImage($groundImg, $qz.$groundName, $groundInfo); 
            }else{ 
                echo "图片或水印图片不存在"; 
                return false; 
            } 
        } 
 
        private function copyImage($groundImg, $waterImg, $pos, $waterInfo){ 
            imagecopy($groundImg, $waterImg, $pos["posX"], $pos["posY"], 0, 0, $waterInfo["width"], $waterInfo["height"]); 
            imagedestroy($waterImg); 
 
            return $groundImg; 
        } 
         
        private function position($groundInfo, $waterInfo, $waterPos){ 
            //需要背景比水印图片大  
            if(($groundInfo["width"]                 return false; 
            } 
 
            switch($waterPos){ 
                case 1: 
                    $posX=0; 
                    $posY=0; 
                    break; 
                case 2: 
                    $posX=($groundInfo["width"]-$waterInfo["width"])/2; 
                    $posY=0; 
                    break; 
                case 3: 
                    $posX=$groundInfo["width"]-$waterInfo["width"]; 
                    $posY=0; 
                    break; 
                case 4: 
                    $posX=0; 
                    $posY=($groundInfo["height"]-$waterInfo["height"]) /2; 
                    break; 
                case 5: 
                    $posX=($groundInfo["width"]-$waterInfo["width"])/2; 
                    $posY=($groundInfo["height"]-$waterInfo["height"]) /2; 
                    break; 
                case 6: 
                    $posX=$groundInfo["width"]-$waterInfo["width"]; 
                    $posY=($groundInfo["height"]-$waterInfo["height"]) /2; 
                    break; 
                case 7: 
                    $posX=0; 
                    $posY=$groundInfo["height"]-$waterInfo["height"]; 
                    break; 
                case 8: 
                    $posX=($groundInfo["width"]-$waterInfo["width"])/2; 
                    $posY=$groundInfo["height"]-$waterInfo["height"]; 
                    break; 
                case 9: 
                    $posX=$groundInfo["width"]-$waterInfo["width"]; 
                    $posY=$groundInfo["height"]-$waterInfo["height"]; 
                    break; 
                case 0: 
                default: 
                    $posX=rand(0, ($groundInfo["width"]-$waterInfo["width"])); 
                    $posY=rand(0, ($groundInfo["height"]-$waterInfo["height"])); 
                    break; 
            } 
 
            return array("posX"=>$posX, "posY"=>$posY); 
        } 
 
    } 
 class Image {
  private $path;
  //构造方法用来对图片所在位置进行初使化
  function __construct($path="./"){
   $this->path=rtrim($path, "/")."/";
  }
  /* 对图片进行缩放
   *
   * 参数$name: 是需要处理的图片名称
   * 参数$width:是缩放后的宽度
   * 参数$height:是缩放后的高度
   * 参数$qz: 是新图片的名称前缀
   * 返回值:就是缩放后的图片名称,失败则返回false
   *
   */
  function thumb($name, $width, $height, $qz="th_"){
   //获取图片信息
   $imgInfo=$this->getInfo($name); //图片的宽度,高度,类型
   //获取图片资源, 各种类型的图片都可以创建资源 jpg, gif, png
   $srcImg=$this->getImg($name, $imgInfo);
   //获取计算图片等比例之后的大小, $size["width"], $size["height"]
   $size=$this->getNewSize($name, $width, $height, $imgInfo);
   //获取新的图片资源, 处理一下gif透明背景
   $newImg=$this->kidOfImage($srcImg, $size, $imgInfo);
   //另存为一个新的图片,返回新的缩放后的图片名称 
   return $this->createNewImage($newImg, $qz.$name, $imgInfo); 
  }

  private function createNewImage($newImg, $newName, $imgInfo){
   switch($imgInfo["type"]){
    case 1://gif
     $result=imageGif($newImg, $this->path.$newName);
     break;
    case 2://jpg
     $result=imageJPEG($newImg, $this->path.$newName);
     break;
    case 3://png
     $return=imagepng($newImg, $this->path.$newName);
     break;
   }
   imagedestroy($newImg);
   return $newName;
  }

  private function kidOfImage($srcImg, $size, $imgInfo){
   $newImg=imagecreatetruecolor($size["width"], $size["height"]);
   
   $otsc=imagecolortransparent($srcImg);

   if($otsc >=0 && $otsc     $tran=imagecolorsforindex($srcImg, $otsc);

    $newt=imagecolorallocate($newImg, $tran["red"], $tran["green"], $tran["blue"]);

    imagefill($newImg, 0, 0, $newt);

    imagecolortransparent($newImg, $newt);
   }

   imagecopyresized($newImg, $srcImg, 0, 0, 0, 0, $size["width"], $size["height"], $imgInfo["width"], $imgInfo["height"]);

   imagedestroy($srcImg);

   return $newImg;
  }

  private function getNewSize($name, $width, $height, $imgInfo){
   $size["width"]=$imgInfo["width"];
   $size["height"]=$imgInfo["height"];

   //缩放的宽度如果比原图小才重新设置宽度
   if($width     $size["width"]=$width;
   }
   //缩放的高度如果比原图小才重新设置高度
   if($height     $size["height"]=$height;
   }

   //图片等比例缩放的算法
   if($imgInfo["width"]*$size["width"] > $imgInfo["height"] * $size["height"]){
    $size["height"]=round($imgInfo["height"]*$size["width"]/$imgInfo["width"]);
   }else{
    $size["width"]=round($imgInfo["width"]*$size["height"]/$imgInfo["height"]);
   }


   return $size;

  }

  private function getInfo($name){
   $data=getImageSize($this->path.$name);

   $imageInfo["width"]=$data[0];
   $imageInfo["height"]=$data[1];
   $imageInfo["type"]=$data[2];

   return $imageInfo;
  }

  private function getImg($name, $imgInfo){
   $srcPic=$this->path.$name;

   switch($imgInfo["type"]){
    case 1: //gif
     $img=imagecreatefromgif($srcPic);
     break;
    case 2: //jpg
     $img=imageCreatefromjpeg($srcPic);
     break;
    case 3: //png
     $img=imageCreatefrompng($srcPic);
     break;
    default:
     return false;
    
   }

   return $img;
  }
  /* 功能:为图片加水印图片
   * 参数$groundName: 背景图片,即需要加水印的图片
   * 参数$waterName: 水钱图片
   * 参数#aterPost:水印位置, 10种状态,
   *  0为随机位置
   *
   *  1. 为顶端居左  2. 为顶端居中  3 为顶端居右
   *  4  为中部居左  5. 为中部居中  6 为中部居右
   *  7 . 为底端居左 8. 为底端居中, 9. 为底端居右
   *
   * 参数$qz : 是加水印后的图片名称前缀
   * 返回值:就是处理后图片的名称
   *
   */
  function waterMark($groundName, $waterName, $waterPos=0, $qz="wa_"){
  
   if(file_exists($this->path.$groundName) && file_exists($this->path.$waterName)){
    $groundInfo=$this->getInfo($groundName);
    $waterInfo=$this->getInfo($waterName);
    //水印的位置
    if(!$pos=$this->position($groundInfo, $waterInfo, $waterPos)){
     echo "水印不应该比背景图片小!";
     return;
    }

    $groundImg=$this->getImg($groundName, $groundInfo);
    $waterImg=$this->getImg($waterName, $waterInfo);

    $groundImg=$this->copyImage($groundImg, $waterImg, $pos, $waterInfo);

    return $this->createNewImage($groundImg, $qz.$groundName, $groundInfo);
   }else{
    echo "图片或水印图片不存在";
    return false;
   }
  }

  private function copyImage($groundImg, $waterImg, $pos, $waterInfo){
   imagecopy($groundImg, $waterImg, $pos["posX"], $pos["posY"], 0, 0, $waterInfo["width"], $waterInfo["height"]);
   imagedestroy($waterImg);

   return $groundImg;
  }
  
  private function position($groundInfo, $waterInfo, $waterPos){
   //需要背景比水印图片大
   if(($groundInfo["width"]     return false;
   }

   switch($waterPos){
    case 1:
     $posX=0;
     $posY=0;
     break;
    case 2:
     $posX=($groundInfo["width"]-$waterInfo["width"])/2;
     $posY=0;
     break;
    case 3:
     $posX=$groundInfo["width"]-$waterInfo["width"];
     $posY=0;
     break;
    case 4:
     $posX=0;
     $posY=($groundInfo["height"]-$waterInfo["height"]) /2;
     break;
    case 5:
     $posX=($groundInfo["width"]-$waterInfo["width"])/2;
     $posY=($groundInfo["height"]-$waterInfo["height"]) /2;
     break;
    case 6:
     $posX=$groundInfo["width"]-$waterInfo["width"];
     $posY=($groundInfo["height"]-$waterInfo["height"]) /2;
     break;
    case 7:
     $posX=0;
     $posY=$groundInfo["height"]-$waterInfo["height"];
     break;
    case 8:
     $posX=($groundInfo["width"]-$waterInfo["width"])/2;
     $posY=$groundInfo["height"]-$waterInfo["height"];
     break;
    case 9:
     $posX=$groundInfo["width"]-$waterInfo["width"];
     $posY=$groundInfo["height"]-$waterInfo["height"];
     break;
    case 0:
    default:
     $posX=rand(0, ($groundInfo["width"]-$waterInfo["width"]));
     $posY=rand(0, ($groundInfo["height"]-$waterInfo["height"]));
     break;
   }

   return array("posX"=>$posX, "posY"=>$posY);
  }

 }

 摘自 chaojie2009的专栏

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/478376.htmlTechArticle?php class Image { private $path; //构造方法用来对图片所在位置进行初使化 function __construct($path=./){ $this-path=rtrim($path, /)./; } /* 对图片进行缩放...
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

CakePHP 项目配置 CakePHP 项目配置 Sep 10, 2024 pm 05:25 PM

在本章中,我们将了解CakePHP中的环境变量、常规配置、数据库配置和电子邮件配置。

适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 Dec 24, 2024 pm 04:42 PM

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

CakePHP 日期和时间 CakePHP 日期和时间 Sep 10, 2024 pm 05:27 PM

为了在 cakephp4 中处理日期和时间,我们将使用可用的 FrozenTime 类。

CakePHP 文件上传 CakePHP 文件上传 Sep 10, 2024 pm 05:27 PM

为了进行文件上传,我们将使用表单助手。这是文件上传的示例。

CakePHP 路由 CakePHP 路由 Sep 10, 2024 pm 05:25 PM

在本章中,我们将学习以下与路由相关的主题?

讨论 CakePHP 讨论 CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP 是 PHP 的开源框架。它的目的是使应用程序的开发、部署和维护变得更加容易。 CakePHP 基于类似 MVC 的架构,功能强大且易于掌握。模型、视图和控制器 gu

如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 Dec 20, 2024 am 11:31 AM

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写

CakePHP 创建验证器 CakePHP 创建验证器 Sep 10, 2024 pm 05:26 PM

可以通过在控制器中添加以下两行来创建验证器。

See all articles