Php Image Upload class. Watermarks are supported to generate thumbnail images.

Source: Internet
Author: User
Tags imagecopy imagejpeg

Share a php Image Upload class written by a netizen. It supports adding watermarks to generate a thumbnail. It includes configuration and information that can be obtained (each configuration information has a default value, if you do not have special requirements, do not configure them ):

The Code is as follows: Copy code

<? Php
/*----------------------------------------------------------------------------------
*
*----------------------------------------------------------------------------------
*/
Class image_up {
// Define basic parameters
Private $ uptype = array ('image/jpg ', 'image/jpeg', 'image/png ', 'image/pjpeg', 'image/gif ', 'image/bmp ', 'image/x-png'); // Upload File Type
Private $ max_file_size = 102400; // maximum upload size (unit: KB)
Private $ destination_folder = "up/"; // File Upload path
Private $ watermark = 1; // whether to append the watermark
Private $ watertype = 1; // watermark type (1 is text, 2 is image)
Private $ waterposition = 1; // watermark position (1 indicates the lower left corner, 2 indicates the lower right corner, 3 indicates the upper left corner, 4 indicates the upper right corner, and 5 indicates the center );
Private $ waterstring = null; // watermark string
Private $ waterimg = null; // watermark image
Private $ imgpreview = 1; // whether to generate a preview image (1 is generated, others are not generated );
Private $ imgpreviewsize = 1; // ratio of the preview image. 0 is displayed based on the fixed width and height, and others are displayed in proportion.
Private $ imgwidth = 200; // fixed width of the preview Image
Private $ imgheight = 200; // fixed height of the preview Image
// ++ ++ ++
Private $ imgthu = 1; // whether to generate and save the thumbnail. 1 is generated, and 0 is not generated.
Private $ imgthu_folder = null; // save path of the thumbnail, which is consistent with the file path by default.
Private $ imgthu_fixed = 0; // whether to use fixed width and height for a thumbnail. If the value is 1, the value 0 indicates flexible change.
Private $ imgthu_width = 200; // thumbnail width
Private $ imgthu_height = 200; // thumbnail height
 
Private $ imgthu_name = null; // thumbnail name
//************************************** **************************************** ************************************
Private $ inputname = "upfile"; // File Upload Box Name
//************************************** **************************************** ************************************
Private $ img_preview_display; // Picture preview
//************************************** **************************************** ************************************
// File upload information. The value 1 indicates that the file does not exist, the value 2 indicates that the file type does not match, the value 3 indicates that the file size exceeds the size limit, the value 4 indicates that the file fails to be uploaded, and the value 0 indicates that the file is uploaded successfully.
Private $ file_up_info = null;
// ++ ++ ++
// Obtain the basic information of the uploaded file externally
Private $ file_name; // original name of the client file
Private $ file_type; // object MIME type
Private $ file_size; // size of the uploaded file, in bytes
Private $ file_tmp_name; // temporary file name stored
Private $ file_error; // Error Code related to File Upload

Private $ img_size; // obtain the length and width of the image.
Private $ file_basename; // obtain the full name with the extension.
Private $ file_extension; // get the file extension
Private $ filename; // file name (without the extension)
Private $ destination; // price path and name
//************************************** **************************************** ************************************
Public function _ set ($ propety_name, $ value ){
$ This-> $ propety_name = $ value;
}
Public function _ get ($ property_name ){
If (isset ($ this-> $ property_name ))
Return ($ this-> $ property_name );
Else return (NULL );
}
//************************************** **************************************** ************************************
// Define the file upload function
Public function up (){
// Determine whether a file exists
If (! Is_uploaded_file ($ _ FILES [$ this-> inputname] ["tmp_name"]) {
$ This-> file_up_info = 1;
Return;
}
// Obtain and assign the corresponding Basic parameters
$ Upfile = $ _ FILES [$ this-> inputname];
$ This-> file_name = $ upfile ["name"];
$ This-> file_type = $ upfile ["type"];
$ This-> file_size = $ upfile ["size"];
$ This-> file_tmp_name = $ upfile ["tmp_name"];
$ This-> file_error = $ upfile ["error"];
// Check whether the file type is correct
If (! In_array ($ this-> file_type, $ this-> uptype )){
$ This-> file_up_info = 2;
Return;
}
// Check whether the file size exceeds the limit
If ($ this-> file_size> $ this-> max_file_size ){
$ This-> file_up_info = 3;
Return;
}
// Determine whether the directory exists
If (! File_exists ($ this-> destination_folder ))
Mkdir ($ this-> destination_folder );
// Obtain the image information and assign a value
$ This-> img_size = getimagesize ($ this-> file_tmp_name );
$ Pathinfo = pathinfo ($ this-> file_name );
$ This-> file_extension = $ pathinfo ["extension"]; // get the file extension
$ This-> file_basename = $ pathinfo ["basename"]; // obtain the full name with the extension.
$ This-> filename = $ pathinfo ["filename"]; // file name (without the extension)
$ Filename2 = $ pathinfo ['filename'];
$ This-> destination = $ this-> destination_folder. $ this-> filename. ".". $ this-> file_extension;
// Determine whether the file name exists. If yes, rename it.
$ N = 1;
While (file_exists ($ this-> destination )){
While (file_exists ($ this-> destination )){
$ N ++;
$ This-> filename = $ this-> filename. "(". $ n .")";
$ This-> destination = $ this-> destination_folder. $ this-> filename. ".". $ this-> file_extension;
}
$ This-> filename = $ filename2. "(". $ n .")";
$ This-> destination = $ this-> destination_folder. $ this-> filename. ".". $ this-> file_extension;
}
// Move the uploaded file
If (move_uploaded_file ($ this-> file_tmp_name, $ this-> destination ))
$ This-> file_up_info = 0;
Else $ this-> file_up_info = 4;

// Add a watermark
If ($ this-> watermark = 1 ){
$ This-> imgthu ();
}
// Generate a thumbnail
If ($ this-> imgthu = 1 ){
$ This-> add_watermark ();
}
// Generate a preview Image
If ($ this-> imgpreviewsize = 0 ){
If ($ this-> img_size ["0"] <$ this-> imgwidth) $ this-> imgwidth = $ this-> img_size ["0"];
If ($ this-> img_size ["1"] <$ this-> imgheight) $ this-> imgheight = $ this-> img_size ["1"];
} Else {
$ This-> imgwidth = $ this-> img_size ["0"] * $ this-> imgpreviewsize;
$ This-> imgheight = $ this-> img_size ["1"] * $ this-> imgpreviewsize;
}
$ This-> img_preview_display = " destination 'width =' $ this-> imgwidth 'height = '$ this-> imgheight'
Alt = "image preview: r file name": $ this-> file_tmp_name/> ";
}
// ================================================ ========================================================== ==============================================
// ================================================ ========================================================== ==============================================
// Generate a thumbnail
Function imgthu (){
If ($ this-> imgthu_folder = null)
$ This-> imgthu_folder = $ this-> destination_folder;

// $ This-> imgthu_name = $ this-> filename. "_ t.". $ this-> file_extension;
$ Imgthu_name_ B = $ this-> filename. "_ t ";
$ Imgthu_name_b2 = $ this-> filename. "_ t ";
$ Destination_ B = $ this-> imgthu_folder. $ imgthu_name_ B. ".". $ this-> file_extension;
// Determine whether the file name exists. If yes, rename it.
$ N = 1;
While (file_exists ($ destination_ B )){
While (file_exists ($ destination_ B )){
$ N ++;
$ Imgthu_name_ B = $ imgthu_name_ B. "(". $ n .")";
$ Destination_ B = $ this-> imgthu_folder. $ imgthu_name_ B. ".". $ this-> file_extension;
}
$ Imgthu_name_ B = $ imgthu_name_b2. "(". $ n .")";
$ Destination_ B = $ this-> imgthu_folder. $ imgthu_name_ B. ".". $ this-> file_extension;
}


$ Imginfo = getimagesize ($ this-> destination );
Switch ($ imginfo [2])
{
Case 1:
$ In = @ imagecreatefromgif ($ this-> destination );
Break;
Case 2:
$ In = @ imagecreatefromjpeg ($ this-> destination );
Break;
Case 3:
$ In = @ imagecreatefrompng ($ this-> destination );
Break;
Case 6:
$ In = @ imagecreatefrombmp ($ this-> destination );
Break;
Default:
Break;
}
// Calculate the thumbnail length and width
If ($ this-> imgthu_fixed = 0 ){
If ($ this-> imgthu_height> ($ imginfo [1]/$ imginfo [0]) * $ this-> imgthu_width)
$ This-> imgthu_width = ($ imginfo [0]/$ imginfo [1]) * $ this-> imgthu_height;
Else
$ This-> imgthu_height = ($ imginfo [1]/$ imginfo [0]) * $ this-> imgthu_width;
}
$ New = imageCreateTrueColor ($ this-> imgthu_width, $ this-> imgthu_height );
ImageCopyResized ($ new, $ in, 0, 0, 0, $ this-> imgthu_width, $ this-> imgthu_height, $ imginfo [0], $ imginfo [1]);
Switch ($ imginfo [2])
{
Case 1:
Imagejpeg ($ new, $ destination_ B );
Break;
Case 2:
Imagejpeg ($ new, $ destination_ B );
Break;
Case 3:
Imagepng ($ new, $ destination_ B );
Break;
Case 6:
Imagewbmp ($ new, $ destination_ B );
Break;
}
}
// ================================================ ========================================================== ==============================================
// ================================================ ========================================================== ==============================================
// Add a watermark
Function add_watermark (){
// 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF (intel byte order ),
// 8 = TIFF (motorola byte order), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF, 15 = WBMP, 16 = XBM.
$ Imginfo = getimagesize ($ this-> destination );
$ Im = imagecreatetruecolor ($ this-> img_size [0], $ this-> img_size [1]); // create a true color
$ White = imagecolorallocate ($ im, 255,255,255); // set the color
$ Black = imagecolorallocate ($ im, 0, 0 );
$ Red = imagecolorallocate ($ im, 255, 0, 0 );
// Fill in the x and y coordinates of the image (0 and 0 in the upper left corner of the image) with the color execution area (that is, with x, y points are in the same color and adjacent points are filled ).
Imagefill ($ im, 0, 0, $ white );

Switch ($ imginfo [2])
{
Case 1:
$ Simage = imagecreatefromgif ($ this-> destination); // create a new image from a file or URL
Break;
Case 2:
$ Simage = imagecreatefromjpeg ($ this-> destination );
Break;
Case 3:
$ Simage = imagecreatefrompng ($ this-> destination );
Break;
Case 6:
$ Simage = imagecreatefromwbmp ($ this-> destination );
Break;
Default:
Echo ("unsupported file types ");
Break;
}
If (! Empty ($ simage ))
{
// Set the location
If ($ this-> watertype = 1 ){
$ Str_len = strlen ($ this-> waterstring );
$ Str_width = $ str_len * 10;
$ Str_height = 20;
} Elseif ($ this-> watertype = 1 & file_exists ($ this-> waterimg )){
$ Iinfo = getimagesize ($ this-> waterimg );
$ Str_width = $ iinfo [0];
$ Str_height = $ iinfo [1];
}

Switch ($ this-> waterposition ){
Case 1:
$ P_x = 5;
$ P_y = $ this-> img_size [1]-$ str_height;
Break;
Case 2:
$ P_x = $ this-> img_size [0]-$ str_width;
$ P_y = $ this-> img_size [1]-$ str_height;
Break;
Case 3:
$ P_x = 5;
$ P_y = 0;
Break;
Case 4:
$ P_x = $ this-> img_size [0]-$ str_width;
$ P_y = 5;
Break;
Case 5:
$ P_x = ($ this-> img_size [0]-$ str_width)/2;
$ P_y = ($ this-> img_size [1]-$ str_height)/2;
Break;
}
Imagecopy ($ im, $ simage, $ this-> img_size [0], $ this-> img_size [1]); // copy part of the image
// Imagefilledrectangle ($ im, 1, $ this-> img_size [1]-15,130, $ this-> img_size [1], $ white ); // color the closed rectangular area of the image

Switch ($ this-> watertype)
{
Case 1: // Add a watermark string
Imagestring ($ im, 10, $ p_x, $ p_y, $ this-> waterstring, $ red );
Break;
Case 2: // Add a watermark image
$ Simage1 = imagecreatefromgif ($ this-> waterimg );
Imagecopy ($ im, $ simage1 );
Imagedestroy ($ simage1 );
Break;
}

Switch ($ imginfo [2])
{
Case 1:
// Imagegif ($ nimage, $ destination );
Imagejpeg ($ im, $ this-> destination );
Break;
Case 2:
Imagejpeg ($ im, $ this-> destination );
Break;
Case 3:
Imagepng ($ im, $ this-> destination );
Break;
Case 6:
Imagewbmp ($ im, $ this-> destination );
Break;
}
// Overwrite the original uploaded file
Imagedestroy ($ im );
Imagedestroy ($ simage );
}
}
}
?>


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.