Php Image Watermarking and uploading Image Watermarking php class

Source: Internet
Author: User
Tags imagecopy imagejpeg

When you need to upload images to a regular website, you often need to add the LOGO watermark of your website to the image. So how to implement this step? First, let's take a look at the principles of adding watermarks to PHP images.
Create an image by determining the file type, copy it to the original image, fill it with and create a rectangle for writing it into imagestring (), or determine the watermark type in the original image program: one is a string, and the other is to add a graphic object to it. Below is a PHP image watermark reprinted!

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 );

PHP image watermark instructions:
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. http://www.jb51.net/list/list_15_1.htm
PHP Image Watermarking code example:

Copy codeThe Code is as follows: // list of File Upload types
$ 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 and 2 indicates the lower right corner
, 3 is the upper left corner, 4 is the upper right corner, and 5 is the center );
$ Waterstring = "<A href =" http://www.xplore.cn/">
Http://www.xplore.cn/</A> "; // 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">
<! --
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! ";
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 "<ccid_file values =" \ "width = ".
($ Image_size [0] * $ imgpreviewsize )."
Height = ". ($ image_size [1] * $ imgpreviewsize );"
Echo "alt = \" image preview: \ r file name :".
$ Destination. "\ r upload time: \"/> ";
}
}
?>
</Body>
</Html>

You can also refer to the PHP class for adding watermarks to images.

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.