PHP upload image for equal ratio scaling can add watermark function _php Tutorial

Source: Internet
Author: User
Do not say anything, directly on the code, you can add the watermark function on their own:
Copy CodeThe 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 height of the thumbnail image
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 new background image
$bgimg = Imagecreatetruecolor ($width, $height);
$white = Imagecolorallocate ($bgimg, 255, 255, 255);
Fill background color to 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 the picture onto 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 picture to background image
$copy = true;
}
}else{
Highly large
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 picture to background image
$copy = true;
}
}
if ($copy) {
Copy picture to 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;
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 thumbnail images
$thumb _file = _make_thumb ($_files[' img ' [' tmp_name '], $imgwidth, $imgheight, $savepath);
Move_uploaded_file ($_files[' img '] [' tmp_name '], $savepath. $file _name);
echo "The resulting image is:";
}else{
echo $_files[' img ' [' ERROR '];
Return
}
}else{
echo "image format is not correct, please upload jpg,gif,png format!";
Return
}



}else{
Echo <<



Zoom picture Save to Square





EOT;
}

http://www.bkjia.com/phpjc/695103.html Span id= "Indexurl" itemprop= "Indexurl" >www.bkjia.com true http://www.bkjia.com/phpjc/695103.html techarticle don't say anything, directly on the code, You can add the watermark function by yourself: Copy code code as follows: PHP/** * * @author Zhao Jinhan * @date January 13, 2014 11:54:30 * @email ...

  • Related Article

    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.