PHP upload image generation proportional thumbnail code _php tutorial

Source: Internet
Author: User
Tags imagejpeg
A simple use of PHP upload image files and then generate a picture of the same scale thumbnail effect, there is a need to learn friends can refer to.
The code is as follows Copy Code

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! can only upload image type: GIF JPG PNG ");

Exit ();

}

if ($size > $maxsize) {

$maxpr = $maxsize/1000;

Echo ("Warning! Upload image size cannot exceed ". $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); Width-to-height ratio

$ra 2 = Number_format (($IMGH/$IMGW), 1); Aspect ratio

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 return thumbnail path

* **/

function Resize ($src, $w, $h)

{

$temp =pathinfo ($SRC);

$name = $temp ["basename"];//file name

$dir = $temp the folder where the ["DirName"];//file is located

$extension = $temp ["extension"];//file name extension

$savepath = "{$dir}/{$name}";//thumbnail save path, new file named *.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);//calculate original aspect ratio

$per 2=round ($w/$h, 2);//calculate thumbnail aspect ratio

Calculating the zoom ratio

if ($per 1> $per 2| | $per 1== $per 2)

{

The original 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 aspect ratio is less than the thumbnail aspect ratio, which is high priority

$per = $h/$height;

}

$temp _w=intval ($width * $per);//Calculate the width of the original image after zooming

$temp _h=intval ($height * $per);//Calculate the height of the original image after zooming

$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, fill the background with insufficient height after zooming

}

if ($per 1== $per 2)

{

Imagejpeg ($temp _img, $savepath, 100);

Imagedestroy ($im);

return $savepath;

Equal ratio Scaling

}

if ($per 1< $per 2)

{

Imagejpeg ($temp _img, $savepath, 100);

Imagedestroy ($im);

Return addbg ($savepath, $w, $h, "H");

High priority, fill the background with insufficient width after zooming

}

}

/**

* 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 first h high priority wh: equal ratio

* @return back 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 image width

$height = $info [1];//target Image Height

$img =create ($SRC);

if ($fisrt = = "WH")

{

Equal ratio Scaling

return $SRC;

}

Else

{

if ($fisrt = = "W")

{

$x = 0;

$y = ($h-$height)/2;//Vertical Center

}

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;

}
}
?>

How to use:

$filename = (_uploadpic ($_files["Upload"), $maxsize, $updir, $newname = ' date ');
$show _pic_scal=show_pic_scal ($filename);
Resize ($filename, $show _pic_scal[0], $show _pic_scal[1]);

http://www.bkjia.com/PHPjc/632958.html www.bkjia.com true http://www.bkjia.com/PHPjc/632958.html techarticle a simple use of PHP upload image files and then generate a picture of the same scale thumbnail effect, there is a need to learn friends can refer to. Code to copy code like this? PHP function_up ...

  • 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.