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(strpos($this->upfiletype,$imgtype) === false) 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"));
}
} else {
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"] = basename($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);
} elseif($imginfo["type"] == 2) {
$img = imagecreatefromjpeg($photo);
} elseif($imginfo["type"] == 3) {
$img = imagecreatefrompng($photo);
} else {
$img = "";
}
if(empty($img)) return false;
$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);
} else {
$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"]);
} else {
$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;
} else {
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"]) {
case 1:
$img = imagecreatefromgif($photo);
break;
case 2:
$img = imagecreatefromjpeg($photo);
break;
case 3:
$img = imagecreatefrompng($photo);
break;
default:
return false;
}
if (empty($img)) return false;
$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);
} else {
$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"]);
} else {
$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;
} else {
imagejpeg($newimg);
}
imagedestroy($newimg);
imagedestroy($img);
return $newname;
}
}