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

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver Mac版
視覺化網頁開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能