PHP implementation upload picture and equal scale thumbnail image compression picture

Source: Internet
Author: User
Tags extend imagejpeg
<?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 show_pic_scal ($width, $height, $picpath) {
$imginfo = getimagesize ($picpath);
$IMGW = $imginfo [0];
$IMGH = $imginfo [1];

$ra = Number_format (($IMGW/$IMGH), 1); Ratio of width to height
$ra 2 = Number_format (($IMGH/$IMGW), 1); Width


if ($imgw > $width or $IMGH > $height) {
if ($imgw > $imgh) {
$newWidth = $width;
$newHeight = Round ($newWidth/$ra);

} elseif ($IMGW < $IMGH) {
$newHeight = $height;
$newWidth = Round ($newHeight/$ra 2);
} else {
$newWidth = $width;
$newHeight = Round ($newWidth/$ra);
}
} else {
$newHeight = $IMGH;
$newWidth = $IMGW;
}
$newsize [0] = $newWidth;
$newsize [1] = $newHeight;

return $newsize;
}



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

Get Target picture information
$info =getimageinfo ($SRC);
$width = $info [0];//target picture width
$height = $info [1];//target picture height
$img =create ($SRC);
if ($fisrt = = "WH")
{
Scaling by equal ratio
return $SRC;
}
Else
{
if ($fisrt = = "W")
{
$x = 0;
$y = ($h-$height)/2;//vertically centered
}
if ($fisrt = = "H")
{
$x = ($w-$width)/2;//Horizontal Center
$y = 0;
}
Imagecopymerge ($BG, $img, $x, $y, 0,0, $width, $height, 100);
Imagejpeg ($BG, $SRC, 100);
Imagedestroy ($BG);
Imagedestroy ($IMG);
return $SRC;
}

}


?>

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.