PHP adds the watermark function for image uploading and proportional scaling.

Source: Internet
Author: User
PHP uploads an image for proportional scaling. you can add the watermark function by yourself. the specific code is as follows. if you like it, you can refer to it and add the code directly, you can add the watermark function on your own:

The code is as follows:


/**
*
* @ 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 <



Save the scaled image as a square





EOT;
}

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.