<?php
function _uploadpic ($upfile, $maxsize, $updir, $newname = ' Date ') {
if ($newname = = ' Date ')
$newname = Date ("Ymdhis"); Use date to make file name
$name = $upfile ["name"];
$type = $upfile ["type"];
$size = $upfile ["Size"];
$tmp _name = $upfile ["Tmp_name"];
Switch ($type) {
Case ' Image/pjpeg ':
Case ' Image/jpeg ':
$extend = ". jpg";
Break
Case ' image/gif ':
$extend = ". gif";
Break
Case ' image/png ':
$extend = ". png";
Break
}
if (Emptyempty ($extend)) {
Echo ("Warning.") Only upload picture type: GIF JPG PNG ");
Exit ();
}
if ($size > $maxsize) {
$maxpr = $maxsize/1000;
Echo ("Warning.") Upload picture size cannot be more than ". $maxpr. "k!");
Exit ();
}
if (Move_uploaded_file ($tmp _name, $updir. $newname. $extend)) {
Return $updir. $newname. $extend;
}
}
function Getimageinfo ($SRC)
{
Return getimagesize ($SRC);
}
/**
* Create picture, return resource type
* @param string $src picture path
* @return Resource $im return resource type
* **/
function Create ($SRC)
{
$info =getimageinfo ($SRC);
Switch ($info [2])
{
Case 1:
$im =imagecreatefromgif ($SRC);
Break
Case 2:
$im =imagecreatefromjpeg ($SRC);
Break
Case 3:
$im =imagecreatefrompng ($SRC);
Break
}
return $im;
}
/**
* Thumbnail main function
* @param string $src picture path
* @param int $w thumbnail width
* @param int $h thumbnail height
* @return Mixed returns the thumbnail path
* **/
function Resize ($src, $w, $h)
{
$temp =pathinfo ($SRC);
$name = $temp ["basename"];//filename
$dir = $temp ["dirname"];//file Folder
$extension = $temp ["extension"];//file name extension
$savepath = "{$dir}/{$name}"//thumbnail save path, new file name *.thumb.jpg
Get basic information about a picture
$info =getimageinfo ($SRC);
$width = $info [0];//get picture width
$height = $info [1];//get picture height
$per 1=round ($width/$height, 2);//calculation of the original aspect ratio
$per 2=round ($w/$h, 2);//calculate the thumbnail aspect ratio
Calculate zoom ratio
if ($per 1> $per 2| | $per 1== $per 2)
{
The image aspect ratio is greater than or equal to the thumbnail aspect ratio, then the width first
$per = $w/$width;
}
if ($per 1< $per 2)
{
The original image aspect ratio is less than the thumbnail aspect ratio, it is highly preferred
$per = $h/$height;
}
$temp _w=intval ($width * $per);//Calculate the width of the original image scaling
$temp _h=intval ($height * $per);//Calculate the height after the original image is scaled
$temp _img=imagecreatetruecolor ($temp _w, $temp _h);//Create Canvas
$im =create ($SRC);
Imagecopyresampled ($temp _img, $im, 0,0,0,0, $temp _w, $temp _h, $width, $height);
if ($per 1> $per 2)
{
Imagejpeg ($temp _img, $savepath, 100);
Imagedestroy ($im);
Return addbg ($savepath, $w, $h, "w");
Width first, make up the background when the height is low after zooming
}
if ($per 1== $per 2)
{
Imagejpeg ($temp _img, $savepath, 100);
Imagedestroy ($im);
return $savepath;
Scaling by equal ratio
}
if ($per 1< $per 2)
{
Imagejpeg ($temp _img, $savepath, 100);
Imagedestroy ($im);
Return addbg ($savepath, $w, $h, "H");
High priority, fill background with insufficient width after scaling
}
}
/**
* Add background
* @param string $src picture path
* @param int $w background image width
* @param int $h background Image height
* @param String $first determines the final position of the image, W-width precedence H-high priority wh: equal ratio
* @return return picture with background
* **/
function addbg ($SRC, $w, $h, $fisrt = "W")
{
$BG =imagecreatetruecolor ($w, $h);
$white = Imagecolorallocate ($BG, 255,255,255);
Imagefill ($BG, 0,0, $white);//Fill background
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.