PHP upload image for equal scaling can increase the watermark function _php skills

Source: Internet
Author: User
Nothing to say, directly on the code, you can add your own watermark function:
Copy Code code 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 height of the thumbnail
Define (' Thumb_width ', 300);
Define (' Thumb_height ', 300);

/**
* Rebuild 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)) {
Upload the picture size
$imagesize =getimagesize ($filename);
$imagewidth = $imagesize [0];
$imageheight = $imagesize [1];
$mime = $imagesize [' MIME '];
Width and height ratio
$ratio = $imagewidth/$imageheight;

Create a new background picture
$bgimg = Imagecreatetruecolor ($width, $height);
$white = Imagecolorallocate ($bgimg, 255, 255, 255);
Fill 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) {
Large width
if ($imagewidth > $width) {
Zoom picture to Background picture
$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 picture to the background picture
$copy = true;
}
}else{
Large height
if ($imageheight > $height) {
Zoom picture
$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 picture to the background picture
$copy = true;
}
}
if ($copy) {
Copy the picture to the background picture
$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;
Custom definition File Upload size
Ini_set (' upload_max_filesize ', $size);
$mathsize = Str_replace (' M ', ', $size);
if ($imgsize > $mathsize) {
echo "Picture size must not 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 thumbnails
$thumb _file = _make_thumb ($_files[' img '] [' tmp_name '], $imgwidth, $imgheight, $savepath);
Move_uploaded_file ($_files[' img '] [' tmp_name '], $savepath. $file _name);
echo "After the generated picture is: ';
}else{
echo $_files[' img ' [' ERROR '];
Return
}
}else{
echo "Picture format is not correct, please upload jpg,gif,png format!"
Return
}



}else{
Echo <<<eot
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title> Zoom picture Save As Square </title>
<body>
<form action= "" method= "POST" enctype= "Multipart/form-data" >
<div>
<label> Upload a photo:</label>
<input type= "file" Name= "img"/>
</div>
<div>
<label> Generate thumbnail width (unit px):</label>
<input type= "text" name= "Width-height" value= "the/>"
</div>
<div>
<label> Maximum file size:</label>
<input type= "text" name= "size" value= "2M"/>
</div>
<div><input type= "Submit" name= "Submission" value= "submitted"/></div>
</form>
</body>
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.