Upimages.class.php PHP Upload Class
Copy Code code as follows:
<?php
Class Upimages {
var $annexFolder = "Upload";//Attachment storage point, default: Annex
var $smallFolder = "small";//thumbnail store path, note: Must be a subdirectory under the $annexFolder, default is: smallimg
var $markFolder = "Mark";//Watermark Image Store
var $upFileType = "jpg gif png";//type of upload, default: JPG gif png rar zip
var $upFileMax = 1024;//Upload size limit, the unit is "KB" and the default is: 1024KB
var $fontType;//font
var $maxWidth = 500; Picture Maximum width
var $maxHeight = 600; Picture Max height
function Upimages ($annexFolder, $smallFolder, $includeFolder) {
$this->annexfolder = $annexFolder;
$this->smallfolder = $smallFolder;
$this->fonttype = $includeFolder. " /04b_08__. TTF ";
}
function UpLoad ($inputName) {
$imageName = time ();//set current times to picture name
if (@empty ($_files[$inputName] ["name"]) die ("Do not upload picture information, please confirm");
$name = Explode (".", $_files[$inputName] ["name"]);//will be uploaded before the file with "." Get file types separately
$imgCount = count ($name);//number of intercepts obtained
$imgType = $name [$imgCount -1];//gets the type of file
if (Strpos ($this->upfiletype, $imgType) = = False) die (Error ("Upload file type support only". $this->upfiletype. "Unsupported". $imgType));
$photo = $imageName. ". $imgType//file name written to the database
$uploadFile = $this->annexfolder. " /". $photo;//file name after upload
$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 ("Upload file over". $this->upfilemax. " KB "));
}
} else {
Die ("Upload image failed, please make sure your upload file does not exceed $upFileMax KB or upload time timeout"));
}
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;//Get Picture source
$newName = substr ($imgInfo ["Name"],0,strrpos ($imgInfo ["name"], "."). " _thumb.jpg ";//New picture name
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, $height -20, $height-7, $black);
Imagettftext ($NEWIMG, 4.9, 0, $height -14, $black, $this->fonttype, $text [0]);
Imagettftext ($NEWIMG, 4.9, 0, $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;
}
}
?>
How to use
Copy Code code as follows:
Include ' Upimages.class.php ';
$max = "Upload"; File Upload Path
$mix = "small"; Thumbnail path (must be established under upload)
$mark = "Mark"; Add water to the picture store path
$text = Array ("Oktang", "2012"); Watermark Content
$img = new Upimages ($max, $mix, $max); Instantiating class files
$photo = $img->upload ("file"); Uploaded file fields
$img->maxwidth = $img->maxheight = 600; Set high, and wide
$img->tofile = true;
$NEWSMALLIMG = $img->smallimg ($photo);
$newMark = $img->watermark ($photo, $text);
Echo $newSmallImg;
Echo $newMark;
echo "<br><br> ';
echo "<br><br> ';
Note that there is a font file, you can download from the Internet.