Simple and practical php thumbnails Generation Code-PHP source code

Source: Internet
Author: User
Tags imagejpeg
This program is based on the user to upload an image and then upload the image to generate a thumbnail proportionally. This program is based on the user to upload the image and then upload the image to generate a thumbnail proportionally

Script ec (2); script

The Code is as follows:

$ W? $ Resizewidth = $ w: $ resizewidth = 400; // The width of the generated image.
$ H? $ Resizeheight = $ h: $ resizeheight = 400; // The height of the generated image.
Function resizeimage ($ im, $ maxwidth, $ maxheight, $ name ){
$ Width = imagesx ($ im );
$ Height = imagesy ($ im );
If ($ maxwidth & $ width> $ maxwidth) | ($ maxheight & $ height> $ maxheight )){
If ($ maxwidth & $ width> $ maxwidth ){
$ Widthratio = $ maxwidth/$ width;
$ Resizewidth = true;
}
If ($ maxheight & $ height> $ maxheight ){
$ Heightratio = $ maxheight/$ height;
$ Resizeheight = true;
}
If ($ resizewidth & $ resizeheight ){
If ($ widthratio <$ heightratio ){
$ Ratio = $ widthratio;
} Else {
$ Ratio = $ heightratio;
}
} Elseif ($ resizewidth ){
$ Ratio = $ widthratio;
} Elseif ($ resizeheight ){
$ Ratio = $ heightratio;
}
$ Newwidth = $ width * $ ratio;
$ Newheight = $ height * $ ratio;
If (function_exists ("imagecopyresampled ")){
$ Newim = imagecreatetruecolor ($ newwidth, $ newheight );
Imagecopyresampled ($ newim, $ im, 0, 0, 0, 0, $ newwidth, $ newheight, $ width, $ height );
} Else {
$ Newim = imagecreate ($ newwidth, $ newheight );
Imagecopyresized ($ newim, $ im, 0, 0, 0, 0, $ newwidth, $ newheight, $ width, $ height );
}
Imagejpeg ($ newim, $ name );
Imagedestroy ($ newim );
} Else {
Imagejpeg ($ im, $ name );
}
}

If ($ _ files ['uploadfile'] ['SIZE']) {
If ($ _ files ['uploadfile'] ['type'] = "image/pjpeg "){
$ Im = imagecreatefromjpeg ($ _ files ['uploadfile'] ['tmp _ name']);
} Elseif ($ _ files ['uploadfile'] ['type'] = "image/x-png "){
$ Im = imagecreatefrompng ($ _ files ['uploadfile'] ['tmp _ name']);
} Elseif ($ _ files ['uploadfile'] ['type'] = "image/gif "){
$ Im = imagecreatefromgif ($ _ files ['uploadfile'] ['tmp _ name']);
}
If ($ im ){
If(file_exists('bbs.jpg ')){
Unlink('bbs.jpg ');
}
Resizeimage(%im,%resizewidth,%resizeheight,'bbs.jpg ');
Imagedestroy ($ im );

}
}
// $ Uploadfile = "www.111cn.net.jpg ";
?>

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.