PHP Classes
elePHPant
Icontem

Gig Cable Label: Generate label images in resistors code colours

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Info   Screenshots Screenshots   View files View files (97)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings  
2014-05-02 (1 year ago) RSS 2.0 feedNot yet rated by the usersTotal: 347 This week: 1All time: 6,434 This week: 1,122Up
Version License PHP version Categories  
gig-cables-code 1.0GNU Lesser Genera...5.3HTML, PHP 5, Graphics, Printing
Description Author  

This class can generate label images for color coded resistors.

It takes the resistance value of an electric resistor and generates an image for a page with a grid of labels that show the resistance value with the respective color code stripes, as well QR-code for the same for associated values.

The class can generate the label images. The generated images are saved to a file in the PNG format and are embedded in the generated page HTML.

It may also return a JSON encoded object with the label image dimensions and image data encoded in base64.

The size of the page and the number of labels in the grid are configurable parameters.

Recommendations  

What is the best PHP qr code generation class?
I need php code to generate qr code offline

Innovation Award  
PHP Programming Innovation award nominee
July 2012
Number 4
Resistors are components used frequently in electronic devices. They usually have color coded stripes that tell what is the resistance value of the resistor.

This class can generate labels suitable for printing that use the same color coded stripes used in resistors.

Manuel Lemos
Picture of Karl Holz
Name: Karl Holz is available for providing paid consulting. Contact Karl Holz .
Classes: 11 packages by
Country: Canada Canada
Age: 34
All time rank: 77421 in Canada Canada
Week rank: 119 Up3 in Canada Canada Up
Innovation award
Innovation award
Nominee: 6x

Details provided by the author  
# GigCableLabel

I currently work as a Tech in the Staging and AV world with various companies and I have noticed that many companies don't really follow a single industry standard for labeling their many cables, cases, lighting, audio, etc.  I wrote this program to help solve a problem that can help companies adopt a single standard for color coded labeling based on the resister colour code for 0-9.  The numeric codes are a maximum of 3 characters 0-9 and a letter for meters or feet.  QR code generation is placed beside the colour coded label.

### The generated QR code can contain:

- URL
- E-mail address
- Phone Number 
- SMS
- Address VCARDS
- Calendar Event
- Custom Text

QR codes have many uses for inventory, logistics, documentation, manifests or usage instructions to name a few.  I'm just tired of wasting my time looking for the right size cable when they are all labeled
differently or not at ALL! on a job site.  An example of the dilemma I would face when looking for a 50 foot cable; which cable is more likely to be a 50 ft cable marking that is clear? 

- The one with the red label?
- The one with the white label?
- The one with the yellow label? 
- Green and Black label?

In the resistor code, Green is 5 and Black is 0; so what do you think makes the most sence when hiring many freelancers and/or techs/hands from labour companies?  The resistor code is the winner, but most are stuck in their old ways


***

The QR code generation is from [phpqrcode](http://phpqrcode.sourceforge.net), this works very well and can be turned off in the class (not in the demo).  The QR code size (1 to 40) will be set based on the input string length and ECC level, all the maximum alphanumeric lengths I test against are listed on this [site](http://www.qrcode.com/en/vertable4.html) (link to page 4/4).  I've noticed that stretched QR code images don't work with Norton Snap on my iPhone 4, test your labels before you print any. I have 2 iPhone Apps for testing QR codes, Norton Snap and QRdeCODE. 

### The Demo:

The GigCableLabel demo will demonstrate this class in a RESTful Setup with PHP 5.3 and jQuery.  GET the label, PUT the label in the project directory, DELETE the selected labels from the project directory and POST a new project for labels.  There is still more work todo to make this tool a success, the UI is still very beta, view the [Demo](http://www.salamcast.com/demos/GigCableLabels/).  

- double click the new label to clear the new image and close the window or drag and drop the label on the project window to save it.
- added some multilingual support, English and goggle translated French, Arabic will be next -- Inshallah.
- I have also added Arabic Number labels, but with an English UI
- Multiple projects are now supported for multiple shows, shops, etc and can be made and loaded dynamically.
- Most of the web apps text has been moved into ini text files and loaded as a single JSON object, making it easier to edit and add translations
- added support for custom label sizes and page layout


### A look at the giglabel.php as an example of how to use this class can be used.

    <?php
    /* Gig Cable Label Class demo */
    /* HTTP/1.0 2XX Success Pages */
    function success_201($url) {
      header("HTTP/1.0 201 Created");
      header("Location: ".$url);
    }

    /* HTTP/1.0 4XX Error Pages */
    function error_400() {
     header("HTTP/1.0 400 Bad Request");
     json_header();
     $http=array();
     $http['header']="HTTP/1.0 400 Bad Request";
     $http['msg']="This request failed to load this resource";
     $http['request']='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
     $http['link']='http://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
     echo json_encode($http);
     exit();
    }

    function error_403($msg='') {
     header("HTTP/1.0 403 Forbidden");
     json_header();
     $http=array();
     $http['header']="HTTP/1.0 403 Forbidden";
     if ($msg=='' || !is_string($msg)) {
      $http['msg']="You're not allowed to request this resource this way "; 
     } else {
      $http['msg']=$msg;
     }
     $http['request']='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
     $http['link']='http://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
     echo json_encode($http);
     exit();
    }

    function error_404() {
     header("HTTP/1.0 404 Not Found");
     json_header();
     $http=array();
     $http['header']="HTTP/1.0 404 Not Found";
     $http['msg']="Couldn't find the requested resource";
     $http['request']='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
     $http['link']='http://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
     echo json_encode($http);
     exit();
    }

    function error_405() {
     header("HTTP/1.0 405 Method Not Allowed");
     json_header();
     $http=array();
     $http['header']="HTTP/1.0 405 Method Not Allowed";
     $http['msg']="You're not allowed to do this type of request on this resource";
     $http['request']='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
     $http['link']='http://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
     echo json_encode($http);
     exit();
    }

    function error_410() {
     header("HTTP/1.0 410 Gone");
     json_header();
     $http=array();
     $http['header']="HTTP/1.0 410 Gone";
     $http['msg']="This Resource is gone";
     $http['request']='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
     $http['link']='http://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
     echo json_encode($http);
     exit();
    }

    function error_412() {
     header("HTTP/1.0 412 Precondition Failed");
     json_header();
     $http=array();
     $http['header']="HTTP/1.0 412 Precondition Failed";
     $http['msg']="couldn't create/update the resource, try again";
     $http['request']='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
     $http['link']='http://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
     echo json_encode($http);
     exit();
    }

    function error_413() {
     header("HTTP/1.0 413 Request Entity Too Large");
     json_header();
     $http=array();
     $http['header']="HTTP/1.0 413 Request Entity Too Large";
     $http['msg']="You're discription is too large for this resource<br />- Consider lowering the Error Correction Capability ";
     $http['request']='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
     $http['link']='http://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
     echo json_encode($http);
     exit();
    }
    /* HTTP application/json header */
    function json_header() {
      header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
      header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT"); 
      header("Cache-Control: no-cache, must-revalidate"); 
      header("Pragma: no-cache");
      header("Content-type: application/json");
    }
    /* Genarates an UUID
     * 
     * @author Anis uddin Ahmad <admin@ajaxray.com>
     * @link   http://www.phpclasses.org/package/4427-PHP-Generate-feeds-in-RSS-1-0-2-0-an-Atom-formats.html
     * @param      string  an optional prefix
     * @return     string  the formated uuid
     */
    function uuid($key = null, $prefix ='QR') {
        $key = ($key == null)? 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] : $key;
        $chars = md5($key);
        $uuid  = substr($chars,0,8) . '-';
        $uuid .= substr($chars,8,4) . '-';
        $uuid .= substr($chars,12,4) . '-';
        $uuid .= substr($chars,16,4) . '-';
        $uuid .= substr($chars,20,12);
        return $prefix .'-'. $uuid;
    }

    require_once 'gigcablelabel.class.php';
    // view switcher
    if     (array_key_exists('ORIG_PATH_INFO', $_SERVER)) { $rest=$_SERVER['ORIG_PATH_INFO']; }
    elseif (array_key_exists('PATH_INFO', $_SERVER)) { $rest=$_SERVER['PATH_INFO']; } else { $rest=''; }
    // imgdata
    if  (array_key_exists('QUERY_STRING', $_SERVER)) { $imgdata=$_SERVER['QUERY_STRING']; } 
    else { $imgdata=''; }
    // RESTful CRUD access switcher
    if  (array_key_exists('REQUEST_METHOD', $_SERVER)) { $method=$_SERVER['REQUEST_METHOD']; } 
    else { $method=''; }

    $webdir=str_replace($_SERVER['DOCUMENT_ROOT'], '', dirname(__FILE__));

    if ($rest) {
     $r=explode('/', $rest);
     array_shift($r); // drop blank before /
     $page=array_shift($r);
     switch ($page) {
        case 'feed':
         // Make dir
         if($method == 'POST') {
          $p=file_get_contents("php://input");
          $post=json_decode($p, true);
          if (!array_key_exists('project', $post)) error_403();
          if (!ctype_alnum($post['project'])) error_403();
          $dir=GigCableLabel::dir().'/'.$post['project'];
          if (!is_dir($dir)) mkdir(trim($dir), 0750, true);
          success_201('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
         } else {
          $p=array_shift($r);
          $dir=GigCableLabel::dir().'/'.$p;
          if ($p == '') error_400();
          if (!ctype_alnum($p)) error_403();
          if (!is_dir($dir)) error_404();
         }

         if($method == 'DELETE') {
           $d=glob($dir.'/*.json');
           $f=$dir.'/'.$imgdata.'.json';
           if (in_array($f, $d)) {
            if (is_file($f)) { unlink($f); } else { error_410(); }
           } else { error_404(); }
           unset($d);
         } 
         if ($method == 'PUT') {
          $j=file_get_contents("php://input");
          $file=json_decode($j, true);
          if (! array_key_exists('code',$file) && !ctype_alnum ($file['code'])) error_404();
          if (! array_key_exists('width',$file) && !ctype_alnum ($file['width'])) error_404();
          if (! array_key_exists('height',$file) && !ctype_alnum ($file['height'])) error_404();
          if (! array_key_exists('x',$file) && !is_numeric($file['x'])) error_404();
          if (! array_key_exists('y',$file) && !is_numeric($file['y'])) error_404();

          $file['published']=gmdate(DATE_ATOM,time());
          $name=$dir."/".str_replace(':', '|', $file['published']).".json";
          $file['file']=$name;
          $file['url']='http://'.$_SERVER['HTTP_HOST'].$webdir.trim($name, '.');
          $file['id']=uuid($name, $file['code']);
          $file['put']='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
          $file['project']=$p;
          $json=json_encode($file, JSON_ERROR_SYNTAX);
          if (! file_put_contents( $name, $json)) { error_412(); }
          json_header();
          echo $json;
          exit();
    
         } elseif($method == 'DELETE' || $method == 'GET' || $method == 'POST') {
          $uri='http://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
          $id=uuid($uri, 'atom');
          $date=gmdate(DATE_ATOM,time());
          json_header();
          $j=glob($dir."/*.json");
          $proj=array();      
          foreach ($j as $c) {
           $link='http://'.$_SERVER['HTTP_HOST'].$webdir.urlencode(trim($c, '.'));
           $f=file_get_contents($c);
           $json=json_decode($f, true);
           $proj[]=$json;
          }
          echo json_encode($proj);
          exit();
         } else { error_405(); }
        break;
        case 'ui.json': GigCableLabel::load_ui($imgdata); break;
        case 'print':
         $width=array_shift($r);
         $height=array_shift($r);
         $x=array_shift($r);
         $y=array_shift($r);
         $img_style=array_shift($r);
         GigCableLabel::print_page($width, $height,$x,$y,$img_style,$imgdata) || error_404();    
        break;
        //case 'label':
        //    $code=array_shift($r);
        //    if (!ctype_alnum ($code)) error_404();
        //    $label=new GigCableLabel($code);
        //    $label->make_label();    
        //break;
        case 'QR':
            $lvl=array_shift($r);
            if (! ctype_upper($lvl)) error_404();
            $code=array_shift($r);
            if (!ctype_alnum ($code)) error_404();
            $desc=trim(implode('//', $r), '/');
            $label=new GigCableLabel($code);
            $label->qr=TRUE;
            $label->lang=$imgdata;
            $label->errorCorrectionLevel=$lvl;
            if ($label->desc=trim($desc)) {
              $label->make_label();
            } else { error_413(); }
        break;
     }
     exit();
    }
    //$label=new GigCableLabel();       
    //exit();
    ?>
   
### Thanks

- For creating the Arabic Images i took a look at the [ArGlyphs class](http://www.phpclasses.org/package/3192-PHP-Convert-Arabic-text-to-Unicode-for-rendering.html) by a brother from Syria - Khaled Al-Shamaa 
- It showed me how to use a ttf font file for creating UNICODE text images in GD images with imagettftext; imagestring was making the text too small.
Screenshots  
  • output
  • new-ui-july13.png
  Files folder image Files  
File Role Description
Files folder imagecss (2 files, 1 directory)
Files folder imageGigCableLabel (8 files)
Files folder imagejs (4 files)
Files folder imagephpqrcode (19 files, 3 directories)
Plain text file gigcablelabel.class.php Class Class that generates the png images from numeric strings
Accessible without login Plain text file giglabel.php Example Gig Cable label as web service, Accepts GET,POST,PUT and DELETE HTTP methods on select resources
Accessible without login HTML file index.html Doc. REST client to giglabel.php
Accessible without login Plain text file licence.txt Lic. Documentation
Accessible without login Image file NewGigCableLabel.png Data newer screen shot
Accessible without login Plain text file README.txt Doc. Documentation

  Files folder image Files  /  css  
File Role Description
Files folder imageimages (12 files)
  Accessible without login Plain text file qr.css Data Custom CSS
  Accessible without login Plain text file style.css Data jquery ui css

  Files folder image Files  /  css  /  images  
File Role Description
  Accessible without login Image file ui-bg_diagonals-th...48_1e158e_40x40.png Icon Icon image
  Accessible without login Image file ui-bg_diamond_75_09810b_10x8.png Icon Icon image
  Accessible without login Image file ui-bg_dots-medium_55_fbf9ee_4x4.png Icon Icon image
  Accessible without login Image file ui-bg_dots-medium_95_fef1ec_4x4.png Icon Icon image
  Accessible without login Image file ui-bg_hexagon_65_4c7d0d_12x10.png Icon Icon image
  Accessible without login Image file ui-bg_hexagon_75_1727d9_12x10.png Icon Icon image
  Accessible without login Image file ui-bg_hexagon_75_2d5841_12x10.png Icon Icon image
  Accessible without login Image file ui-bg_loop_0_1f08bf_21x21.png Icon Icon image
  Accessible without login Image file ui-bg_loop_30_7ea276_21x21.png Icon Icon image
  Accessible without login Image file ui-icons_2e83ff_256x240.png Icon Icon image
  Accessible without login Image file ui-icons_cd0a0a_256x240.png Icon Icon image
  Accessible without login Image file ui-icons_ffffff_256x240.png Icon Icon image

  Files folder image Files  /  GigCableLabel  
File Role Description
  Accessible without login Plain text file data.ini Data class config file
  Accessible without login Plain text file north_america.ini Data resister code colours
  Accessible without login Plain text file ui-department.ini Data UI Config
  Accessible without login Plain text file ui-level.ini Data UI Config
  Accessible without login Plain text file ui-page_size.ini Data UI Config
  Accessible without login Plain text file ui-type.ini Data UI Config
  Accessible without login Plain text file ui-unit.ini Data UI Config
  Accessible without login Plain text file ui.ini Data UI Config

  Files folder image Files  /  js  
File Role Description
  Accessible without login Plain text file jquery-1.7.2.min.js Data jquery
  Accessible without login Plain text file jquery-ui-1.8.21.custom.min.js Data jquery ui
  Accessible without login Plain text file json2.js Data jquery json
  Accessible without login Plain text file qr-label.js Data custom jquery for loading label preview

  Files folder image Files  /  phpqrcode  
File Role Description
Files folder imagebindings (1 directory)
Files folder imagecache (40 files)
Files folder imagetools (5 files)
  Accessible without login Plain text file CHANGELOG Data phpqrcode files needed by class for QR codes
  Accessible without login Plain text file index.php Example phpqrcode files needed by class for QR codes
  Accessible without login Plain text file INSTALL Doc. phpqrcode files needed by class for QR codes
  Accessible without login Plain text file LICENSE Lic. phpqrcode files needed by class for QR codes
  Accessible without login Plain text file phpqrcode.php Example phpqrcode files needed by class for QR codes
  Accessible without login Plain text file qrbitstream.php Example phpqrcode files needed by class for QR codes
  Accessible without login Plain text file qrconfig.php Conf. phpqrcode files needed by class for QR codes
  Accessible without login Plain text file qrconst.php Example phpqrcode files needed by class for QR codes
  Accessible without login Plain text file qrencode.php Example phpqrcode files needed by class for QR codes
  Accessible without login Plain text file qrimage.php Example phpqrcode files needed by class for QR codes
  Accessible without login Plain text file qrinput.php Example phpqrcode files needed by class for QR codes
  Accessible without login Plain text file qrlib.php Example phpqrcode files needed by class for QR codes
  Accessible without login Plain text file qrmask.php Example phpqrcode files needed by class for QR codes
  Accessible without login Plain text file qrrscode.php Example phpqrcode files needed by class for QR codes
  Accessible without login Plain text file qrspec.php Example phpqrcode files needed by class for QR codes
  Accessible without login Plain text file qrsplit.php Example phpqrcode files needed by class for QR codes
  Accessible without login Plain text file qrtools.php Example phpqrcode files needed by class for QR codes
  Accessible without login Plain text file README Data phpqrcode files needed by class for QR codes
  Accessible without login Plain text file VERSION Data phpqrcode files needed by class for QR codes

  Files folder image Files  /  phpqrcode  /  bindings  
File Role Description
Files folder imagetcpdf (1 file)

  Files folder image Files  /  phpqrcode  /  bindings  /  tcpdf  
File Role Description
  Accessible without login Plain text file qrcode.php Example Example script

  Files folder image Files  /  phpqrcode  /  cache  
File Role Description
  Accessible without login Image file frame_1.png Icon Icon image
  Accessible without login Image file frame_10.png Icon Icon image
  Accessible without login Image file frame_11.png Icon Icon image
  Accessible without login Image file frame_12.png Icon Icon image
  Accessible without login Image file frame_13.png Icon Icon image
  Accessible without login Image file frame_14.png Icon Icon image
  Accessible without login Image file frame_15.png Icon Icon image
  Accessible without login Image file frame_16.png Icon Icon image
  Accessible without login Image file frame_17.png Icon Icon image
  Accessible without login Image file frame_18.png Icon Icon image
  Accessible without login Image file frame_19.png Icon Icon image
  Accessible without login Image file frame_2.png Icon Icon image
  Accessible without login Image file frame_20.png Icon Icon image
  Accessible without login Image file frame_21.png Icon Icon image
  Accessible without login Image file frame_22.png Icon Icon image
  Accessible without login Image file frame_23.png Icon Icon image
  Accessible without login Image file frame_24.png Icon Icon image
  Accessible without login Image file frame_25.png Icon Icon image
  Accessible without login Image file frame_26.png Icon Icon image
  Accessible without login Image file frame_27.png Icon Icon image
  Accessible without login Image file frame_28.png Icon Icon image
  Accessible without login Image file frame_29.png Icon Icon image
  Accessible without login Image file frame_3.png Icon Icon image
  Accessible without login Image file frame_30.png Icon Icon image
  Accessible without login Image file frame_31.png Icon Icon image
  Accessible without login Image file frame_32.png Icon Icon image
  Accessible without login Image file frame_33.png Icon Icon image
  Accessible without login Image file frame_34.png Icon Icon image
  Accessible without login Image file frame_35.png Icon Icon image
  Accessible without login Image file frame_36.png Icon Icon image
  Accessible without login Image file frame_37.png Icon Icon image
  Accessible without login Image file frame_38.png Icon Icon image
  Accessible without login Image file frame_39.png Icon Icon image
  Accessible without login Image file frame_4.png Icon Icon image
  Accessible without login Image file frame_40.png Icon Icon image
  Accessible without login Image file frame_5.png Icon Icon image
  Accessible without login Image file frame_6.png Icon Icon image
  Accessible without login Image file frame_7.png Icon Icon image
  Accessible without login Image file frame_8.png Icon Icon image
  Accessible without login Image file frame_9.png Icon Icon image

  Files folder image Files  /  phpqrcode  /  tools  
File Role Description
  Accessible without login Plain text file merge.bat Data Auxiliary data
  Accessible without login Plain text file merge.php Example Example script
  Accessible without login Plain text file merge.sh Data Auxiliary data
  Accessible without login Plain text file merged_config.php Example Example script
  Accessible without login Plain text file merged_header.php Example Example script

 Version Control Unique User Downloads Download Rankings  
 97%Total:347All time:6,434
 This week:1This week:1,122Up