How php generates images and Verification Code Images

Source: Internet
Author: User
Tags imagejpeg

This php image and Verification Code Generation Principle code is supported by the php gd library. If your system cannot create an image, set gd. before dll; then, restart apache. If it is iis, restart iis.

ThisHow php creates images and Verification Code ImagesThe code is supported by the php gd library. If your system cannot create an image, you need to replace the gd. dll file. Then, restart apache. If it is iis, restart iis.

<?

$ 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; // www. bKjia. c0m
}
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('www.bKjia.c0m.jpg ');
}
Resizeimage(%im,%resizewidth,%resizeheight,'bbs.jpg ');
ImageDestroy ($ im );

}
}
// $ Uploadfile = "bbs.jpg ";

?>

Create ImageRemember to use ImageDestroy to clear the memory.

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.