PHP is very powerful to use a very simple PHP upload class _php Tutorial

Source: Internet
Author: User
Tags imagejpeg
uploading.class.php Upload class

Class Upimages {
var $annexFolder = "Upload";//Attachment storage point, default: Annex
var $smallFolder = "small";//thumbnail storage path, note: Must be placed under the $annexFolder subdirectory, default: Smallimg
var $markFolder = "Mark";//Watermark Picture Storage Place
var $upFileType = "jpg gif png";//upload type, default: JPG gif png rar zip
var $upFileMax = 1024;//upload size limit in KB, default: 1024KB
var $fontType;//font
var $maxWidth = 500; Max width of picture
var $maxHeight = 600; Maximum height of picture
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 as picture names
if (@empty ($_files[$inputName ["name"]) die ("Do not upload picture information, please confirm");
$name = Explode (".", $_files[$inputName ["name"]);//upload the file before uploading to "." Get file types separately
$imgCount = count ($name);//Get the number of intercepts
$imgType = $name [$imgCount -1];//Gets the type of the file
if (Strpos ($this->upfiletype, $imgType) = = = False) die (Error ("Upload file type only". $this->upfiletype. "Not supported". $imgType));
$photo = $imageName. ".". $imgType;//write the file name of the database
$uploadFile = $this->annexfolder. " /". $photo;//file name after uploading
$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 exceeds". $this->upfilemax. " KB "));
}
} else {
Die (Error ("Upload image failed, please verify that 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 a 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;
}
}
?>


Start call (use method)

Include uploading.class.php;
$max = "Upload"; File Upload Path
$mix = "small"; Thumbnail path (must be established under upload)
$mark = "Mark"; Picture storage path of water-adding primer
$text = Array ("Oktang", "2012"); Watermark Content
$img = new Upimages ($max, $mix, $max); Instantiating a class file
$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 "

";
echo "

";

PHP is very powerful to use a very simple PHP upload class, I hope to help you.

http://www.bkjia.com/PHPjc/486186.html www.bkjia.com true http://www.bkjia.com/PHPjc/486186.html techarticle uploading. class.php Upload class? PHP class Upimages {var $annexFolder = "Upload";//Attachment point, Default: Annex var $smallFolder = "Small ";//thumbnail storage path, note: Must ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.