Very simple PHP thumbnail generation program source code

Source: Internet
Author: User
Tags imagejpeg
A very simple PHP code program that generates thumbnails. the parameters and code can be simplified. if you are interested, you can try its functions. please correct me if it is not perfect. Non-original, from network "> <LINKhref =" http://www.php100.com//statics/style/headfloor_950_081205.css "type = text/

$ FILENAME = "image_name ";

// Generate the image width
$ RESIZEWIDTH = 400;

// Generate the image height
$ RESIZEHEIGHT = 400;


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 ")){
Www.phperz.com


$ 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. ". jpg ");
ImageDestroy ($ newim );
} Else {
ImageJpeg ($ im, $ name. ". jpg ");
}
}

 

If ($ _ FILES ['image'] ['size']) {
If ($ _ FILES ['image'] ['type'] = "image/pjpeg "){
$ Im = imagecreatefromjpeg ($ _ FILES ['image'] ['tmp _ name']);
} Elseif ($ _ FILES ['image'] ['type'] = "image/x-png "){
$ Im = imagecreatefrompng ($ _ FILES ['image'] ['tmp _ name']);
} Elseif ($ _ FILES ['image'] ['type'] = "image/gif "){
$ Im = imagecreatefromgif ($ _ FILES ['image'] ['tmp _ name']);
}
If ($ im ){
If (file_exists ("your filename.jpg ")){
Unlink ("your filename.jpg ");
}
ResizeImage ($ im, $ RESIZEWIDTH, $ RESIZEHEIGHT, $ FILENAME); www ~ Phperz ~ Com
ImageDestroy ($ im );
}
}

?>

">


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.