search
HomeBackend DevelopmentPHP Tutorial透过 PHP 生成 一维码

通过 PHP 生成 一维码
此代码来自  http://www.nixiaofeng.com/110.html

<?php$code = $_GET['code'];if ($code != "") {    if (!is_numeric($code)) die('输入的不是数字');    if (strlen($code) < 12 || strlen($code) > 13) die('条码长度不正确');    if (strlen($code) == 12) {        // 计算校验位        $lsum = 0;        $rsum = 0;        for($i=1; $i<=strlen($code); $i++) {            if($i % 2) {                $lsum += (int)$code[$i-1];            }else{                $rsum += (int)$code[$i-1];            }        }        $tsum = $lsum + $rsum * 3;        $chkdig = 10 - ($tsum % 10);        if ($chkdig == 10) $chkdig = 0;        $code .= $chkdig;    }    // 定义起始付    $start = '101';    // 定义中止符    $end = '101';    // 定义中间分隔符    $center = '01010';    // 定义左资料码    $Guide = array(0=>'AAAAAA','AABABB','AABBAB','AABBBA','ABAABB','ABBAAB','ABBBAA','ABABAB','ABABBA','ABBABA');    // 定义左侧码,分为“A”、“B”两种    $Lencode = array("A" => array('0001101','0011001','0010011','0111101','0100011','0110001','0101111','0111011','0110111','0001011'),    "B" => array('0100111','0110011','0011011','0100001','0011101','0111001','0000101','0010001','0001001','0010111'));    // 定义右侧码,统一为“C”编码    $Rencode = array('1110010','1100110','1101100','1000010','1011100','1001110','1010000','1000100','1001000','1110100');         // 编码起始符    $barcode = $start;    // 编码左资料位    for($i=1; $i<=6; $i++) {        $barcode .= $Lencode[$Guide[$code[0]][($i-1)]][$code[$i]];    }         // 编码中间分隔符    $barcode .= $center;         // 编码右资料位    for($i=7; $i<13; $i++) {        $barcode .= $Rencode[$code[($i)]];    }         // 编码中止符    $barcode .= $end;    // 定义每个条码单元的宽度和高度,单位是像素    $width = 2;    $height = 40;         // 定义起始符、中止符、中间分隔符的高度增量    $increment = 10;         // 创建方形(×95是因为整个条码共95个单元,+60和+30是给条码图片周围留空白边框)    $img = ImageCreate($width*95+60,$height+30);  // 目前这个方形是透明的         // “1”的颜色(黑)与“0”的颜色(白)    $fg = ImageColorAllocate($img, 0, 0, 0);    $bg = ImageColorAllocate($img, 255, 255, 255);         // 以“0”的颜色(白色),填充整个方形    ImageFilledRectangle($img, 0, 0, $width*95+60, $height+30, $bg);         // 循环编码后的每一个单元,输出条码图形    for ($x=0; $x<strlen($barcode); $x++) {        // ($x<4) 为起始符,($x>=92)为中止符,($x>=45 && $x<50)为中间分隔符        // 这3个需要将高度增加        if (($x<4) || ($x>=45 && $x<50) || ($x>=92)) {            $increment = 10;        } else {            $increment = 0;        }        // 当编码值为“1”时,输出黑色;当编码值为“0”时,输出白色        if ($barcode[$x] == '1') {            $color = $fg;        } else {            $color = $bg;        }        ImageFilledRectangle($img, ($x*$width)+30,5,($x+1)*$width+29,$height+$increment,$color);    }    ImageString($img, 5, 20, $height+5, $code[0], $fg);    for ($x=0; $x<6; $x++) {        // 左侧识别码        ImageString($img, 5, $width*(8+$x*6)+30, $height+5, $code[$x+1], $fg);        // 右侧识别码        ImageString($img, 5, $width*(53+$x*6)+30, $height+5, $code[$x+7], $fg);    }    header("Content-Type: image/jpeg");    ImageJPEG($img, "", 100);}?><form action="?">输入EAN-13条形码(如果输入12位长度,将自动计算校验位)<input type="text" name="code"> <input type="submit" value="生成条码图片"></form>


透过 PHP 生成 一维码

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)