Php 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. Php teaches you how to upload the source code of this image. It can also generate thumbnails and add watermarks for the uploaded image. it can be said that this 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 );
}
?>
Bytes. Php Tutorial...