This image upload source code is a can upload pictures and also have to upload images to generate thumbnails and add watermark function Oh, can be said to be a perfect picture upload class Oh.
PHP Tutorial Image upload code (with generate thumbnails and add watermark function)
This image upload source code is a can upload pictures and also have to upload images to generate thumbnails and add watermark function Oh, can be said to be a perfect picture upload class Oh.
Class Upfile {
Public $filepath = "www.bKjia.c0m/"; Upload file Folder
Public $filesize = 1000000; Size allowed for uploads
If you want to modify the type of file that is allowed to upload, search for "switch ($upfiletype) {//File type"
Public $reimagesize = Array (
True,//whether to generate thumbnails
400,//thumbnail width
300,//thumbnail Image Height
""//Thumbnail storage folder if empty and current file to generate thumbnail in the same directory file prefix r_
); Whether to generate thumbnails of the array (generated or not generated, thumbnail width, thumbnail height, storage folder); Note: Store folder followed by '/'
Public $india = true; Whether water is printed true hit false not hit
Public $indiaimage = ""; Watermark image address is empty do not hit the picture watermark if there is a text watermark suggest do not open the picture watermark
Public $indiaimagex = 100; Picture distance from picture left
Public $indiaimagey = 10; Picture distance from picture above
Public $indiatext = "www.bKjia.c0m"; Watermark Text
Public $fontsize = 6; Watermark Text Size, 1 min 6 max
Public $INDIATEXTX = 10; Text distance from picture left
Public $indiatexty = 10; Text distance from picture above
Public $r = 250; Picture color Three primary colors $r red
Public $g = 250; $g Green
Public $b = 250; $b Blue
Public $indiapath = ""; Watermark image to save the path, if it is empty directly replace the original picture
Start upload processing
function UploadFile ($upfile) {
if ($upfile = = "") {
Die ("UploadFile: Insufficient parameters");
}
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; File too large
}
Switch ($upfiletype) {//File type
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; This type is not supported
}
if (!is_uploaded_file ($upfiletmpname) or!is_file ($upfiletmpname)) {
return false;
; Documents are not formally uploaded;
}
if ($this->upfileerror! = 0) {
return false; Other errors
}
if ($this->upfileerror = = 0) {
if (!file_exists ($upfiletmpname)) {
return false; Temporary file does not exist
} else {
$filename = Date ("Ymdhis", Time () + 3600 * 8); The picture is named at the current time
$filename = $this->filepath. $filename. "." . $type;
if (!move_uploaded_file ($upfiletmpname, $filename)) {
return false; The file is missing in the move
} else {
if ($this->india = = True) {
$this->goindia ($filename, $type, true);
} else {
if ($this->reimagesize[0] = = True) {
$this->goreimagesize ($filename, $type);
} else {
return true; Upload success!
Unlink ($upfiletmpname);
}
}
}
}
}
}
Add watermark Processing
function Goindia ($filename, $filetype, $reimage =false) {
if (!file_exists ($filename)) {
$this->reerror (7); The file to which you want to add a watermark does not exist
} else {
if ($filetype = = "jpg") {
$im = Imagecreatefromjpeg ($filename);
} else
if ($filetype = = "gif") {
$im = Imagecreatefromgif ($filename);
} else
if ($filetype = = "png") {
$im = Imagecreatefrompng ($filename);
}
if ($this->indiatext! = "") {//If the watermark text is not empty
$textcolor = Imagecolorallocate ($im, $this->r, $this->g, $this->b); Set Text color
Imagestring ($im, $this->fontsize, $this->indiatextx, $this->indiatexty, $this->indiatext, $textcolor); Write text to a picture
}
if ($this->indiaimage! = "") {//If the watermark picture is not empty
$indiaimagetype = getimagesize ($this->indiaimage);
$logow = $indiaimagetype [0]; Get the width of the watermark picture
$logoh = $indiaimagetype [1]; Get a high watermark image
Switch ($indiaimagetype [2]) {//To determine the format of the watermark picture
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); Turn on process color mode
Imagecopy ($im, $logo, $this->indiaimagex, $this->indiaimagey, 0, 0, $logow, $logoh);
Imagedestroy ($im);
Imagedestroy ($logo);
}
}
if ($this->indiapath = = "") {//If the watermark storage address is not empty
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; Add Watermark Succeeded
}
} else {
if (!file_exists ($this->indiapath)) {
mkdir ($this->indiapath);
return false; Please re-upload
} 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; Add Watermark Succeeded
}
}
}
}
function Goreimagesize ($filename, $filetype) {
if (!file_exists ($filename)) {
return false; The picture to generate the thumbnail does not exist
} 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]; Get the original picture width
$srcimagetypeh = $srcimagetype [1]; Get the original picture height
$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 the store watermark address is not empty
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);
}
?>
http://www.bkjia.com/PHPjc/633045.html www.bkjia.com true http://www.bkjia.com/PHPjc/633045.html techarticle This image upload source code is a can upload pictures and also have to upload images to generate thumbnails and add watermark function Oh, can be said to be a perfect picture upload class Oh. PHP teaches ...