Upload an image and add a watermark? How can this problem be solved?

Source: Internet
Author: User
Add a watermark to the uploaded image ??? Add a watermark to the uploaded image ??? I implemented a function for uploading images (images can be uploaded after testing, which is no problem). now, when I want to upload an image by adding a watermark image (a small gif, the webpage prompts the function imagecopymerge () to report an error. I don't know what went wrong. can you help me see how to add a watermark to the uploaded image ???
Add a watermark to the uploaded image ???


I implemented a function for uploading images (images can be uploaded after testing. this is no problem)

Add a watermark image (a small gif) when uploading an image)

When uploading an image, the webpage prompts the function imagecopymerge () to report an error. I don't know what went wrong. please help me see it.


Function code for uploading images:

PHP code
  /* $ Files is the uploaded image file $ folder is the folder for uploading images */function uploads ($ files, $ folder) {// get the image suffix $ knamearray = explode (". ", $ _ FILES [" upfile "] [" name "]); $ kname = $ knamearray [count ($ knamearray)-1]; // set the image file name to system Time $ filename = date ('ymdhis '); $ filenames = $ filename. $ code. ". ". $ kname; $ upfile = $ folder. "/". $ filenames; // upload the image move_uploaded_file ($ files, $ upfile); // add the watermark image ...... $ Im = imagecreatefromgif ("wimages/logo.gif"); // The size of a logo.gif image is 120*45 imagecopymerge ($ upfile, $ im, 80 ); // the following error message is displayed... Return $ filenames; // return the image file name}


------ Solution --------------------
$ Im = imagecreatefromgif ("wimages/logo.gif"); // The size of the logo.gif image is 120*45
It is best to change the path to a relative path;
Imagecopymerge ($ upfile, $ im, 0, 0, 0, 45, 80); // you are prompted for a problem...
Could you help me explain the meaning of the parameters of imagecopymerge?
------ Solution --------------------
Discussion
$ Im = imagecreatefromgif ("wimages/logo.gif"); // The size of the logo.gif image is 120*45
It is best to change the path to a relative path;
Imagecopymerge ($ upfile, $ im, 0, 0, 0, 45, 80); // you are prompted for a problem...
Could you help me explain the meaning of the parameters of imagecopymerge?

------ Solution --------------------
You can write as follows:

$ Descrition = "/upload/imag_1123323.jpg"; // This is the path and file name of the watermark image;

$ Nimage = imagecreatetruecolor ($ image_size [0], $ image_size [1]); // create a true color image
// $ Image_size is the array of image size information to be watermark;

$ Simage = imagecreatefromgif ($ destination); // read the image to be watermark in gif format;

Imagecopy ($ nimage, $ simage, 0, 0, 0, $ image_size [0], $ image_size [1]);
// Copy the image to be watermark to the created real-color image;
Imagedestroy ($ simage); // destroy
$ Simage1 = imagecreatefromgif ("wimages/logo.gif") // This path is estimated to be correct.
// Read the watermark image
Imagecopy ($ nimage, $ simage1,); // Copy the watermark to the image;


Imagejpeg ($ nimage, $ descrtion); // Save the new image after adding the three images as an image without watermarks.

Imagedestroy ($ nimage); // destroy
Imagedestroy ($ simage );
------ Solution --------------------
Recently, I used an online tool to modify it and made it into a class ,,
With this class, you can upload multiple images ....
PHP code
Class upimg {// This class is used to upload images of the $ uptypes array type. you can enable the set watermark through setWaterMark ().

/**
*
* Add a watermark to the image;
* External access to the $ msg, $ img_width, $ img_height, $ save_dir, $ img_size variable
* Get the image upload errors, path, size, and other information;
*/
Private $ uptypes = array ('image/GIF', 'image/jpg ', 'image/jpeg', 'image/png ', 'image/pjpeg '); // 'image/bmp ', 'image/x-png ');
Private $ max_file_size = 200000;
Private $ dir_folder = "../imageupload /";
Private $ water_mark = 1;
Private $ water_type = 1;
Private $ water_str = "http://www.516ya.cn ";
Private $ water_img = "xplore.gif ";
Private $ create_water = false;
Private $ overwrite = false;

Public $ msg = "";
Public $ img_width = 0;
Public $ img_height = 0;
Public $ save_dir = "";
Public $ img_size = 0;
Public $ sucess = false;

Function setWaterMark (){
$ This-> create_water = true;
}

Function startUpload ($ files ){


If (! Is_uploaded_file ($ files ['tmp _ name'])
{
$ This-> msg. = "The image does not exist! --";

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.