How to rename uploaded files and add watermarks in ckeditor

Source: Internet
Author: User
Tags image identifier imagecopy imagejpeg

This article describes how to use ckeditor to upload a file, rename the file, and add the watermark configuration.


First, the files I want to upload are organized by date.

Modify the config. php file in the editoreditorfilemanagerconnectorsphp folder.

Find the following content:

The Code is as follows: Copy code

// Path to user files relative to the document root.

$ Config ['userfilespath'] =

To:

The Code is as follows: Copy code

// Path to user files relative to the document root.

$ Config ['userfilespath'] = '/uploadfiles/'. date ("Ym ")."/";

In this way, the uploaded files are stored by date.

Second, rename

Modify the io. php file in this folder.

Find:

The Code is as follows: Copy code

// Do a cleanup of the file name to avoid possible problems

Function SanitizeFileName ($ sNewFileName)

{

Global $ Config;

$ SNewFileName = stripslashes ($ sNewFileName );

// Replace dots in the name with underscores (only one dot can be there... security issue ).

If ($ Config ['forcesingleextension'])

$ SNewFileName = preg_replace ('/\.(?! [^.] * $)/',' _ ', $ SNewFileName );

// Remove/| :? * "<>

$ SNewFileName = preg_replace ('/\\|\/ |\| |\:| \? | \ * | "| <|>/',' _ ', $ SNewFileName );

Return $ sNewFileName;

}

To:

The Code is as follows: Copy code

// Do a cleanup of the file name to avoid possible problems

Function SanitizeFileName ($ sNewFileName)

{

Global $ Config;

$ SNewFileName = stripslashes ($ sNewFileName );

// Replace dots in the name with underscores (only one dot can be there... security issue ).

If ($ Config ['forcesingleextension'])

$ SNewFileName = preg_replace ('/\.(?! [^.] * $)/',' _ ', $ SNewFileName );

$ SExtension = substr ($ sNewFileName, (strrpos ($ sNewFileName, '.') + 1 ));

$ SNewFileName = my_setfilename (). '.'. $ sExtension;

Return $ sNewFileName;

}

Function my_setfilename (){

$ Gettime = explode ('', microtime ());

$ String = 'abcdefghijklmnopgrstuvwxyz0123456789 ';

$ Rand = '';

For ($ x = 0; $ x <12; $ x ++)

$ Rand. = substr ($ string, mt_rand (0, strlen ($ string)-1), 1 );

Return date ("ymdHis"). substr ($ gettime [0], 2, 6). $ rand;

}


 

Solution for Fckeditor to upload duplicate image file names and Chinese garbled characters

The test Fckeditor2.6.6 does not solve the problem of changing the Chinese name of the uploaded file to garbled characters. This is because Fckeditor does not rename the file when implementing the upload function, which may easily lead to duplicate names and garbled characters of the uploaded image file.

The solution for uploading image files with duplicate names and garbled characters is as follows:

Open commands. php In the editor/filemanager/connectors/php Directory and find the FileUpload function.

The Code is as follows: Copy code

$ SExtension = substr ($ sFileName, (strrpos ($ sFileName, '.') + 1 ));
$ SExtension = strtolower ($ sExtension );

Add later
$ SFileName = rand (0,100). ".". $ sExtension;

Here, the rand function can change the rename rules as needed.
Another solution is to use the iconv function to encode and convert the file name. However, the problem still persists. Therefore, it is best to rename the image file name Uploaded By Fckeditor.

Fckeditor uploads images and adds watermarks

For website owners, adding watermarks to protect the copyright of images is essential. We can use the PHP watermarking function and the Fckeditor File Uploading function FileUpload to add watermarks to images, for watermark functions, refer to the PHP image watermark function: supports adding a watermark article in the form of images and text.

The Code is as follows: Copy code

Function setWater ($ imgSrc, $ markImg, $ markText, $ TextColor, $ markPos, $ fontType, $ markType)
{

$ SrcInfo = @ getimagesize ($ imgSrc );
$ SrcImg_w = $ srcInfo [0];
$ SrcImg_h = $ srcInfo [1];

Switch ($ srcInfo [2])
{
Case 1:
$ Srcim = imagecreatefromgif ($ imgSrc );
Break;
Case 2:
$ Srcim = imagecreatefromjpeg ($ imgSrc );
Break;
Case 3:
$ Srcim = imagecreatefrompng ($ imgSrc );
Break;
Default:
Die ("unsupported image file types ");
Exit;
}

If (! Strcmp ($ markType, "img "))
{
If (! File_exists ($ markImg) | empty ($ markImg ))
{
Return;
}

$ MarkImgInfo = @ getimagesize ($ markImg );
$ MarkImg_w = $ markImgInfo [0];
$ MarkImg_h = $ markImgInfo [1];

If ($ srcImg_w <$ markImg_w | $ srcImg_h <$ markImg_h)
{
Return;
}

Switch ($ markImgInfo [2])
{
Case 1:
$ Markim = imagecreatefromgif ($ markImg );
Break;
Case 2:
$ Markim = imagecreatefromjpeg ($ markImg );
Break;
Case 3:
$ Markim = imagecreatefrompng ($ markImg );
Break;
Default:
Die ("unsupported watermark image file types ");
Exit;
}

$ Logow = $ markImg_w;
$ Logoh = $ markImg_h;
}

If (! Strcmp ($ markType, "text "))
{
$ FontSize = 16;
If (! Empty ($ markText ))
{
If (! File_exists ($ fontType ))
{
Return;
}
}
Else {
Return;
}

$ Box = @ imagettfbbox ($ fontSize, 0, $ fontType, $ markText );
$ Logow = max ($ box [2], $ box [4])-min ($ box [0], $ box [6]);
$ Logoh = max ($ box [1], $ box [3])-min ($ box [5], $ box [7]);
}

If ($ markPos = 0)
{
$ MarkPos = rand (1, 9 );
}

Switch ($ markPos)
{
Case 1:
$ X = + 5;
$ Y = + 5;
Break;
Case 2:
$ X = ($ srcImg_w-$ logow)/2;
$ Y = + 5;
Break;
Case 3:
$ X = $ srcImg_w-$ logow-5;
$ Y = + 15;
Break;
Case 4:
$ X = + 5;
$ Y = ($ srcImg_h-$ logoh)/2;
Break;
Case 5:
$ X = ($ srcImg_w-$ logow)/2;
$ Y = ($ srcImg_h-$ logoh)/2;
Break;
Case 6:
$ X = $ srcImg_w-$ logow-5;
$ Y = ($ srcImg_h-$ logoh)/2;
Break;
Case 7:
$ X = + 5;
$ Y = $ srcImg_h-$ logoh-5;
Break;
Case 8:
$ X = ($ srcImg_w-$ logow)/2;
$ Y = $ srcImg_h-$ logoh-5;
Break;
Case 9:
$ X = $ srcImg_w-$ logow-5;
$ Y = $ srcImg_h-$ logoh-5;
Break;
Default:
Die ("this location is not supported ");
Exit;
}

$ Dst_img = @ imagecreatetruecolor ($ srcImg_w, $ srcImg_h );

Imagecopy ($ dst_img, $ srcim, 0, 0, 0, 0, $ srcImg_w, $ srcImg_h );

If (! Strcmp ($ markType, "img "))
{
Imagecopy ($ dst_img, $ markim, $ x, $ y, 0, 0, $ logow, $ logoh );
Imagedestroy ($ markim );
}

If (! Strcmp ($ markType, "text "))
{
$ Rgb = explode (',', $ TextColor );

$ Color = imagecolorallocate ($ dst_img, $ rgb [0], $ rgb [1], $ rgb [2]);
Imagettftext ($ dst_img, $ fontSize, 0, $ x, $ y, $ color, $ fontType, $ markText );
}

Switch ($ srcInfo [2])
{
Case 1:
Imagegif ($ dst_img, $ imgSrc );
Break;
Case 2:
Imagejpeg ($ dst_img, $ imgSrc );
Break;
Case 3:
Imagepng ($ dst_img, $ imgSrc );
Break;
Default:
Die ("unsupported watermark image file types ");
Exit;
}

Imagedestroy ($ dst_img );
Imagedestroy ($ srcim );
}

$ ImgSrc: target image with a relative directory address,
$ MarkImg: a watermark image with a relative directory address. It supports PNG and GIF formats. For example, if a watermark image is under the mark directory of an execution file, it can be written as: mark/mark.gif.
$ MarkText: watermark text added to an image
$ TextColor: The font color of the watermark text
$ MarkPos: Position of the image watermark. value range: 0 ~ 9
0: random position, ranging from 1 ~ Select a random location between 8
1: Top center left 2: Top center 3: Top center right 4: left center
5: Image Center 6: center on the right 7: center on the bottom 8: center on the bottom 9: center on the right
$ FontType: The specific library with the relative directory address
$ MarkType: the watermark method for an image. img indicates adding a watermark as an image, and text indicates adding a watermark as a text.

Code comment:

4th ~ Row 6: Obtain the width and height of the target image.
8th ~ Row 22: Call different functions based on the image type to obtain the operation image identifier.

Knowledge Point of the GetImageSize function: GetImageSize can be used without the GD degree installed. The returned value array has four elements. The index value 0 indicates the Image Height. Index value 1 indicates the image width. The index value 2 is the image file format. The value 1 is GIF, 2 is JPEG/JPG, and 3 is PNG. The index value 3 is the string of the image height and width, and the height is xxx width = yyy. The returned image width and height are measured in pixels)

24th ~ Row 58: When you select the image method to watermark the target image, you can obtain the width and height of the watermark image. Generally, it is the website logo. If the target image is smaller than the width or height of the watermark image or the watermark image does not exist, this function is displayed.

Return Statement knowledge point: direct return indicates that nothing is returned and this function is directly ended. It can also be interpreted as returning NULL.

60th ~ Row 77: when selecting the text method to flat the target image with watermarks, first set the watermark text size. The default value is 16px. You can adjust the font size as needed. If the font file does not exist, jump out of the function and use the imagettfbbox function to obtain the virtual length and width of the text in the specified format.

Imagettfbbox function knowledge point: this function returns an array containing eight units to represent the four corners of the text box. The index value meaning: 0 represents the position X in the lower left corner, and 1 represents the position Y in the lower right corner, 2 represents the position X in the lower right corner, 3 represents the position Y in the lower right corner, 4 represents the position X in the upper right corner, 5 represents the position Y in the upper right corner, 6 represents the position X in the upper left corner, and 7 represents the position Y in the upper left corner. This function must be supported by both the GD library and the FreeType library.
The max function returns the maximum value of a parameter.

79th ~ Row 125: calculates the coordinate value based on the watermark position. You can refine the watermark position based on the effect.

127th ~ Row 3: create an image with the same size as the target image.

Note: Because the range of the imagecreatetruecolor function is a black image, if your target image is transparent, the new image will not be transparent.

131st ~ Row 3: the watermark is generated based on the image or text format.

Call description:

You can call the function as a function call. Of course, you can encapsulate the function as a class, or you can further segment the module as needed. Of course, you can use it now without any problems. I have tested it. Please feel free to use it.

Other Instructions:

The imagettftext and imagettfbbox functions require the support of the GD library and FreeType library. If your runtime environment does not support the GD library and FreeType library, the text mode cannot be implemented, you can use the imagestring function to add a text watermark to the image and set the $ logow and $ logoh values in the text mode.

The imagejpeg function can also set the quality of the merged image.

PHP Image Watermarking function ideas summary:
First, calculate the width and height of the target image, watermark image, and text, and calculate the position information of the final Watermark based on the specific position, that is, the X and Y values. After merging the image, the watermark is added to the new image.

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.