Php Image Upload code (with the ability to generate thumbnails and add watermarks)

Source: Internet
Author: User
Tags imagejpeg

This image upload source code is a function that can upload images and generate thumbnails and add watermarks for uploaded images. It is a perfect Image Upload class.

Php tutorial Image Upload code (with the ability to generate thumbnails and add watermarks)
This image upload source code is a function that can upload images and generate thumbnails and add watermarks for uploaded images. It is a perfect Image Upload class.

Class upfile {
Public $ filepath = "www. bKjia. c0m/"; // upload a file storage folder

Public $ filesize = 1000000; // size of the file to be uploaded

// If You Want To modify the file type that can be uploaded, search [switch ($ upfiletype) {// file type]

Public $ reimagesize = array (
True, // whether to generate a thumbnail
400, // thumbnail width
300, // The height of the thumbnail
"" // Folder for storing thumbnails if it is empty and the file to be generated is in the same directory as the file prefix r _
); // Whether to generate a thumbnail array (generate or not generate, thumbnail width, thumbnail height, storage folder); Note: Save the folder followed '/'

Public $ india = true; // whether to add a watermark. true: false.

Public $ indiaimage = ""; // If the watermark image address is empty, no image watermark is required. If there is a text watermark, we recommend that you do not enable the image watermark.

Public $ indiaimagex = 100; // distance from the image to the left

Public $ indiaimagey = 10; // The distance between the image and the image

Public $ indiatext = "www. bKjia. c0m"; // watermark text

Public $ fontsize = 6; // watermark text size, 1 minimum 6 maximum

Public $ indiatextx = 10; // The text distance from the left of the image

Public $ indiatexty = 10; // text distance from the image

Public $ r = 250; // the three primary colors of the image $ r red

Public $ g = 250; // $ g green

Public $ B = 250; // $ B blue

Public $ indiapath = ""; // save path of the watermark image. If it is empty, the original image is replaced directly.

// 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; // The 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;
; // The file is not formally uploaded;
}
If ($ this-> upfileerror! = 0 ){
Return false; // other errors
}
If ($ this-> upfileerror = 0 ){
If (! File_exists ($ upfiletmpname )){
Return false; // The temporary file does not exist.
} Else {
$ Filename = date ("ymdhis", time () + 3600*8); // The image is named after the current time.
$ Filename = $ this-> filepath. $ filename. ".". $ type;
If (! Move_uploaded_file ($ upfiletmpname, $ filename )){
Return false; // The object is lost while moving.
} Else {
If ($ this-> india = true ){
$ This-> goindia ($ filename, $ type, true );
} Else {
If ($ this-> reimagesize [0] = true ){
$ This-> goreimagesize ($ filename, $ type );
} Else {
Return true; // upload successful!
Unlink ($ upfiletmpname );
}
}
}

}
}

}
// Add a watermark
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 text to an image
}
If ($ this-> indiaimage! = "") {// If the watermark image is not empty
$ Indiaimagetype = getimagesize ($ this-> indiaimage );
$ Logow = $ indiaimagetype [0]; // obtain the width of the watermark image.
$ Logoh = $ indiaimagetype [1]; // obtain the watermark image height.
Switch ($ indiaimagetype [2]) {// determine the watermark image format
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); // enable the mixed 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 blank
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; // watermark added successfully
}
} Else {
If (! File_exists ($ this-> indiapath )){
Mkdir ($ this-> indiapath );
Return false; // 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; // watermark added successfully
}
}
}
}
Function goreimagesize ($ filename, $ filetype ){
If (! File_exists ($ filename )){
Return false; // the image to be scaled 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]; // obtain the width of the original image.
$ Srcimagetypeh = $ srcimagetype [1]; // get the original image 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 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"/> <br/>
<Input type = "submit" value = "Upload" name = "submit"/>
</Form>

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.