PHP generated thumbnail image-large image generated small picture

Source: Internet
Author: User

/*
Functions: Generating thumbnails
Makebuild ("Images/a.jpg", "news/b.jpg", "100");
Parameters:
Echo $BuildFile; Original artwork with path
Echo $newFile; Generated thumbnail band path
echo $File _width; Thumbnail width value
echo $File _height; Thumbnail height value (the default is the proportional value of the width)
Echo $rate; thumbnail image quality;
*/
function Makebuild ($BuildFile, $newFile, $File _width, $File _height=0, $rate =1000) {
if (!is_file ($BuildFile)) {
$this->msg ("file". $BuildFile. "is not a valid graphics file!) The system cannot generate a thumbnail of the file! ");
return false;
}
$data = getimagesize ($BuildFile);
Switch ($data [2]) {
Case 1:
$im = @ImageCreateFromGIF ($BuildFile);
Break
Case 2:
$im = @ImageCreateFromJPEG ($BuildFile);
Break
Case 3:
$im = @ImageCreateFromPNG ($BuildFile);
Break
}
if (! $im) {
return false;
}
else{
$srcW = Imagesx ($im); # get the original width;
$srcH = Imagesy ($im); # get the original height;
$dstX = 0;
$dstY = 0;

if ($File _height==0) {
$File _height = $File _width/$srcW * $srcH;
}

if ($srcW * $File _height> $srcH * $File _width) {
$fFile _height = Round ($srcH * $File _width/$srcW);
$dstY = Floor (($File _height-$fFile _height)/2);
$fFile _width = $File _width;
}
else {
$fFile _width = Round ($srcW * $File _height/$srcH);
$dstX = Floor (($File _width-$fFile _width)/2);
$fFile _height = $File _height;
}
$ni = Imagecreatetruecolor ($File _width, $File _height);
$dstX = ($dstX <0) 0: $dstX;
$dstY = ($dstX <0) 0: $dstY;
$dstX = ($dstX > ($File _width/2)) floor ($File _WIDTH/2): $dstX;
$dstY = ($dstY > ($File _height/2)) floor ($File _height/s): $dstY;
Imagecopyresized ($ni, $im, $dstX, $dstY, 0,0, $fFile _width, $fFile _height, $srcW, $srcH);

Imagejpeg ($ni, $newFile, $rate); # Generate thumbnails;
Imagedestroy ($im); # Imagedestroy (Resource) frees the memory associated with the image
}
}

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.