Php watermarks and uploads images. php class

Source: Internet
Author: User
Tags file size file upload imagecopy imagejpeg php class rand

/*
* Function: PHP image watermark (watermarks support images or text)
* Parameters:
* $ GroundImage: specifies the background image to be Watermark. Currently, only GIF, JPG, and PNG formats are supported;
* $ WaterPos watermark position, which has 10 statuses. 0 indicates a random position;
* 1 is the top left, 2 is the top center, and 3 is the top right;
* 4: center left, 5: center, and 6: center right;
* 7 indicates that the bottom is left, 8 indicates that the bottom is center, and 9 indicates that the bottom is right;
* $ WaterImage: specifies the image Watermark. Currently, only the GIF, JPG, and PNG formats are supported;
* $ WaterText text watermark: uses text as a watermark. It supports ASCII codes and does not support Chinese characters;
* $ TextFont text size. The value is 1, 2, 3, 4, or 5. The default value is 5;
* $ TextColor text color. The value is a hexadecimal color value. The default value is # FF0000 (red );
*
* Note: Support GD 2.0, Support FreeType, GIF Read, GIF Create, JPG, and PNG
* $ WaterImage and $ waterText should not be used at the same time. Select one of them and use $ waterImage first.
* When $ waterImage is valid, $ waterString, $ stringFont, and $ stringColor are invalid.
* The name of the watermark image is the same as that of $ groundImage.
* Author: longware @ 2004-11-3 14:15:13
*/
Function imageWaterMark ($ groundImage, $ waterPos = 0, $ waterImage = "", $ waterText = "", $ textFont = 5, $ textColor = "# FF0000 & Prime ;)
{
$ IsWaterImage = FALSE;
$ FormatMsg = "this file format is not supported Currently. Use image processing software to convert images to GIF, JPG, and PNG formats .";

// Read the watermark file
If (! Emptyempty ($ waterImage) & file_exists ($ waterImage ))
{
$ IsWaterImage = TRUE;
$ Water_info = getimagesize ($ waterImage );
$ Water_w = $ water_info [0]; // Obtain the watermark image width.
$ Water_h = $ water_info [1]; // Get the watermark image height

Switch ($ water_info [2]) // Obtain the watermark image format
{
Case 1: $ water_im = imagecreatefromgif ($ waterImage); break;
Case 2: $ water_im = imagecreatefromjpeg ($ waterImage); break;
Case 3: $ water_im = imagecreatefrompng ($ waterImage); break;
Default: die ($ formatMsg );
}
}

// Read the background image
If (! Emptyempty ($ groundImage) & file_exists ($ groundImage ))
{
$ Ground_info = getimagesize ($ groundImage );
$ Ground_w = $ ground_info [0]; // Obtain the width of the background image.
$ Ground_h = $ ground_info [1]; // get the height of the background image

Switch ($ ground_info [2]) // Obtain the background image format
{
Case 1: $ ground_im = imagecreatefromgif ($ groundImage); break;
Case 2: $ ground_im = imagecreatefromjpeg ($ groundImage); break;
Case 3: $ ground_im = imagecreatefrompng ($ groundImage); break;
Default: die ($ formatMsg );
}
}
Else
{
Die ("the image to be watermarked does not exist !");
}

// Watermark Position
If ($ isWaterImage) // image watermark
{
$ W = $ water_w;
$ H = $ water_h;
$ Label = "image ";
}
Else // text watermark
{
$ Temp = imagettfbbox (ceil ($ textFont * 5), 0, "./cour. ttf", $ waterText); // get the text range using the TrueType font
$ W = $ temp [2]-$ temp [6];
$ H = $ temp [3]-$ temp [7];
Unset ($ temp );
$ Label = "text area ";
}
If ($ ground_w <$ w) | ($ ground_h <$ h ))
{
Echo "the length or width of the image to be watermarked is smaller than that of the watermark". $ label. ", the watermark cannot be generated !";
Return;
}
Switch ($ waterPos)
{
Case 0: // random
$ PosX = rand (0, ($ ground_w-$ w ));
$ PosY = rand (0, ($ ground_h-$ h ));
Break;
Case 1: // 1 is the top left
$ PosX = 0;
$ PosY = 0;
Break;
Case 2: // 2 center the top
$ PosX = ($ ground_w-$ w)/2;
$ PosY = 0;
Break;
Case 3: // 3: top right
$ PosX = $ ground_w-$ w;
$ PosY = 0;
Break;
Case 4: // 4 is left in the middle
$ PosX = 0;
$ PosY = ($ ground_h-$ h)/2;
Break;
Case 5: // 5 center in the middle
$ PosX = ($ ground_w-$ w)/2;
$ PosY = ($ ground_h-$ h)/2;
Break;
Case 6: // 6 is the center and right
$ PosX = $ ground_w-$ w;
$ PosY = ($ ground_h-$ h)/2;
Break;
Case 7: // 7 is left at the bottom
$ PosX = 0;
$ PosY = $ ground_h-$ h;
Break;
Case 8: // 8 is centered at the bottom
$ PosX = ($ ground_w-$ w)/2;
$ PosY = $ ground_h-$ h;
Break;
Case 9: // 9: right
$ PosX = $ ground_w-$ w;
$ PosY = $ ground_h-$ h;
Break;
Default: // random
$ PosX = rand (0, ($ ground_w-$ w ));
$ PosY = rand (0, ($ ground_h-$ h ));
Break;
}

// Set the mixed color mode of the image
Imagealphablending ($ ground_im, true );

If ($ isWaterImage) // image watermark
{
Imagecopy ($ ground_im, $ water_im, $ posX, $ posY, 0, 0, $ water_w, $ water_h); // Copy the watermark to the target file
}
Else // text watermark
{
If (! Emptyempty ($ textColor) & (strlen ($ textColor) = 7 ))
{
$ R = hexdec (substr ($ textColor, 1, 2 ));
$ G = hexdec (substr ($ textColor, 3, 2 ));
$ B = hexdec (substr ($ textColor, 5 ));
}
Else
{
Die ("incorrect watermark text color format !");
}
Imagestring ($ ground_im, $ textFont, $ posX, $ posY, $ waterText, imagecolorallocate ($ ground_im, $ R, $ G, $ B ));
}

// The image after the watermark is generated
@ Unlink ($ groundImage );
Switch ($ ground_info [2]) // Obtain the background image format
{
Case 1: imagegif ($ ground_im, $ groundImage); break;
Case 2: imagejpeg ($ ground_im, $ groundImage); break;
Case 3: imagepng ($ ground_im, $ groundImage); break;
Default: die ($ errorMsg );
}

// Release the memory
If (isset ($ water_info) unset ($ water_info );
If (isset ($ water_im) imagedestroy ($ water_im );
Unset ($ ground_info );
Imagedestroy ($ ground_im );
}
//-----------------------------
$ Id = $ _ REQUEST ['id'];
$ Num = count ($ _ FILES ['userfile'] ['name']);
Print_r ($ _ FILES ['userfile']);
Print_r ($ _ FILES ['userfile'] ['name']);

Echo $ num;
Echo "<bR> ";
If (isset ($ id )){
For ($ I = 0; $ I <$ id; $ I ++ ){

If (isset ($ _ FILES )&&! Emptyempty ($ _ FILES ['userfile']) & $ _ FILES ['userfile'] ['size']> 0)
{
$ Uploadfile = "./". time (). "_". $ _ FILES ['userfile'] [name] [$ I];
Echo "<br> ";
Echo $ uploadfile;
If (copy ($ _ FILES ['userfile'] ['tmp _ name'] [$ I], $ uploadfile ))
{
Echo "OK <br> ";

// Text watermark
// ImageWaterMark ($ uploadfile, 5, "", "HTTP: // www.lvye.info", 5, "# cccccc");

// Image watermark
Watermark waterimage##logo_ok1.gif "; // watermark image path
ImageWaterMark ($ uploadfile, 9, $ waterImage );

Echo " ";
}
Else
{
Echo "Fail <br> ";
}
}
}
}

?>
<Form enctype = "multipart/form-data" method = "POST">
<? Php
For ($ a = 0; $ a <$ id; $ a ++ ){
Echo "file: <input name =" userfile [] "type =" file "> <br> ";

}
?>
<Input type = "submit" value = "upload">
</Form>
?>

// The following code uploads an image and watermarks it.

Parameter description:
$ Max_file_size: size limit of the uploaded file, in bytes
$ Destination_folder: File Upload path
$ Watermark: whether to add a watermark. (1 indicates adding a watermark; others indicates not adding a watermark );
Instructions for use:
1. Remove the number before the line "extension = php_gd2.dll" in the PHP. Ini file because the GD library is used;
2. Change extension_dir = to the directory where your php_gd2.dll is located;
3. asp tutorial "> http://www.111cn.net/php.asp;
**************************************** ************/
// Upload file type list
$ Uptypes = array (
'Image/jpg ',
'Image/jpeg ',
'Image/png ',
'Image/pjpeg ',
'Image/GIF ',
'Image/bmp ',
'Image/x-png'
);
$ Max_file_size = 2000000; // size limit of uploaded files, in bytes
$ Destination_folder = "uploadimg/"; // file Upload path
$ Watermark = 1; // whether to add a watermark. (1 indicates adding a watermark. Otherwise, no watermark is added );
$ Watertype = 1; // watermark type (1 is text, 2 is image)
$ 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 );
$ Waterstring = "http://www.xplore.cn/"; // watermark string
$ Waterimg = "xplore.gif"; // watermark image
$ Imgpreview = 1; // whether to generate a preview image (1 is generated, others are not generated );
$ Imgpreviewsize = 1/2; // Thumbnail ratio
?>
<Html>
<Head>
<Title> ZwelL image upload program </title>
<Style type = "text/css tutorial">
<! --
Body
{
Font-size: 9pt;
}
Input
{
Background-color: #66 CCFF;
Border: 1px inset # CCCCCC;
}
-->
</Style>
</Head>
<Body>
<Form enctype = "multipart/form-data" method = "post" name = "upform">
Upload files:
<Input name = "upfile" type = "file">
<Input type = "submit" value = "upload"> <br>
The file type that can be uploaded is: <? = Implode (',', $ uptypes)?>
</Form>
<? Php
If ($ _ SERVER ['request _ method'] = 'post ')
{
If (! Is_uploaded_file ($ _ FILES ["upfile"] [tmp_name])
// Whether a file exists
    {
Echo "the image does not exist at www.111cn.net! ";
Exit;
    }
$ File = $ _ FILES ["upfile"];
If ($ max_file_size <$ file ["size"])
// Check the file size
    {
Echo "the file is too large! ";
Exit;
    }
If (! In_array ($ file ["type"], $ uptypes ))
// Check the file type
    {
Echo "file type does not match! ". $ File [" type "];
Exit;
    }
If (! File_exists ($ destination_folder ))
    {
Mkdir ($ destination_folder );
    }
$ Filename = $ file ["tmp_name"];
$ Image_size = getimagesize ($ filename );
$ Pinfo = pathinfo ($ file ["name"]);
$ Ftype = $ pinfo ['extension'];
$ Destination = $ destination_folder.time (). ".". $ ftype;
If (file_exists ($ destination) & $ overwrite! = True)
    {
Echo "a file with the same name already exists ";
Exit;
    }
If (! Move_uploaded_file ($ filename, $ destination ))
    {
Echo "an error occurred while moving the file ";
Exit;
    }
$ Pinfo = pathinfo ($ destination );
$ Fname = $ pinfo [basename];
Echo "<font color = red> uploaded successfully </font> <br> File name: <font color = blue> ". $ destination_folder. $ fname. "</font> <br> ";
Echo "width:". $ image_size [0];
Echo "length:". $ image_size [1];
Echo "<br> size:". $ file ["size"]. "bytes ";
If ($ watermark = 1)
    {
$ Iinfo = getimagesize ($ destination, $ iinfo );
$ Nimage = imagecreatetruecolor ($ image_size [0], $ image_size [1]);
$ White = imagecolorallocate ($ nimage, 255,255,255 );
$ Black = imagecolorallocate ($ nimage, 0, 0 );
$ Red = imagecolorallocate ($ nimage, 255, 0, 0 );
Imagefill ($ nimage, 0, 0, $ white );
Switch ($ iinfo [2])
        {
Case 1:
$ Simage = imagecreatefromgif ($ destination );
Break;
Case 2:
$ Simage = imagecreatefromjpeg ($ destination );
Break;
Case 3:
$ Simage = imagecreatefrompng ($ destination );
Break;
Case 6:
$ Simage = imagecreatefromwbmp ($ destination );
Break;
Default:
Die ("unsupported file types ");
Exit;
        }
Imagecopy ($ nimage, $ simage, 0, 0, 0, $ image_size [0], $ image_size [1]);
Imagefilledrectangle ($ nimage, 1, $ image_size [1]-15, 80, $ image_size [1], $ white );
Switch ($ watertype)
        {
Case 1: // add a watermark string
Imagestring ($ nimage, 2, 3, $ image_size [1]-15, $ waterstring, $ black );
Break;
Case 2: // add a watermark image
$ Simage1 = imagecreatefromgif ("xplore.gif ");
Imagecopy ($ nimage, $ simage1 );
Imagedestroy ($ simage1 );
Break;
        }
Switch ($ iinfo [2])
        {
Case 1:
// Imagegif ($ nimage, $ destination );
Imagejpeg ($ nimage, $ destination );
Break;
Case 2:
Imagejpeg ($ nimage, $ destination );
Break;
Case 3:
Imagepng ($ nimage, $ destination );
Break;
Case 6:
Imagewbmp ($ nimage, $ destination );
// Imagejpeg ($ nimage, $ destination );
Break;
        }
// Overwrite the original uploaded File
Imagedestroy ($ nimage );
Imagedestroy ($ simage );
    }
If ($ imgpreview = 1)
    {
Echo "<br> image preview: <br> ";
Echo "Echo "alt =" image preview: File name: ". $ destination." www.111cn.net Upload Time: "> ";
    }
}
?>

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.