php生成条形码代码

 更新时间:2016年11月25日 16:58  点击:1384
成条形码就是必须生成图片了,在php生成图片我们就必须用到gd库来实现了,所以你得找到你的将php.ini文件找到extension=php_gd2.dll 去掉前面的;。你就可能使用些实例了。

*/

 代码如下 复制代码

class cd_barra
{
    var $file;
    var $into;
   
    var $cd_barras = array(0=>"00110",1=>"10001",2=>"01001",3=>"11000",4=>"00101",
                           5=>"10100",6=>"01100",7=>"00011",8=>"10010",9=>"01010"
                           );
    function cd_barra($value,$files,$into=1) {
      $lower = 1 ; $hight = 55;         
      $this->into = $into;
      for($count1=9;$count1>=0;$count1--){
        for($count2=9;$count2>=0;$count2--){  
          $count = ($count1 * 10) + $count2 ;
          $text = "" ;
          for($i=1;$i<6;$i++){
            $text .=  substr($this->cd_barras[$count1],($i-1),1) . substr($this->cd_barras[$count2],($i-1),1);
          }
          $this->cd_barras[$count] = $text;
       }
      }
   
          //$img         = imagecreate($lower*95+300,$hight+30);
          $img         = imagecreate(145,55);
   
    //$img         = imagecreate(395,73);  
          $cl_black = imagecolorallocate($img, 0, 0, 0);
          $cl_white = imagecolorallocate($img, 255, 255, 255);
   
              
          
          imagefilledrectangle($img, 0, 0, $lower*95+1000, $hight+30, $cl_white);
          
   
          imagefilledrectangle($img, 1,1,1,53,$cl_black);
          imagefilledrectangle($img, 2,1,2,53,$cl_white);
          imagefilledrectangle($img, 3,1,3,53,$cl_black);
          imagefilledrectangle($img, 4,1,4,53,$cl_white);
   
   
   
    $thin = 1 ;
    if(substr_count(strtoupper($_server['server_software']),"win32")){
        //o tamanho para windows tem que ser 3
        // for windows, the wide bar has = 3
         $wide = 3;
    } else {
            $wide = 2.72;
       }
    $pos   = 5 ;
    $text = $value ;
    if((strlen($text) % 2) <> 0){
        $text = "0" . $text;
    }
   
   
    while (strlen($text) > 0) {
      $i = round($this->barra_left($text,2));
      $text = $this->barra_right($text,strlen($text)-2);
      
      $f = $this->cd_barras[$i];
      
      for($i=1;$i<11;$i+=2){
        if (substr($f,($i-1),1) == "0") {
          $f1 = $thin ;
        }else{
          $f1 = $wide ;
        }
   
      
      imagefilledrectangle($img, $pos,1,$pos-1+$f1,53,$cl_black)  ;
      $pos = $pos + $f1 ;  
      
      if (substr($f,$i,1) == "0") {
          $f2 = $thin ;
        }else{
          $f2 = $wide ;
        }
   
      imagefilledrectangle($img, $pos,1,$pos-1+$f2,53,$cl_white)  ;
      $pos = $pos + $f2 ;  
      }
    }
   
       
    imagefilledrectangle($img, $pos,1,$pos-1+$wide,53,$cl_black);
    $pos=$pos+$wide;
   
    imagefilledrectangle($img, $pos,1,$pos-1+$thin,53,$cl_white);
    $pos=$pos+$thin;
   
   
    imagefilledrectangle($img, $pos,1,$pos-1+$thin,53,$cl_black);
    $pos=$pos+$thin;
   
    $this->put_img($img,$files);
    }
   
    function barra_left($input,$comp){
        return substr($input,0,$comp);
    }
   
    function barra_right($input,$comp){
        return substr($input,strlen($input)-$comp,$comp);
    }
   
    function put_img($image,$file){
        if($this->into){           
   imagegif($image,$file);
        }
  else {
                    header("content-type: image/gif");
                    imagegif($image);
             }
        imagedestroy($image);
    }
}

?>

//调用 方法

 代码如下 复制代码

<?php
  include("codes.php");
 $new_code = new cd_barra("1234567890","a.gif",1);
 
 ?>
   <img src="a.gif"   />

本款程序可以获取图片大小,格式等信息,同时还可以对图片进行缩略图处理与给图片加水印功能哦。
 代码如下 复制代码

class image{
        public $filename;
        private $info;
        private $im;
  

//构造
        public function __construct($filename){
                $this->filename = $filename;
                $this->info    = @getimagesize($filename);
    if($this->info[2]>3){echo "只支持gif、jpeg、png 格式。";exit;}
  
        }
       
// 载入
        public function imgload(){
   switch($this->info[2]){
    case 1:
    $this->im=@imagecreatefromgif($this->filename);
    break; 
    case 2:
    $this->im=@imagecreatefromjpeg($this->filename);
    break; 
    case 3:
    $this->im=@imagecreatefrompng($this->filename);
    break; 
   }
        }

// 保存
        public function imagesave($img,$savename,$inf){
   switch($inf){
    case 1:
    imagegif($img,$savename);;
    break; 
    case 2:
    imagejpeg($img,$savename);;
    break; 
    case 3:
    imagepng($img,$savename);;
    break; 
   }
        }  

  
//滤镜
 public function filter($arg=1,$savename=''){
  $this->imgload();
  
  if($savename=='')$savename='f_'.$this->filename;
     $ok=false;
     switch($arg){
   case 1:
   imagefilter($this->im,img_filter_negate);//反色
   $ok=true;
   break;
   case 2:
   imagefilter($this->im,img_filter_grayscale); //黑白
   $ok=true;
   break;
   case 3:
   imagefilter($this->im,img_filter_emboss);//浮雕
   $ok=true;
   break;
   case 4:
   imagefilter($this->im,img_filter_gaussian_blur); //高斯模糊
   $ok=true;
   break;
   case 5:
   imagefilter($this->im,img_filter_brightness,50); //亮度50
   $ok=true;
   break;
   case 6:
   imagefilter($this->im,img_filter_contrast,-50); //对比度-50
   $ok=true;
   break;  
  }
  if($ok){  
   $this->imagesave($this->im,$savename,$this->info[2]);  //写文件
   imagedestroy($this->im);
   return 1;
  }else{
   imagedestroy($this->im);
   return 0;}
 }
 

 

这款图片上传代码可以把上传的图片增加水印,生成小图片,同时还可以生成文字水印。

 代码如下 复制代码
class upimages {
        var $annexfolder = "upload";//附件存放点,默认为:annex
        var $smallfolder = "small";//缩略图存放路径,注:必须是放在 $annexfolder下的子目录,默认为:smallimg
        var $markfolder = "mark";//水印图片存放处
        var $upfiletype = "jpg gif png";//上传的类型,默认为:jpg gif png rar zip
        var $upfilemax = 1024;//上传大小限制,单位是"kb",默认为:1024kb
        var $fonttype;//字体
        var $maxwidth = 500; //图片最大宽度
        var $maxheight = 600; //图片最大高度
        function upimages($annexfolder,$smallfolder,$includefolder) {
                $this->annexfolder = $annexfolder;
                $this->smallfolder = $smallfolder;
                $this->fonttype = $includefolder."/04b_08__.ttf";
        }
        function upload($inputname) {
                $imagename = time();//设定当前时间为图片名称
                if(@empty($_files[$inputname]["name"])) die("没有上传图片信息,请确认");
                $name = explode(".",$_files[$inputname]["name"]);//将上传前的文件以"."分开取得文件类型
                $imgcount = count($name);//获得截取的数量
                $imgtype = $name[$imgcount-1];//取得文件的类型
                if(strpos($this->upfiletype,$imgtype) === false) die(error("上传文件类型仅支持 ".$this->upfiletype." 不支持 ".$imgtype));
                $photo = $imagename.".".$imgtype;//写入数据库教程的文件名
                $uploadfile = $this->annexfolder."/".$photo;//上传后的文件名称
                $upfileok = move_uploaded_file($_files[$inputname]["tmp_name"],$uploadfile);
                if($upfileok) {
                        $imgsize = $_files[$inputname]["size"];
                        $ksize = round($imgsize/1024);
                        if($ksize > ($this->upfilemax*1024)) {
                                @unlink($uploadfile);
                                die(error("上传文件超过 ".$this->upfilemax."kb"));
                        }
                } else {
                        die(error("上传图片失败,请确认你的上传文件不超过 $upfilemax kb 或上传时间超时"));
                }
                return $photo;
        }
        function getinfo($photo) {
                $photo = $this->annexfolder."/".$photo;
                $imageinfo = getimagesize($photo);
                $imginfo["width"] = $imageinfo[0];
                $imginfo["height"] = $imageinfo[1];
                $imginfo["type"] = $imageinfo[2];
                $imginfo["name"] = basename($photo);
                return $imginfo;
        }
        function smallimg($photo,$width=128,$height=128) {
                $imginfo = $this->getinfo($photo);
                $photo = $this->annexfolder."/".$photo;//获得图片源
                $newname = substr($imginfo["name"],0,strrpos($imginfo["name"], "."))."_thumb.jpg";//新图片名称
                if($imginfo["type"] == 1) {
                        $img = imagecreatefromgif($photo);
                } elseif($imginfo["type"] == 2) {
                        $img = imagecreatefromjpeg($photo);
                } elseif($imginfo["type"] == 3) {
                        $img = imagecreatefrompng($photo);
                } else {
                        $img = "";
                }
                if(empty($img)) return false;
                $width = ($width > $imginfo["width"]) ? $imginfo["width"] : $width;
                $height = ($height > $imginfo["height"]) ? $imginfo["height"] : $height;
                $srcw = $imginfo["width"];
                $srch = $imginfo["height"];
                if ($srcw * $width > $srch * $height) {
                        $height = round($srch * $width / $srcw);
                } else {
                        $width = round($srcw * $height / $srch);
                }
                if (function_exists("imagecreatetruecolor")) {
                        $newimg = imagecreatetruecolor($width, $height);
                        imagecopyresampled($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo["width"], $imginfo["height"]);
                } else {
                        $newimg = imagecreate($width, $height);
                        imagecopyresized($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo["width"], $imginfo["height"]);
                }
                if ($this->tofile) {
                        if (file_exists($this->annexfolder."/".$this->smallfolder."/".$newname)) @unlink($this->annexfolder."/".$this->smallfolder."/".$newname);
                        imagejpeg($newimg,$this->annexfolder."/".$this->smallfolder."/".$newname);
                        return $this->annexfolder."/".$this->smallfolder."/".$newname;
                } else {
                        imagejpeg($newimg);
                }
                imagedestroy($newimg);
                imagedestroy($img);
                return $newname;
        }
        function watermark($photo,$text) {
                $imginfo = $this->getinfo($photo);
                $photo = $this->annexfolder."/".$photo;
                $newname = substr($imginfo["name"], 0, strrpos($imginfo["name"], ".")) . "_mark.jpg";
                switch ($imginfo["type"]) {
                        case 1:
                                $img = imagecreatefromgif($photo);
                        break;
                        case 2:
                                $img = imagecreatefromjpeg($photo);
                        break;
                        case 3:
                                $img = imagecreatefrompng($photo);
                        break;
                        default:
                                return false;
                }
                if (empty($img)) return false;
                $width = ($this->maxwidth > $imginfo["width"]) ? $imginfo["width"] : $this->maxwidth;
                $height = ($this->maxheight > $imginfo["height"]) ? $imginfo["height"] : $this->maxheight;
                $srcw = $imginfo["width"];
                $srch = $imginfo["height"];
                if ($srcw * $width > $srch * $height) {
                        $height = round($srch * $width / $srcw);
                } else {
                        $width = round($srcw * $height / $srch);
                }
                if (function_exists("imagecreatetruecolor")) {
                        $newimg = imagecreatetruecolor($width, $height);
                        imagecopyresampled($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo["width"], $imginfo["height"]);
                } else {
                        $newimg = imagecreate($width, $height);
                        imagecopyresized($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo["width"], $imginfo["height"]);
                }
               
                $white = imagecolorallocate($newimg, 255, 255, 255);
                $black = imagecolorallocate($newimg, 0, 0, 0);
                $alpha = imagecolorallocatealpha($newimg, 230, 230, 230, 40);
                imagefilledrectangle($newimg, 0, $height-26, $width, $height, $alpha);
                imagefilledrectangle($newimg, 13, $height-20, 15, $height-7, $black);
                imagettftext($newimg, 4.9, 0, 20, $height-14, $black, $this->fonttype, $text[0]);
                imagettftext($newimg, 4.9, 0, 20, $height-6, $black, $this->fonttype, $text[1]);
                if($this->tofile) {
                        if (file_exists($this->annexfolder."/".$this->markfolder."/".$newname)) @unlink($this->annexfolder."/".$this->markfolder."/".$newname);
                        imagejpeg($newimg,$this->annexfolder."/".$this->markfolder."/".$newname);
                        return $this->annexfolder."/".$this->markfolder."/".$newname;
                } else {
                        imagejpeg($newimg);
                }
                imagedestroy($newimg);
                imagedestroy($img);
                return $newname;
        }
}
这是一段完美的php柱状图生成类代码,可以生成漂亮实用的柱状图
 代码如下 复制代码
function createimage($data,$twidth,$tspace,$height){
                        $dataname = array();
                        $datavalue = array();
                        $i = 0;
                        $j = 0;
                        $k = 0;
                        $num = sizeof($data);
                       
                        foreach($data as $key => $val){
                                        $dataname[] = $key;
                                        $datavalue[] = $val;
                                }
       
                        $maxnum = max($data);
                        $width = ($twidth + $tspace) * $num + 4;//image's width
                        $im = imagecreate($width + 40,$height+20);
                        $linecolor = imagecolorallocate($im,12,12,12);
                        $bgcolor = imagecolorallocate($im,235,233,233);
                        $tcolor = imagecolorallocate($im,123,200,56);
                        imagefill($im,0,0,$bgcolor);
                        imageline ( $im, 30, 0, 30, $height - 2, $linecolor);
                        imageline ( $im, 30, $height - 2, $width + 30 -2 , $height - 2,$linecolor);
                        while($i < $num){
                                imagefilledrectangle ( $im, $i * ($tspace+$twidth) + 40, $height - $datavalue[$i], $i * ($tspace+$twidth) + 40 + $twidth, $height - 3, $tcolor);
                                imagestringup ( $im, 4, $i * ($tspace+$twidth) + $twidth/2 + 30, $height - 10, $dataname[$i]."(".$datavalue[$i].")", $linecolor);
                                $i++;
                        }
                        while($j <= (500/10)){
                                imagestringup ( $im, 4, 2, $height - $j * 10 + 10, $j * 10, $linecolor);
                                $j = $j + 10;
                        }
                        while($k <= (500/10)){
                                if($k != 0)
                                imageline ( $im, 28, $height - $k * 10, 32 , $height - $k * 10,$linecolor);
                                $k = $k + 10;
                        }
                        imagepng($im);
                }

//调用方法:

 代码如下 复制代码
header("content-type:image/png");
$data = array("yahoo" => 120, "google" => 260,"microsoft" => 320,"ibm" => 290,"sun system" => 150,"inter" => 260);
createimage($data,38,25,460);
这款程序给图片加文字水印时是调用 了C:\\WINDOWS\\Fonts\\\\SIMHEI.TTF字体,给图片加水印时就可以自定图片哦。

$image->wprint_img();//执行图片水印
$image->wprint_string();//执行文字水印
*/

 代码如下 复制代码

class editimage{
 private $imagefile;//图片文件
 private $smallimg;//水印图片
 private $string;//水印文字
 private $position;//存放位置
 private $dst_x=600;//原始图片打水印x坐标
 private $dst_y=0;//原始图片打水印y坐标
 private $str_x=450;
 private $str_y=200;
 private $font="c:windows ontssimhei.ttf";//原始图片打水印字体路径
 private $imgej;// imagecolorallocate后的变量
 
 function __get($value){
  return $this->$value;
 }
 function __set($property,$value){
  $this->$property=$value;
 }
 /**
  * 构造函数初始化
  *
  * @param string $imagefile 被上水印的文件
  * @param string $smallimg 水印文件
  * @param string $string 水印文字
  * @param string $position 存放位置
  * @param int $dst_x  被上水印的图片x
  * @param int $dst_y  被上水印的图片y
  */
 function __construct($imagefile,$smallimg='',$string=''){//,$position='',$dst_x=0,$dst_y=0
  $this->imagefile=$imagefile;
  $this->smallimg=$smallimg;
  $this->string=$string;
  $this->imgej=$this->imagecreatef($this->imagefile);
 }

 function get_extname($file){//获取文件的后缀名
  if (file_exists($this->imagefile)) {
   $img=getimagesize($file);
   switch ($img[2]){
    case "1":
     return "gif";
    case "2":
     return "jpg";
    case "3":
     return "png";
   }
  }else{
   return false;
  }
 }  
 

 

[!--infotagslink--]

相关文章

  • 不打开网页直接查看网站的源代码

      有一种方法,可以不打开网站而直接查看到这个网站的源代码..   这样可以有效地防止误入恶意网站...   在浏览器地址栏输入:   view-source:http://...2016-09-20
  • php 调用goolge地图代码

    <?php require('path.inc.php'); header('content-Type: text/html; charset=utf-8'); $borough_id = intval($_GET['id']); if(!$borough_id){ echo ' ...2016-11-25
  • JS基于Mootools实现的个性菜单效果代码

    本文实例讲述了JS基于Mootools实现的个性菜单效果代码。分享给大家供大家参考,具体如下:这里演示基于Mootools做的带动画的垂直型菜单,是一个初学者写的,用来学习Mootools的使用有帮助,下载时请注意要将外部引用的mootools...2015-10-23
  • JS+CSS实现分类动态选择及移动功能效果代码

    本文实例讲述了JS+CSS实现分类动态选择及移动功能效果代码。分享给大家供大家参考,具体如下:这是一个类似选项卡功能的选择插件,与普通的TAb区别是加入了动画效果,多用于商品类网站,用作商品分类功能,不过其它网站也可以用,...2015-10-21
  • JS实现自定义简单网页软键盘效果代码

    本文实例讲述了JS实现自定义简单网页软键盘效果。分享给大家供大家参考,具体如下:这是一款自定义的简单点的网页软键盘,没有使用任何控件,仅是为了练习JavaScript编写水平,安全性方面没有过多考虑,有顾虑的可以不用,目的是学...2015-11-08
  • php 取除连续空格与换行代码

    php 取除连续空格与换行代码,这些我们都用到str_replace与正则函数 第一种: $content=str_replace("n","",$content); echo $content; 第二种: $content=preg_replac...2016-11-25
  • php简单用户登陆程序代码

    php简单用户登陆程序代码 这些教程很对初学者来讲是很有用的哦,这款就下面这一点点代码了哦。 <center> <p>&nbsp;</p> <p>&nbsp;</p> <form name="form1...2016-11-25
  • PHP实现清除wordpress里恶意代码

    公司一些wordpress网站由于下载的插件存在恶意代码,导致整个服务器所有网站PHP文件都存在恶意代码,就写了个简单的脚本清除。恶意代码示例...2015-10-23
  • js识别uc浏览器的代码

    其实挺简单的就是if(navigator.userAgent.indexOf('UCBrowser') > -1) {alert("uc浏览器");}else{//不是uc浏览器执行的操作}如果想测试某个浏览器的特征可以通过如下方法获取JS获取浏览器信息 浏览器代码名称:navigator...2015-11-08
  • JS实现双击屏幕滚动效果代码

    本文实例讲述了JS实现双击屏幕滚动效果代码。分享给大家供大家参考,具体如下:这里演示双击滚屏效果代码的实现方法,不知道有觉得有用处的没,现在网上还有很多还在用这个特效的呢,代码分享给大家吧。运行效果截图如下:在线演...2015-10-30
  • JS日期加减,日期运算代码

    一、日期减去天数等于第二个日期function cc(dd,dadd){//可以加上错误处理var a = new Date(dd)a = a.valueOf()a = a - dadd * 24 * 60 * 60 * 1000a = new Date(a)alert(a.getFullYear() + "年" + (a.getMonth() +...2015-11-08
  • PHP开发微信支付的代码分享

    微信支付,即便交了保证金,你还是处理测试阶段,不能正式发布。必须到你通过程序测试提交订单、发货通知等数据到微信的系统中,才能申请发布。然后,因为在微信中是通过JS方式调用API,必须在微信后台设置支付授权目录,而且要到...2014-05-31
  • php二维码生成

    本文介绍两种使用 php 生成二维码的方法。 (1)利用google生成二维码的开放接口,代码如下: /** * google api 二维码生成【QRcode可以存储最多4296个字母数字类型的任意文本,具体可以查看二维码数据格式】 * @param strin...2015-10-21
  • Java生成随机姓名、性别和年龄的实现示例

    这篇文章主要介绍了Java生成随机姓名、性别和年龄的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-10-01
  • PHP常用的小程序代码段

    本文实例讲述了PHP常用的小程序代码段。分享给大家供大家参考,具体如下:1.计算两个时间的相差几天$startdate=strtotime("2009-12-09");$enddate=strtotime("2009-12-05");上面的php时间日期函数strtotime已经把字符串...2015-11-24
  • C#生成随机数功能示例

    这篇文章主要介绍了C#生成随机数功能,涉及C#数学运算与字符串操作相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • php怎么用拼音 简单的php中文转拼音的实现代码

    小编分享了一段简单的php中文转拼音的实现代码,代码简单易懂,适合初学php的同学参考学习。 代码如下 复制代码 <?phpfunction Pinyin($_String...2017-07-06
  • php导出csv格式数据并将数字转换成文本的思路以及代码分享

    php导出csv格式数据实现:先定义一个字符串 存储内容,例如 $exportdata = '规则111,规则222,审222,规222,服2222,规则1,规则2,规则3,匹配字符,设置时间,有效期'."/n";然后对需要保存csv的数组进行foreach循环,例如复制代...2014-06-07
  • ecshop商品无限级分类代码

    ecshop商品无限级分类代码 function cat_options($spec_cat_id, $arr) { static $cat_options = array(); if (isset($cat_options[$spec_cat_id]))...2016-11-25
  • 几种延迟加载JS代码的方法加快网页的访问速度

    本文介绍了如何延迟javascript代码的加载,加快网页的访问速度。 当一个网站有很多js代码要加载,js代码放置的位置在一定程度上将会影像网页的加载速度,为了让我们的网页加载速度更快,本文总结了一下几个注意点...2013-10-13