php upload image and generate the specified size map

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags address data directory file function function parameters function_exists image

-
php tutorials to upload pictures and generate the specified size chart in proportion This is an image thumbnail function Oh, upload a new picture to $ srcfile Then file $ thumbwidth thumbnail width maximum size and thumb thumb size Small picture.
Image thumbnail function parameter description:

$ srcfile original address
$ dir new map directory
$ thumbwidth Shrinks the maximum width of the image
$ thumbheitht thumbnail image max size
$ ratio default scaling to 1 is reduced to a fixed size.
* /
function makethumb ($ srcfile, $ dir, $ thumbwidth, $ thumbheight, $ ratio = 0)
{
/ / Determine whether the file exists
if (! file_exists ($ srcfile)) return false;
// Create a new file of the same name, but the directory is different
$ imgname = explode ('/', $ srcfile);
$ arrcount = count ($ imgname);
$ dstfile = $ dir. $ imgname [$ arrcount-1];
// Thumbnail size
$ tow = $ thumbwidth;
$ toh = $ thumbheight;
if ($ tow <40) $ tow = 40;
if ($ toh <45) $ toh = 45;
// Get image information
$ im = '';
if ($ data = getimagesize ($ srcfile)) {
if ($ data [2] == 1) {
$ make_max = 0; / / gif not dealt with
if (function_exists ("imagecreatefromgif")) {
$ im = imagecreatefromgif ($ srcfile);
}
} elseif ($ data [2] == 2) {
if (function_exists ("imagecreatefromjpeg")) {
$ im = imagecreatefromjpeg ($ srcfile);
}
} elseif ($ data [2] == 3) {
if (function_exists ("imagecreatefrompng")) {
$ im = imagecreatefrompng ($ srcfile);
}
}
}
if (! $ im) return ';
$ srcw = imagesx ($ im);
$ srch = imagesy ($ im);
$ towh = $ tow / $ toh;
$ srcwh = $ srcw / $ srch;
if ($ towh <= $ srcwh) {
$ ftow = $ tow;
$ ftoh = $ ftow * ($ srch / $ srcw);
} else {
$ ftoh = $ toh;
$ ftow = $ ftoh * ($ srcw / $ srch);
}
if ($ ratio) {
$ ftow = $ tow;
$ ftoh = $ toh;
}
// Zoom out the picture
if ($ srcw> $ tow || $ srch> $ toh || $ ratio) {
if (function_exists ("imagecreatetruecolor") && function_exists ("imagecopyresampled") && @ $ ni = imagecreatetruecolor ($ ftow, $ ftoh)) {
imagecopyresampled ($ ni, $ im, 0, 0, 0, 0, $ ftow, $ ftoh, $ srcw, $ srch);
} elseif (function_exists ("imagecreate") && function_exists ("imagecopyresized") && @ $ ni = imagecreate ($ ftow, $ ftoh)) {
imagecopyresized ($ ni, $ im, 0, 0, 0, 0, $ ftow, $ ftoh, $ srcw, $ srch);
} else {
return
}
if (function_exists ('imagejpeg')) {
imagejpeg ($ ni, $ dstfile);
} elseif (function_exists ('imagepng')) {
imagepng ($ ni, $ dstfile);
}
} else {
// smaller than the size of the direct copy
copy ($ srcfile, $ dstfile);
}
imagedestroy ($ im);
if (! file_exists ($ dstfile)) {
return
} else {
return $ dstfile;
}
}
?>

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.