0

0

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

php中文网

php中文网

发布时间:2016-07-13 10:45:03

|

996人浏览过

|

来源于php中文网

原创

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

神采PromeAI
神采PromeAI

将涂鸦和照片转化为插画,将线稿转化为完整的上色稿。

下载

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); } ?>        

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/633045.htmlTechArticle这款图片上传源代码是一款可以上传图片并且还具有给上传的图片生成缩略图与增加水印功能哦,可以说是一款完美的图片上传类哦。 php教...

相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

php

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
Java空对象相关教程合集
Java空对象相关教程合集

本专题整合了Java空对象相关教程,阅读专题下面的文章了解更多详细内容。

0

2026.01.29

clawdbot ai使用教程 保姆级clawdbot部署安装手册
clawdbot ai使用教程 保姆级clawdbot部署安装手册

Clawdbot是一个“有灵魂”的AI助手,可以帮用户清空收件箱、发送电子邮件、管理日历、办理航班值机等等,并且可以接入用户常用的任何聊天APP,所有的操作均可通过WhatsApp、Telegram等平台完成,用户只需通过对话,就能操控设备自动执行各类任务。

19

2026.01.29

clawdbot龙虾机器人官网入口 clawdbot ai官方网站地址
clawdbot龙虾机器人官网入口 clawdbot ai官方网站地址

clawdbot龙虾机器人官网入口:https://clawd.bot/,clawdbot ai是一个“有灵魂”的AI助手,可以帮用户清空收件箱、发送电子邮件、管理日历、办理航班值机等等,并且可以接入用户常用的任何聊天APP,所有的操作均可通过WhatsApp、Telegram等平台完成,用户只需通过对话,就能操控设备自动执行各类任务。

16

2026.01.29

Golang 网络安全与加密实战
Golang 网络安全与加密实战

本专题系统讲解 Golang 在网络安全与加密技术中的应用,包括对称加密与非对称加密(AES、RSA)、哈希与数字签名、JWT身份认证、SSL/TLS 安全通信、常见网络攻击防范(如SQL注入、XSS、CSRF)及其防护措施。通过实战案例,帮助学习者掌握 如何使用 Go 语言保障网络通信的安全性,保护用户数据与隐私。

8

2026.01.29

俄罗斯Yandex引擎入口
俄罗斯Yandex引擎入口

2026年俄罗斯Yandex搜索引擎最新入口汇总,涵盖免登录、多语言支持、无广告视频播放及本地化服务等核心功能。阅读专题下面的文章了解更多详细内容。

567

2026.01.28

包子漫画在线官方入口大全
包子漫画在线官方入口大全

本合集汇总了包子漫画2026最新官方在线观看入口,涵盖备用域名、正版无广告链接及多端适配地址,助你畅享12700+高清漫画资源。阅读专题下面的文章了解更多详细内容。

210

2026.01.28

ao3中文版官网地址大全
ao3中文版官网地址大全

AO3最新中文版官网入口合集,汇总2026年主站及国内优化镜像链接,支持简体中文界面、无广告阅读与多设备同步。阅读专题下面的文章了解更多详细内容。

350

2026.01.28

php怎么写接口教程
php怎么写接口教程

本合集涵盖PHP接口开发基础、RESTful API设计、数据交互与安全处理等实用教程,助你快速掌握PHP接口编写技巧。阅读专题下面的文章了解更多详细内容。

11

2026.01.28

php中文乱码如何解决
php中文乱码如何解决

本文整理了php中文乱码如何解决及解决方法,阅读节专题下面的文章了解更多详细内容。

16

2026.01.28

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PHP课程
PHP课程

共137课时 | 10.1万人学习

JavaScript ES5基础线上课程教学
JavaScript ES5基础线上课程教学

共6课时 | 11.2万人学习

PHP新手语法线上课程教学
PHP新手语法线上课程教学

共13课时 | 0.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

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