php image upload code (with thumbnails and watermarking function)

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags address class code file file type file_exists files filesize

php tutorial image upload code (with thumbnails and watermarking function)
This picture upload source code is a picture can be uploaded and also has to generate thumbnails for uploaded images and add watermarking function Oh, can be said to be a perfect image upload category Oh.

class upfile {
public $ filepath = "www.jzread.com/"; / / upload files stored folder

public $ filesize = 1000000; / / allow the size of the upload

// If you want to modify the type of upload files, please search 【switch ($ upfiletype) {// file type】

public $ reimagesize = array (
true, / / ​​whether to generate thumbnails
400, // thumbnail width
300, // thumbnail high
"" // thumbnail storage folder if empty and the current thumbnail file to be generated in the same directory file prefix r_
); / / Whether to generate a thumbnail array (generated or not generated, thumbnail width, thumbnail high, store the folder); Note: The storage folder followed by '/'

public $ india = true; // Whether to hit the watermark true 打 打 不 打

public $ indiaimage = ""; / / watermark picture address is empty, do not play picture watermark If there is a text watermark is not recommended to open the picture watermark

public $ indiaimagex = 100; // picture from the left of the picture

public $ indiaimagey = 10; // picture distance from the picture above

public $ indiatext = "www.jzread.com"; // watermark text

public $ fontsize = 6; // watermark text size, a minimum of 6 maximum

public $ indiatextx = 10; // text distance from the left of the picture

public $ indiatexty = 10; // text distance from the picture above

public $ r = 250; / / picture color three primary colors $ r red

public $ g = 250; // $ g green

public $ b = 250; // $ b blue

public $ indiapath = ""; / / add a watermark image save path, if empty, directly replace the original picture

// start the upload process
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 is 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;
; / / File is 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 has been named at the current time
$ filename = $ this-> filepath. $ filename. ".". $ type;
if (! move_uploaded_file ($ upfiletmpname, $ filename)) {
return false; // The file is missing on 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; // uploaded successfully!
unlink ($ upfiletmpname);
}
}
}

}
}

}
// Add watermarking
function goindia ($ filename, $ filetype, $ reimage = false) {
if (! file_exists ($ filename)) {
$ this-> reerror (7); // The file to add the 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 the text color
imagestring ($ im, $ this-> fontsize, $ this-> indiatextx, $ this-> indiatexty, $ this-> indiatext, $ textcolor); // Write the text to the picture
}
if ($ this-> indiaimage! = "") {// If the watermark image is not empty
$ indiaimagetype = getimagesize ($ this-> indiaimage);
$ logow = $ indiaimagetype [0]; / / Get watermark picture width
$ logoh = $ indiaimagetype [1]; / / get watermark picture high
switch ($ indiaimagetype [2]) {// Judge the format of the watermark image
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 blending 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 success
}
} else {
if (! file_exists ($ this-> indiapath)) {
mkdir ($ this-> indiapath);
return false; // Please upload again
} 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 success
}
}
}
}
function goreimagesize ($ filename, $ filetype) {
if (! file_exists ($ filename)) {
return false; // The image to be thumbnails 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 width of the original image
$ srcimagetypeh = $ srcimagetype [1]; // Get the height of the original image
$ 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 stored 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);
}
?>

<form action = "" method = "post" enctype = "multipart / form-data">
<input type = "file" name = "uploadfile" />
<input type = "submit" value = "upload" name = "submit" />
</ form>

Related Article

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.