Add watermark code modification for fck image uploading

Source: Internet
Author: User
Tags imagecopy imagejpeg

Add watermark code modification to the fck upload image. You only need to find the editor/filemanger/conectors/php/commands. php file and replace the following php code with your commands. php file.

Add watermark code modification for fck image uploading
// You only need to find the editor/filemanger/conectors/php tutorial/commands. php tutorial file and replace the following php code with your commands. php file.
Function getfolders ($ resourcetype, $ currentfolder)
{
// Map the virtual path to the local server path.
$ Sserverdir = servermapfolder ($ resourcetype, $ currentfolder, 'getfolders ');

// Array that will hold the folders names.
$ Afolders = array ();

$ Ocurrentfolder = opendir ($ sserverdir );

While ($ sfile = readdir ($ ocurrentfolder ))
{
If ($ sfile! = '.' & $ Sfile! = '..' & Is_dir ($ sserverdir. $ sfile ))
$ Afolders [] = '<folder name = "'. converttoxmlattribute ($ sfile). '"/> ';
}

Closedir ($ ocurrentfolder );

// Open the "folders" node.
Echo "<folders> ";

Natcasesort ($ afolders );
Foreach ($ afolders as $ sfolder)
Echo $ sfolder;

// Close the "folders" node.
Echo "</folders> ";
}

Function getfoldersandfiles ($ resourcetype, $ currentfolder)
{
// Map the virtual path to the local server path.
$ Sserverdir = servermapfolder ($ resourcetype, $ currentfolder, 'getfoldersandfiles ');

// Arrays that will hold the folders and files names.
$ Afolders = array ();
$ Afiles = array ();

$ Ocurrentfolder = opendir ($ sserverdir );

While ($ sfile = readdir ($ ocurrentfolder ))
{
If ($ sfile! = '.' & $ Sfile! = '..')
{
If (is_dir ($ sserverdir. $ sfile ))
$ Afolders [] = '<folder name = "'. converttoxmlattribute ($ sfile). '"/> ';
Else
{
$ Ifilesize = @ filesize ($ sserverdir. $ sfile );
If (! $ Ifilesize ){
$ Ifilesize = 0;
}
If ($ ifilesize> 0)
{
$ Ifilesize = round ($ ifilesize/1024 );
If ($ ifilesize <1) $ ifilesize = 1;
}

$ Afiles [] = '<file name = "'. converttoxmlattribute ($ sfile). '" size = "'. $ ifilesize. '"/> ';
}
}
}

// Send the folders
Natcasesort ($ afolders );
Echo '<folders> ';

Foreach ($ afolders as $ sfolder)
Echo $ sfolder;

Echo '</folders> ';

// Send the files
Natcasesort ($ afiles );
Echo '<files> ';

Foreach ($ afiles as $ sfiles)
Echo $ sfiles;

Echo '</files> ';
}

Function createfolder ($ resourcetype, $ currentfolder)
{
If (! Isset ($ _ get )){
Global $ _ get;
}
$ Serrornumber = '0 ';
$ Serrormsg = '';

If (isset ($ _ get ['newfoldername'])
{
$ Snewfoldername = $ _ get ['newfoldername'];
$ Snewfoldername = sanitizefoldername ($ snewfoldername );

If (strpos ($ snewfoldername ,'..')! = False)
$ Serrornumber = '000000'; // invalid folder name.
Else
{
// Map the virtual path to the local server path of the current folder.
$ Sserverdir = servermapfolder ($ resourcetype, $ currentfolder, 'createfolder ');

If (is_writable ($ sserverdir ))
{
$ Sserverdir. = $ snewfoldername;

$ Serrormsg = createserverfolder ($ sserverdir );

Switch ($ serrormsg)
{
Case '':
$ Serrornumber = '0 ';
Break;
Case 'invalidargument ':
Case 'no such file or directory ':
$ Serrornumber = '000000'; // path too long.
Break;
Default:
$ Serrornumber = '123 ';
Break;
}
}
Else
$ Serrornumber = '123 ';
}
}
Else
$ Serrornumber = '123 ';

// Create the "error" node.
Echo '<error number = "'. $ serrornumber. '" originaldescription = "'. converttoxmlattribute ($ serrormsg). '"/> ';
}

Function fileupload ($ resourcetype, $ currentfolder, $ scommand)
{
If (! Isset ($ _ files )){
Global $ _ files;
}
$ Serrornumber = '0 ';
$ Sfilename = '';

If (isset ($ _ files ['newfile']) &! Is_null ($ _ files ['newfile'] ['tmp _ name'])
{
Global $ config;

$ Ofile = $ _ files ['newfile'];

// Map the virtual path to the local server path.
$ Sserverdir = servermapfolder ($ resourcetype, $ currentfolder, $ scommand );

// Get the uploaded file name.
$ Sfilename = $ ofile ['name'];
$ Sfilename = sanitizefilename ($ sfilename );

$ Soriginalfilename = $ sfilename;

// Get the extension.
$ Sextension = substr ($ sfilename, (strrpos ($ sfilename, '.') + 1 ));
$ Sextension = strtolower ($ sextension );

If (isset ($ config ['secureimageupload'])
{
If ($ isimagevalid = isimagevalid ($ ofile ['tmp _ name'], $ sextension) === false)
{
$ Serrornumber = '123 ';
}
}

If (isset ($ config ['htmlextension'])
{
If (! Ishtmlextension ($ sextension, $ config ['htmlextension']) &
($ Detecthtml = detecthtml ($ ofile ['tmp _ name']) = true)
{
$ Serrornumber = '123 ';
}
}

// Check if it is an allowed extension.
If (! $ Serrornumber & isallowedext ($ sextension, $ resourcetype ))
{
$ Icounter = 0;

While (true)
{
$ Sfilepath = $ sserverdir. $ sfilename;
// $ Sfilepath = $ sserverdir. time (). $ sextension;

If (is_file ($ sfilepath ))
{
$ Icounter ++;
$ Sfilename = removeextension ($ soriginalfilename). '('. $ icounter. ').'. $ sextension;
$ Serrornumber = '123 ';
}
Else
{
Move_uploaded_file ($ ofile ['tmp _ name'], $ sfilepath );

If (is_file ($ sfilepath ))
{
If (isset ($ config ['chmodonupload']) &! $ Config ['chdonupload'])
{
Break;
}

$ Permissions = 0777;

If (isset ($ config ['chmodonupload']) & $ config ['chmodonupload'])
{
$ Permissions = $ config ['chmodonupload'];
}

$ Oldumask = umask (0 );
Chmod ($ sfilepath, $ permissions );
Umask ($ oldumask );
}

Break;
}
}

// Add a watermark
// Add start


$ Waterimg = "php100.com.png"; // modify the address of your watermark image.

If ($ watermark = 1)
{
$ Image_size = getimagesize ($ sfilepath );
$ Iinfo = getimagesize ($ sfilepath, $ 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 ($ sfilepath );
Break;
Case 2:
$ Simage = imagecreatefromjpeg ($ sfilepath );
Break;
Case 3:
$ Simage = imagecreatefrompng ($ sfilepath );
Break;
// Case 6:
// $ Simage = imagecreatefromwbmp ($ sfilepath );
// Break;
Default:
Die ("unsupported file types ");
Exit;
}

Imagecopy ($ nimage, $ simage, 0, 0, 0, $ image_size [0], $ image_size [1]);

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 = imagecreatefrompng ($ waterimg );
Imagecopy ($ nimage, $ simage1, $ image_size [0]/2, $ image_size [0]/0,240, 65 );
Imagedestroy ($ simage1 );
Break;
}

Switch ($ iinfo [2])
{
Case 1:
Imagegif ($ nimage, $ sfilepath );
// Imagejpeg ($ nimage, $ sfilepath );
Break;
Case 2:
Imagejpeg ($ nimage, $ sfilepath );
Break;
Case 3:
Imagepng ($ nimage, $ sfilepath );
Break;
// Case 6:
// Imagewbmp ($ nimage, $ sfilepath );
/// Imagejpeg ($ nimage, $ sfilepath );
// Break;
}

// Overwrite the original uploaded file
Imagedestroy ($ nimage );
Imagedestroy ($ simage );
}


// Add watermark results

If (file_exists ($ sfilepath ))
{
// Previous checks failed, try once again
If (isset ($ isimagevalid) & $ isimagevalid ===- 1 & isimagevalid ($ sfilepath, $ sextension) === false)
{
@ Unlink ($ sfilepath );
$ Serrornumber = '123 ';
}
Else if (isset ($ detecthtml) & $ detecthtml ===- 1 & detecthtml ($ sfilepath) === true)
{
@ Unlink ($ sfilepath );
$ Serrornumber = '123 ';
}
}
}
Else
$ Serrornumber = '123 ';
}
Else
$ Serrornumber = '123 ';


$ Sfileurl = combinepaths (getresourcetypepath ($ resourcetype, $ scommand), $ currentfolder );
$ Sfileurl = combinepaths ($ sfileurl, $ sfilename );

Senduploadresults ($ serrornumber, $ sfileurl, $ sfilename );

Exit;
}?>


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.