php生成圖片與驗證碼圖片生成原理

來源:互聯網
上載者:User
關鍵字 網路程式設計 PHP教程

這款php教程生成圖片與驗證碼圖片生成原理代碼,是由php gd庫來支援,如果你的系統不能創建圖片就把gd.dll前面的;去再,重起apache,如果是iis重起iis就OK了。

<?

$w?$RESIZEWIDTH=$w:$RESIZEWIDTH=400;// 生成圖片的寬度


$h?$RESIZEHEIGHT=$h:$RESIZEHEIGHT=400;// 生成圖片的高度


function ResizeImage($im,$maxwidth,$maxheight,$name){


    $width = imagesx($im);


    $height = imagesy($im);


    if(($maxwidth &amp;&amp; $width &gt; $maxwidth) || ($maxheight &amp;&amp; $height &gt; $maxheight)) {


        if($maxwidth &amp;&amp; $width &gt; $maxwidth){


            $widthratio = $maxwidth/$width;


            $RESIZEWIDTH=true;//www.111cn.net


        }


if($maxheight &amp;&amp; $height &gt; $maxheight){


            $heightratio = $maxheight/$height;


            $RESIZEHEIGHT=true;


        }


        if($RESIZEWIDTH &amp;&amp; $RESIZEHEIGHT){


            if($widthratio &lt; $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";


?>

創建圖片後記得用ImageDestroy 清空記憶體哦。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.