PHP generated pictures and verification code picture generation principle

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags code create gd library image jpg name php php tutorial

This PHP tutorial generates pictures and verification code picture generation principle code, is by the PHP GD library to support, if your system can not create a picture on the front of the Gd.dll;

?

$w? $RESIZEWIDTH = $w: $RESIZEWIDTH =400;//to generate the width of the picture


$h? $RESIZEHEIGHT = $h: $RESIZEHEIGHT =400;//to generate the height of the picture


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.111cn.net


        }


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.111cn.net.jpg ');


        }


Resizeimage ($im, $RESIZEWIDTH, $RESIZEHEIGHT, ' bbs.jpg ');


Imagedestroy ($im);


  


    }


}


//$uploadfile = "bbs.jpg";


?>

After creating the picture, remember to empty the memory with Imagedestroy.

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.