首页 > 作文

超级好用的一个php上传图片类(随机名,缩略图,加水印)

更新时间:2023-04-06 13:30:28 阅读: 评论:0

upimages.class.php php上传类


复制代码 代码如下:

<?php

class upimages {

var $annexfolder = “upload”;//附件存放点,默认为:annex

var $smallfolder = “small”;//缩略图存放路径,注:必须是放在 $annexfolder下的子目录,默认为:smallimg

var $markfolder = “mark”;//水印图片存放处

var $upfiletype = “jpg gif png”;//上传的类型,默认为:jpg gif png rar zip

var $upfilemax = 1024;//上传大小限制,单位是“kb”,默认为:1024kb

var $fonttype;//字体

var $maxwidth = 500; //图片最大宽度

var $maxheight = 600; //图片最大高度

function upimages($annexfolder,$smallfolder,$includefolder) {

$this->annexfolder = $annexfolder;

$this->smallfolder = $smallfolder;

$this->fonttype = $includefolder.”/04b_08__.ttf”;

}

function upload($inputname) {

$imagename = time();//设定当前时间为图片名称

if(@empty($_files[$inputname][“name”])) die(“没有上传图片信息,请确认”遗体告别仪式流程);

$name = explode(“.”,$_files[$inputname][“name”]);//将上传前的文件以“.”分开取得文件类型

$imgcount = count($name);//获得截取的数量地铁兄弟

$imgtype = $name[$imgcount-1];//取得文件的类型失恋之后

if(s十八的因数trpos($this->upfiletype,$imgtype) === fal) die(error(“上传文件类型仅支持 “.$this->upfiletype.” 不支持 “.$imgtype));

$photo = $imagename.”.”.$imgtype;//写入数据库的文件名

$uploadfile = $this->annexfolder.”/”.$photo;//上传后的文件名称

$upfileok = move_uploaded_file($_files[$inputname][“tmp_name”],$uploadfile);

if($upfileok) {

$imgsize = $_files[$inputname][“size”];

$ksize = round($imgsize/1024);

if($ksize > ($this->upfilemax*1024)) {

@unlink($uploadfile);

die(error(“上传文件超过 “.$this->upfilemax.”kb”));

}

} el {

die(error(“上传图片失败,请确认你的上传文件不超过 $upfilemax kb 或上传时间超时”));

}

return $photo;

}

function getinfo($photo) {

$photo = $this->annexfolder.”/”.$photo;

$imageinfo = getimagesize($photo);

$imginfo[“width”] = $imageinfo[0];

$imginfo[“height”] = $imageinfo[1];

$imginfo[“type”] = $imageinfo[2];

$imginfo[“name”] = baname($photo);

return $imginfo;

}

function smallimg($photo,$width=128,$height=128) {

$imginfo = $this->getinfo($photo);

$photo = $this->annexfolder.”/”.$photo;//获得图片源

$newname = substr($imginfo[“name”],0,strrpos($imginfo[“name”], “.”)).”_thumb.jpg”;//新图片名称

if($imginfo[“type”] == 1) {

$img = imagecreatefromgif($photo);

} elif($imginfo[“type”] == 2) {

$img = imagecreatefromjpeg($photo);

} elif($imginfo[“type”] == 3) {

$img = imagecreatefrompng($photo);

} el {

$img = “”;

}

if(empty($img)) return fal;

$width = ($width > $imginfo[“width”]) ? $imginfo[“width”] : $width;

$height = ($height > $imginfo[“height”]) ? $imginfo[“height”] : $height;

$srcw = $imginfo[“width”];

$srch = $imginfo[“height”];

if ($srcw * $width > $srch * $height) {

$height = round($srch * $width / $srcw);

} el {

$width = round($srcw * $height / $srch);

}

if (function_exists(“imagecreatetruecolor”)) {

$newimg = imagecreatetruecolor($width, $height);

imagecopyresampled($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo[“width”], $imginfo[“height”]);

} el {

$newimg = imagecreate($width, $height);

imagecopyresized($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo[“width”], $imginfo[“height”]);

}

if ($this->tofile) {

if (file_exists($this->annexfolder.”/”.$this->smallfolder.”/”.$newname)) @unlink($this->annexfolder.”/”.$this->smallfolder.”/”.$newname);

imagejpeg($newimg,$this->annexfolder.”/”.$this->smallfolder.”/”.$newname);

return $this->annexfolder.”/”.$this->smallfolder.”/”.$newname;

} el {

imagejpeg($newimg);

}

imagedestroy($newimg);

imagedestroy($img);

return $newname;

}

function watermark($photo,$text) {

$imginfo = $this->getinfo($photo);

$photo = $this->annexfolder.”/”.$photo;

$newname = substr($imginfo[“name”], 0, strrpos($imginfo[“name”], “.”)) . “_mark.jpg”;

switch ($imginfo[“type”]) {

ca 1:
我的青春我的团
$img = imagecreatefromgif($photo);

break;

ca 2:

$img = imagecreatefromjpeg($photo);

break;

ca 3:

$img = imagecreatefrompng($photo);

break;

default:

return fal;

}

if (empty($img)) return fal;

$width = ($this->maxwidth > $imginfo[“width”]) ? $imginfo[“width”] : $this->maxwidth;

$height = ($this->maxheight > $imginfo[“height”]) ? $imginfo[“height”] : $this->maxheight;

$srcw = $imginfo[“width”];

$srch = $imginfo[“height”];

if ($srcw * $width > $srch * $height) {

$height = round($srch * $width / $srcw);

} el {

$width = round($srcw * $height / $srch);

}

if (function_exists(“imagecreatetruecolor”)) {

$newimg = imagecreatetruecolor($width, $height);

imagecopyresampled($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo[“width”], $imginfo[“height”]);

} el {

$newimg = imagecreate($width, $height);

imagecopyresized($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo[“width”], $imginfo[“height”]);

}

$white = imagecolorallocate($newimg, 255, 255, 255);

$black = imagecolorallocate($newimg, 0, 0, 0);

$alpha = imagecolorallocatealpha($newimg, 230, 230, 230, 40);

imagefilledrectangle($newimg, 0, $height-26, $width, $height, $alpha);

imagefilledrectangle($newimg, 13, $height-20, 15, $height-7, $black);

imagettftext($newimg, 4.9, 0, 20, $height-14, $black, $this->fonttype, $text[0]);

imagettftext($newimg, 4.9, 0, 20, $height-6, $black, $this->fonttype, $text[1]);

if($this->tofile) {

if (file_exists($this->annexfolder.”/”.$this->markfolder.”/”.$newname)) @unlink($this->annexfolder.”/”.$this->markfolder.”/”.$newname);

imagejpeg($newimg,$this->annexfolder.”/”.$this->markfolder.”/”.$newname);

return $this->annexfolder.”/”.$this->markfolder.”/”.$newname;

} el {

imagejpeg($newimg);

}

imagedestroy($newimg);

imagedestroy($img);

return $newname;

}

}

?>

使用方法


复制代码 代码如下:

include ‘upimages.class.php’;

$max=”upload”; //文件上传路径

$mix=”small”; //缩略图路径(必须在upload下建立)

$mark=”mark”; //加水引的图片存放路径

$text = array(“oktang”,”2012″); //水印内容

$img= new upimages($max,$mix,$max); //实例化类文件

$photo = $img->upload(“file”); //上传的文件域

$img->maxwidth = $img->maxheight = 600; //设置高,和宽

$img->tofile = true;

$newsmallimg = $img->smallimg($photo);

$newmark = $img->watermark($photo,$text);

echo $newsmallimg;

echo $newmark;

echo “<img src='”.$newsmallimg.”‘ border=’0’><br><br>”;

echo “<img src='”.$newmark.”‘ border=’0’><br><br>”;

注意里面有个字体文件,大家可以从网上下载。

本文发布于:2023-04-06 13:30:27,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/d82a41e96941b0285e3110f4e5bc4bab.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:超级好用的一个php上传图片类(随机名,缩略图,加水印).doc

本文 PDF 下载地址:超级好用的一个php上传图片类(随机名,缩略图,加水印).pdf

标签:上传   图片   文件   路径
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图