Home php教程 php手册 php 图片上传代码(具有生成缩略图与增加水印功能)

php 图片上传代码(具有生成缩略图与增加水印功能)

Jun 13, 2016 am 09:48 AM
php upload and code Function Can picture Increase payment watermark source code generate

这款图片上传源代码是一款可以上传图片并且还具有给上传的图片生成缩略图与增加水印功能哦,可以说是一款完美的图片上传类哦。

php教程 图片上传代码(具有生成缩略图与增加水印功能)
这款图片上传源代码是一款可以上传图片并且还具有给上传的图片生成缩略图与增加水印功能哦,可以说是一款完美的图片上传类哦。

class upfile {
 public $filepath = "www.bKjia.c0m/"; //上传文件存放文件夹

 public $filesize = 1000000; //允许上传的大小

 //如果要修改允许上传文件的类型  请搜索 【 switch ($upfiletype) { //文件类型  】

 public $reimagesize = array (
  true, //是否生成缩略图
  400, //缩略图宽
  300,//缩略图高
  "" //缩略图存放文件夹 如果为空和当前要生成缩略图的文件在同一目录 文件前缀r_
 ); //是否生成缩略图 array(生成或不生成,缩略图宽,缩略图高,存放文件夹); 注意:存放文件夹后跟 '/'

 public $india = true; //是否打水印 true打 false不打

 public $indiaimage = ""; //水印图片地址为空则不打图片水印 如果有文字水印建议不要开启图片水印

 public $indiaimagex = 100; //图片距离图片左边距离

 public $indiaimagey = 10; //图片距离图片上面距离

 public $indiatext = "www.bKjia.c0m"; //水印文字

 public $fontsize = 6; //水印文字大小,1最小6最大

 public $indiatextx = 10; //文字距离图片左边距离

 public $indiatexty = 10; //文字距离图片上面距离

 public $r = 250; //图片颜色三原色 $r红

 public $g = 250; //$g绿

 public $b = 250; //$b蓝

 public $indiapath = ""; //加了水印的图片保存路径,如果为空就直接替代原来的图片

 //开始上传处理
 function uploadfile($upfile) {
  if ($upfile == "") {
   die("uploadfile:参数不足");
  }
  if (!file_exists($this->filepath)) {
   mkdir($this->filepath);
  }
  $upfiletype = $upfile['type'];
  $upfilesize = $upfile['size'];
  $upfiletmpname = $upfile['tmp_name'];
  $upfilename = $upfile['name'];
  $upfileerror = $upfile['error'];
  if ($upfilesize > $this->filesize) {
   return false; //文件过大
  }
  switch ($upfiletype) { //文件类型
   case 'image/jpeg' :
    $type = 'jpg';
    break;
   case 'image/pjpeg' :
    $type = 'jpg';
    break;
   case 'image/png' :
    $type = 'png';
    break;
   case 'image/gif' :
    $type = 'gif';
    break;
  }
  if (!isset ($type)) {
   return false; //不支持此类型
  }
  if (!is_uploaded_file($upfiletmpname) or !is_file($upfiletmpname)) {
   return false;
   ; //文件不是经过正规上传的;
  }
  if ($this->upfileerror != 0) {
   return false; //其他错误
  }
  if ($this->upfileerror == 0) {
   if (!file_exists($upfiletmpname)) {
    return false; //临时文件不存在
   } else {
    $filename = date("ymdhis", time() + 3600 * 8); //图片已当前时间命名
    $filename = $this->filepath . $filename . "." . $type;
    if (!move_uploaded_file($upfiletmpname, $filename)) {
     return false; //文件在移动中丢失
    } else {
     if ($this->india == true) {
      $this->goindia($filename, $type,true);
     } else {
      if ($this->reimagesize[0] == true) {
       $this->goreimagesize($filename, $type);
      } else {
       return true; //上传成功!
       unlink($upfiletmpname);
      }
     }
    }

   }
  }

 }
 //添加水印处理
 function goindia($filename, $filetype,$reimage=false) {
  if (!file_exists($filename)) {
   $this->reerror(7); //要添加水印的文件不存在
  } else {
   if ($filetype == "jpg") {
    $im = imagecreatefromjpeg($filename);
   } else
    if ($filetype == "gif") {
     $im = imagecreatefromgif($filename);
    } else
     if ($filetype == "png") {
      $im = imagecreatefrompng($filename);
     }
   if ($this->indiatext != "") { //如果水印文字不为空
    $textcolor = imagecolorallocate($im, $this->r, $this->g, $this->b); //设置文字颜色
    imagestring($im, $this->fontsize, $this->indiatextx, $this->indiatexty, $this->indiatext, $textcolor); //将文字写入图片
   }
   if ($this->indiaimage != "") {//如果水印图片不为空
    $indiaimagetype = getimagesize($this->indiaimage);
    $logow = $indiaimagetype[0]; //得到水印图片的宽
    $logoh = $indiaimagetype[1]; //得到水印图片的高
    switch ($indiaimagetype[2]) { //判断水印图片的格式
     case 1 :
      $indiaimagetype = "gif";
      $logo = imagecreatefromgif($this->indiaimage);
      break;
     case 2 :
      $indiaimagetype = "jpg";
      $logo = imagecreatefromjpeg($this->indiaimage);
      break;
     case 3 :
      $indiaimagetype = "png";
      $logo = imagecreatefrompng($this->indiaimage);
      break;
    }
    imagealphablending($im, true); //打开混色模式
    imagecopy($im, $logo, $this->indiaimagex, $this->indiaimagey, 0, 0, $logow, $logoh);
    imagedestroy($im);
    imagedestroy($logo);
   }
  }
  if ($this->indiapath == "") { //如果水印存放地址不为空
   if ($filetype == "jpg") {
    imagejpeg($im, $filename);
   } else
    if ($filetype == "gif") {
     imagegif($im, $filename);
    } else
     if ($filetype == "png") {
      imagepng($im, $filename);
     }
   if($reimage == true){
    $this->goreimagesize($filename,$filetype);
   }else{
    return true; //添加水印成功
   }
  } else {
   if (!file_exists($this->indiapath)) {
    mkdir($this->indiapath);
    return false; //请重新上传
   } else {
    $indianame = basename($filename);
    $indianame = $this->indiapath . $indianame;
    if ($filetype == "jpg") {
     imagejpeg($im, $indianame);
    } else
     if ($filetype == "gif") {
      imagegif($im, $indianame);
     } else
      if ($filetype == "png") {
       imagepng($im, $indianame);
      }
    if($reimage == true){
     $this->goreimagesize($indianame,$filetype);
     echo $indianame;
    }else{
     return true; //添加水印成功
    }
   }
  }
 }
 function goreimagesize($filename, $filetype) {
  if (!file_exists($filename)) {
   return false; //要生成缩略图的图片不存在
  } else {
   if ($filetype == 'jpg') {
    $reimage = imagecreatefromjpeg($filename);
   }
   elseif ($filetype == 'png') {
    $reimage = imagecreatefrompng($filename);
   } else
    if ($filetype == 'gif') {
     $reimage = imagecreatefromgif($filename);
    }
   if (isset ($reimage)) {
    $srcimagetype = getimagesize($filename);
    $srcimagetypew = $srcimagetype[0]; //得到原始图片宽度
    $srcimagetypeh = $srcimagetype[1]; //得到原始图片高度
    $reim = imagecreatetruecolor($this->reimagesize[1], $this->reimagesize[2]);
    imagecopyresized($reim, $reimage, 0, 0, 0, 0, $this->reimagesize[1], $this->reimagesize[2], $srcimagetypew, $srcimagetypeh);
    $reimagepath = $this->reimagesize[3];
    if ($reimagepath != "") { //如果存放水印地址不为空
     if (!file_exists($reimagepath)) {
      mkdir($reimagepath);
     } else {
      $reimagename = basename($filename);
      $reimagename = $reimagepath . "r_" . $reimagename;
      if ($filetype == "gif")
       imagegif($reim, $reimagename);
      else
       if ($filetype == "jpg")
        imagejpeg($reim, $reimagename);
       else
        if ($filetype == "png")
         imagepng($reim, $reimagename);
      return true;
     }
    } else {
     $filename = basename($filename);
     if($this->indiapath == ""){
      $filename = $this->filepath."r_" . $filename;
     }else{
      $filename = $this->indiapath."r_" . $filename;
     }
     if ($filetype == "gif")
      imagegif($reim, $filename);
     else
      if ($filetype == "jpg")
       imagejpeg($reim, $filename);
      else
       if ($filetype == "png")
        imagepng($reim, $filename);
     return true;
    }

   }
  }
 }

}
if ($_post["submit"]) {
 $file = $_files['uploadfile'];
 $upfile = new upfile();
 echo $upfile->uploadfile($file);
}
?>

 


 

 
 
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

See all articles