php 图片上传代码(具有生成缩略图与增加水印功能)_PHP教程
这款图片上传源代码是一款可以上传图片并且还具有给上传的图片生成缩略图与增加水印功能哦,可以说是一款完美的图片上传类哦。
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);
}
?>

热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

本教程演示了如何使用PHP有效地处理XML文档。 XML(可扩展的标记语言)是一种用于人类可读性和机器解析的多功能文本标记语言。它通常用于数据存储

JWT是一种基于JSON的开放标准,用于在各方之间安全地传输信息,主要用于身份验证和信息交换。1.JWT由Header、Payload和Signature三部分组成。2.JWT的工作原理包括生成JWT、验证JWT和解析Payload三个步骤。3.在PHP中使用JWT进行身份验证时,可以生成和验证JWT,并在高级用法中包含用户角色和权限信息。4.常见错误包括签名验证失败、令牌过期和Payload过大,调试技巧包括使用调试工具和日志记录。5.性能优化和最佳实践包括使用合适的签名算法、合理设置有效期、

静态绑定(static::)在PHP中实现晚期静态绑定(LSB),允许在静态上下文中引用调用类而非定义类。1)解析过程在运行时进行,2)在继承关系中向上查找调用类,3)可能带来性能开销。

字符串是由字符组成的序列,包括字母、数字和符号。本教程将学习如何使用不同的方法在PHP中计算给定字符串中元音的数量。英语中的元音是a、e、i、o、u,它们可以是大写或小写。 什么是元音? 元音是代表特定语音的字母字符。英语中共有五个元音,包括大写和小写: a, e, i, o, u 示例 1 输入:字符串 = "Tutorialspoint" 输出:6 解释 字符串 "Tutorialspoint" 中的元音是 u、o、i、a、o、i。总共有 6 个元

PHP的魔法方法有哪些?PHP的魔法方法包括:1.\_\_construct,用于初始化对象;2.\_\_destruct,用于清理资源;3.\_\_call,处理不存在的方法调用;4.\_\_get,实现动态属性访问;5.\_\_set,实现动态属性设置。这些方法在特定情况下自动调用,提升代码的灵活性和效率。

PHP和Python各有优势,选择依据项目需求。1.PHP适合web开发,尤其快速开发和维护网站。2.Python适用于数据科学、机器学习和人工智能,语法简洁,适合初学者。

PHP在电子商务、内容管理系统和API开发中广泛应用。1)电子商务:用于购物车功能和支付处理。2)内容管理系统:用于动态内容生成和用户管理。3)API开发:用于RESTfulAPI开发和API安全性。通过性能优化和最佳实践,PHP应用的效率和可维护性得以提升。

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7
