PHP example: add a watermark to the uploaded image _ PHP Tutorial

Source: Internet
Author: User
Tags php example
PHP example: add a watermark to the uploaded image. The program that uses php (as the mainstream development language) to add a watermark to the uploaded image creates a graph by judging the file type and then copies it to the original image, filling and setting up re the program that uses php (as the current mainstream development language) to add a watermark to the uploaded image is to create a graph by judging the file type, and then copy it to the original graph, fill in and create a rectangle for writing the image string () or determine the watermark type in the original image program: one is a string, and the other is to add a graphic object on it. If you are familiar with the GD library of php (as the mainstream development language), it is not difficult to understand this article!

The following is a reference clip:

/*************************************** **************
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. use php (as the mainstream development language ). the "extension = php (as the mainstream development language) _ gd2.dll" line in the INI file is removed because the GD library is used;
2. change extension_dir = to the directory where your php (as the mainstream development language) _ gd2.dll is located;
3. http://www.knowsky.com/php (as the mainstream development language). 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
?>


ZwelL image upload program




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 "uploaded successfully
File name: ". $ destination_folder. $ fname ."
";
Echo "width:". $ image_size [0];
Echo "length:". $ image_size [1];
Echo"
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

The program that adds a watermark to an uploaded image (as the mainstream development language) is to create a graph by judging the file type, copy it to the original image, fill in and create a re...

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.