You can add the watermark function by yourself:
Copy codeThe Code is as follows:
<? Php
/**
*
* @ Author zhao jinhan
* @ Date January 13, 2014 11:54:30
* @ Email xb_zjh@126.com
*
*/
Header ('content-type: text/html; charset = UTF-8 ');
// Define the width and height of the thumbnail
Define ('thumb _ width', 300 );
Define ('thumb _ height', 300 );
/**
* Regenerate the uploaded file name
* @ Return string
* @ Author zhao jinhan
*
*/
Function _ file_type ($ filetype = null ){
Switch ($ filetype)
{
Case "image/jpeg ":
$ Fileextname = "jpg ";
Break;
Case "image/gif ":
$ Fileextname = "gif ";
Break;
Case "image/png ":
$ Fileextname = "png ";
Break;
Default:
$ Fileextname = false;
Break;
}
Return $ fileextname? Date ('ymdhis ', time ().'. '. $ fileextname: false;
}
/**
*
* @ Param string $ filename
* @ Param string $ width
* @ Param string $ height
* @ Param string $ quality
* @ Param string $ savepath
* @ Return boolean
*/
Function _ make_thumb ($ filename = '', $ width = THUMB_WIDTH, $ height = THUMB_HEIGHT, $ savepath = './upload '){
If (file_exists ($ filename )){
// Size of the uploaded Image
$ Imagesize = getimagesize ($ filename );
$ Imagewidth = $ imagesize [0];
$ Imageheight = $ imagesize [1];
$ Mime = $ imagesize ['mime '];
// Width/height ratio
$ Ratio = $ imagewidth/$ imageheight;
// Create a background image
$ Bgimg = imagecreatetruecolor ($ width, $ height );
$ White = imagecolorallocate ($ BGB, 255,255,255 );
// The background color is white.
Imagefill ($ bgimg, 0, 0, $ white );
If ($ mime = 'image/gif '){
$ Im = @ imagecreatefromgif ($ filename);/* Attempt to open */
$ Outfun = 'imagegif ';
} Elseif ($ mime = 'image/png '){
$ Im = @ imagecreatefrompng ($ filename);/* Attempt to open */
$ Outfun = 'imagepng ';
} Else {
$ Im = @ imagecreatefromjpeg ($ filename);/* Attempt to open */
$ Outfun = 'imagejpeg ';
}
If ($ ratio> 1 ){
// The width is large.
If ($ imagewidth> $ width ){
// Scale the image to the background image
$ New_width = $ width;
$ New_height = ($ width * $ imageheight)/$ imagewidth;
$ Bg_y = ceil (abs ($ height-$ new_height)/2 ));
Imagecopyresampled ($ bgimg, $ im, 0, $ bg_y, 0, 0, $ new_width, $ new_height, $ imagewidth, $ imageheight );
} Else {
// Copy the image to the background image
$ Copy = true;
}
} Else {
// High
If ($ imageheight> $ height ){
// Scale the image
$ New_height = $ height;
$ New_width = ($ height * $ imagewidth)/$ imageheight;
$ Bg_x = ceil ($ width-$ new_width)/2 );
Imagecopyresampled ($ bgimg, $ im, $ bg_x, 0, 0, 0, $ new_width, $ new_height, $ imagewidth, $ imageheight );
} Else {
// Copy the image to the background image
$ Copy = true;
}
}
If ($ copy ){
// Copy the image to the background image
$ Bg_x = ceil ($ width-$ imagewidth)/2 );
$ Bg_y = ceil ($ height-$ imageheight)/2 );
Imagecopy ($ bgimg, $ im, $ bg_x, $ bg_y, 0, 0, $ imagewidth, $ imageheight );
}
$ Ext = _ file_type ($ mime );
$ Outfun ($ bgimg, $ savepath. '/'. $ ext );
Imagedestroy ($ bgimg );
Return $ savepath. '/'. $ ext;
} Else {
Return false;
}
}
If ($ _ POST ){
$ Size = $ _ POST ['SIZE']? Strtoupper (trim ($ _ POST ['SIZE']): '2m ';
$ Imgsize = $ _ FILES ['img '] ['SIZE']? $ _ FILES ['img '] ['SIZE']/(1024*1024): 0;
$ Imgwidth = $ imgheight = $ _ POST ['width-height']? Intval ($ _ POST ['width-height']): 300;
// Customize the file upload size
Ini_set ('upload _ max_filesize ', $ size );
$ Mathsize = str_replace ('M', '', $ size );
If ($ imgsize> $ mathsize ){
Echo "the image size cannot exceed {$ size }! ";
Return;
}
If ($ file_name = _ file_type ($ _ FILES ['img '] ['type']) {
If ($ _ FILES ['img '] ['error'] = UPLOAD_ERR_ OK ){
$ Savepath = 'upload /';
If (! Is_dir ($ savepath )){
Mkdir ($ savepath, 0644 );
}
// Generate a thumbnail
$ Thumb_file = _ make_thumb ($ _ FILES ['img '] ['tmp _ name'], $ imgwidth, $ imgheight, $ savepath );
// Move_uploaded_file ($ _ FILES ['img '] ['tmp _ name'], $ savepath. $ file_name );
Echo "the generated image is: ";
} Else {
Echo $ _ FILES ['img '] ['error'];
Return;
}
} Else {
Echo "the image format is incorrect. Upload jpg, gif, and png formats! ";
Return;
}
} Else {
Echo <EOT
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> Save the scaled image as a square </title>
</Head>
<Body>
<Form action = "" method = "POST" enctype = "multipart/form-data">
<Div>
<Label> upload an image: </label>
<Input type = "file" name = "img"/>
</Div>
<Div>
<Label> width and height (unit: px) of the generated thumbnail: </label>
<Input type = "text" name = "width-height" value = "300"/>
</Div>
<Div>
<Label> maximum file size: </label>
<Input type = "text" name = "size" value = "2 M"/>
</Div>
<Div> <input type = "submit" name = "submit" value = "submit"/> </div>
</Form>
</Body>
</Html>
EOT;
}