柳暗花明笔记

    php把生成的条形码以水印形式贴到指定图片上程序

    所属分类:[ php&web ] 猫哲学家 发表于:2012-07-25 22:01 浏览:581

    class PhpUpImageWater{ private $groundImage; private $groundImageWidth; private $groundImageHeight; private $groundImageHandle; private $waterType; private $waterPosType; private $waterPosX; private $waterPosY; private $waterWidth; private $waterHeight; private $waterImage; private $waterImageType; private $waterImageHandle; private $waterImageAlpha; private $waterText; private $waterTextColor; private $waterTextSize; private $waterTextFont; private $errorMsg; public function PhpUpImageWater($groundImage = "demo.jpg", $waterPosType = 5){ if(false == file_exists($groundImage)){ $this->throwError("groundImage404"); } $this->checkGD(); $this->groundImage = &$groundImage; $this->waterType = &$waterType; $this->waterPosType = &$waterPosType; $this->setGroundImageInfo(); } private function checkGD(){ if(false == function_exists("gd_info")){ $this->throwError("NonGD"); } } private function setGroundImageInfo(){ $groundImageType = getimagesize($this->groundImage); $this->groundImageWidth = $groundImageType[0]; $this->groundImageHeight = $groundImageType[1]; $this->groundImageType = $groundImageType[2]; if($this->groundImageWidth < 150 or $this->groundImageHeight < 150){ $this->throwError("TooSmall"); } } private function setWaterTextInfo($waterText = "phpup.com",$waterTextColor = "#000000", $waterTextSize = "5", $waterTextFont = "fonts.ttf"){ $this->waterType = 0; if(strlen($waterTextColor) == 7){ $this->waterTextColor = &$waterTextColor; }else{ $this->throwError("WrongColor"); } $this->waterText = &$waterText; $this->waterTextSize = &$waterTextSize; $this->waterTextFont = &$waterTextFont; $waterTextInfo = imagettfbbox(ceil($this->waterTextSize*1.2), 0, $this->waterTextFont, $this->waterText); $this->waterWidth = $waterTextInfo[4] - $waterTextInfo[6]; $this->waterHeight = $waterTextInfo[1] - $waterTextInfo[7]; unset($waterTextInfo); } punlic function setWaterImageInfo($waterImage = "logo.gif"){ if(file_exists($waterImage)){ $this->waterType = 1; $this->waterImage = &$waterImage; $waterImageInfo = getimagesize($this->waterImage); $this->waterWidth = $waterImageInfo[0]; $this->waterHeight = $waterImageInfo[1]; $this->waterImageType = $waterImageInfo[2]; unset($waterImageInfo); }else{ $this->throwError("waterImage404"); } } private function setWaterPos(){ switch($this->waterPosType){ case 0://Ëæ»ú $this->waterPosX = rand(0,($this->groundImageWidth - $this->waterWidth)); $this->waterPosY = rand(0,($this->groundImageHeight - $this->waterHeight)); break; case 1://1Ϊ¶¥¶ËŸÓ×ó $this->waterPosX = 0; $this->waterPosY = 0; break; case 2://2Ϊ¶¥¶ËŸÓÖÐ $this->waterPosX = ($this->groundImageWidth - $this->waterWidth) / 2; $this->waterPosY = 0; break; case 3://3Ϊ¶¥¶ËŸÓÓÒ $this->waterPosX = $this->groundImageWidth - $this->waterWidth; $this->waterPosY = 0; break; case 4://4ΪÖв¿ŸÓ×ó $this->waterPosX = 0; $this->waterPosY = ($this->groundImageHeight - $this->waterHeight) / 2; break; case 5://5ΪÖв¿ŸÓÖÐ $this->waterPosX = ($this->groundImageWidth - $this->waterWidth) / 2; $this->waterPosY = ($this->groundImageHeight - $this->waterHeight) / 2; break; case 6://6ΪÖв¿ŸÓÓÒ $this->waterPosX = $this->groundImageWidth - $this->waterWidth; $this->waterPosY = ($this->groundImageHeight - $this->waterHeight) / 2; break; case 7://7Ϊµ×¶ËŸÓ×ó $this->waterPosX = 0; $this->waterPosY = $this->groundImageHeight - $this->waterHeight * rand(115,125) / 100; break; case 8://8Ϊµ×¶ËŸÓÖÐ $this->waterPosX = ($this->groundImageWidth - $this->waterWidth) / 2; $this->waterPosY = $this->groundImageHeight - $this->waterHeight * rand(115,125) / 100; break; case 9://9Ϊµ×¶ËŸÓÓÒ $this->waterPosX = $this->groundImageWidth - $this->waterWidth; $this->waterPosY = $this->groundImageHeight - $this->waterHeight; break; default://Ëæ»ú $this->waterPosX = rand(0,($this->groundImageWidth - $this->waterWidth)); $this->waterPosY = rand(0,($this->groundImageHeight - $this->waterHeight)); } } private function setGroundImageHandle(){ switch($this->groundImageType){ case 1: $this->groundImageHandle = imagecreatefromgif($this->groundImage); break; case 2: $this->groundImageHandle = imagecreatefromjpeg($this->groundImage); break; case 3: $this->groundImageHandle = imagecreatefrompng($this->groundImage); break; default: $this->throwError("NonType"); } } private function setWaterImageHandle(){ switch($this->waterImageType){ case 1: $this->waterImageHandle = imagecreatefromgif($this->waterImage); break; case 2: $this->waterImageHandle = imagecreatefromjpeg($this->waterImage); break; case 3: $this->waterImageHandle = imagecreatefrompng($this->waterImage); break; default: $this->throwError("NonType"); } } private function putWateredImage($extFileName){ switch($this->groundImageType){ case 1: imagegif($this->groundImageHandle, $extFileName.basename($this->groundImage)); break; case 2: //imagejpeg($this->groundImageHandle, $extFileName.basename($this->groundImage)); imagejpeg($this->groundImageHandle, $extFileName.'.jpg'); break; case 3: imagepng($this->groundImageHandle, $extFileName.basename($this->groundImage)); break; default: $this->throwError("NonType"); } } private function destroyHandle(){ imagedestroy($this->groundImageHandle); if(isset($this->waterImageHandle)){ imagedestroy($this->waterImageHandle); } } function makeWater($extFileName = ""){ $this->setGroundImageHandle(); $this->setWaterPos(9); imagealphablending($this->groundImageHandle, true); if($this->waterType == 0){ imagettftext($this->groundImageHandle, $this->waterTextSize, 0, $this->waterPosX, $this->waterHeight + $this->waterPosY, imagecolorallocate($this->groundImageHandle, hexdec(substr($this->waterTextColor,1,2)), hexdec(substr($this->waterTextColor,3,2)), hexdec(substr($this->waterTextColor,5,2))), $this->waterTextFont, $this->waterText); }else{ $this->setWaterImageHandle(); imagecopy($this->groundImageHandle,$this->waterImageHandle , $this->waterPosX, $this->waterPosY, 0, 0, $this->waterWidth,$this->waterHeight); } //@unlink($this->groundImage); $this->putWateredImage($extFileName); $this->destroyHandle(); } private function throwError($errType){ switch($errType){ case "TooSmall": $this->errorMsg = "ÒªŽòˮӡ͌Ƭ̫С"; break; case "groundImage404": $this->errorMsg = "ÒªŽòˮӡ͌Ƭ²»ŽæÔÚ"; break; case "waterImage404": $this->errorMsg = "ˮӡ͌Ƭ²»ŽæÔÚ"; break; case "NonGD": $this->errorMsg = "ûÓа²×°GD¿â"; break; case "NonType": $this->errorMsg = "²»Ö§³ÖµÄÎÄŒþžñÊœ"; break; case "WrongColor": $this->errorMsg = "ŽíÎóµÄÑÕÉ«žñÊœ"; break; default: $this->errorMsg = "ÎŽÖªŽíÎó"; } die($this->errorMsg); exit(); } } function EAN_8($code,$name) { //Ò»žöµ¥ÔªµÄ¿í¶È $lw = 2; //ÌõÐÐÂëµÄžß¶È $hi = 20; //ÌõÐÎÂëµÄËã·š $Guide = array(1=>'AAAAAA','AABABB','AABBAB','ABAABB','ABBAAB','ABBBAA','ABABAB','ABABBA','ABBABA'); $Lstart ='1'; $Lencode = array("A" => array('1001101','0111001','0000011','0110101','0100111','0110011','0101110','0111011','0110111','0001011'), "B" => array('0100111','0110011','0011011','0100101','0011101','0111001','0010101','0010001','0001001','0010111')); $Rencode = array('1110010','1100110','1101100','1010010','1011100', '1001110','1010010','1010100','1001010','1110100'); $center = '01'; $ends = '0'; if ( strlen($code) != 8 ) { die("ÇëÊäÈशλÓÐЧÊý×Ö"); } $lsum =0; $rsum =0; for($i=0;$i<(strlen($code)-1);$i++){ if($i % 2){ $lsum +=(int)$code[$i]; }else{ $rsum +=(int)$code[$i]; } } $tsum = $lsum*3 + $rsum; //Êä³öÌõÐÎÂë $barcode = $Lstart; for($i=1;$i<=4;$i++) { $barcode .= $Lencode [$Guide[$code[0]][($i-1)]] [$code[$i]]; } $barcode .= $center; for($i=5;$i<8;$i++) { $barcode .= $Rencode[$code[($i)]] ; } $barcode .= $ends; $img = ImageCreate($lw*55,$hi+25); //ÌõÐÎÂë͌ƬµÄ³ßŽç $fg = ImageColorAllocate($img, 0, 0, 0); //ºÚÉ« $bg = ImageColorAllocate($img, 255, 255, 255); //°×É« ImageFilledRectangle($img, 0, 0, $lw*75, $hi+30, $bg); for ($x=0;$x if ($barcode[$x] == '1') { $color = $fg; } else { $color = $bg; } ImageFilledRectangle($img, ($x*$lw)+5,5,($x+1)*$lw+29,$hi+5,$color); } for ($x=0;$x<8;$x++) { ImageString($img,5,$lw*(4+$x*6),$hi+5,$code[$x],$fg); //Êä³তžöÊý×Ö } //header("Content-Type: image/jpeg"); $result_name = $name.'.jpg'; ImageJPEG($img,$result_name); copy($result_name,'barcode/'.$result_name); unlink($result_name); echo 'create:barcode/ '.$result_name.' is ok! '; }
    标签: php 
    上一编文章 javascript循环切换图片代码(使用jquery) 下一编文章 远程linux添加防火墙80端口
    最近文章
    发表评论