Present different data outside of the network using PHP

This is very handy if you want to have a walled garden section inside your private network while still hosting public data.

<?php
$ipcheck = "192.168.0.";  //Replace with first 3 octets of your existing IP scheme
if( substr($_SERVER['REMOTE_ADDR'],0,strlen($ipcheck )) == $ipcheck ) {
    //Begin Internal code
}else{
    //Begin External code
}
?>

Send ZPL to Label Printer in PHP

I needed to generate ZPL data on the fly, and push it to Zebra label printers. I also wanted to be able to see the label on screen, so I also setup an API call to Labelary.

I ended up using pfsockopen(). This example requires Apache’s mod_rewrite and PHP curl.

<?php
$zaddress = "192.168.0.245"; //IP address of network connected printer
$bc = "123456789012"; //Data to include in label data - this example is just text
$qty = "1";
$zpldata = "
^XA
^PW812
^FT0,260^A0N,260^FB812,1,0,C^FD" . $bc . "\&^FS
^PQ" . $qty . "
^XZ";

try {
    $fp=pfsockopen($zaddress,9100);
    fputs($fp,$zpldata);
    fclose($fp);
    $status="success";
    $zpl = $zpldata;
    $curl = curl_init();// print density 8dpmm=203dpi, 12dpmm=300dpi / width x height / index nth (0)
    curl_setopt($curl, CURLOPT_URL, "http://api.labelary.com/v1/printers/8dpmm/labels/4.0x6.0/0/"); //downloads a 4x6 label image at 203dpi
    curl_setopt($curl, CURLOPT_POST, TRUE);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $zpl);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array("Accept: image/png"));
    $result = curl_exec($curl);
    if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == 200) {
        $file = fopen("printerlabel.png", "w");
        fwrite($file, $result); //writes the png file to the server
        fclose($file);
    }else{print_r("Error: $result");}
    curl_close($curl);
    $qty = "";
    }catch (Exception $e) {echo "<div class='alert alert-danger'>Caught exception: ",  $e->getMessage(), "\n</div>";}
}
?>

Format a background image in a DIV

I wanted to add a holiday closure notice to a website. I don’t write HTML/CSS code every day, so I just lookup what I need when I need it – that has served me very well for many years. These days, google search is great at giving garbage results. With AI generated websites, and lame article writers, it is hard to find what I am looking for.

I see obvious problems (to me), like using url(“somesite.com/picture.jpg”); – I thought the standard practice was to use double quotes for your style call-out, and use single quotes for nesting, but maybe that is just me. Even when I write standalone CSS, I still use single quotes. Maybe I am the odd one.

For this site, and task, inline CSS makes the most sense.

<div style="background-image:url('flag.jpg');background-repeat:no-repeat,no-repeat;width:100%;height:75px;">Content</div>
Content

Also, when the first presented article contains thousands of words, but it does not mention background- size, then I suspect that it is not written by someone who knows what they are doing.

So, my scenario: I found an American flag image, and it is quite wide. That is fine, I want to accommodate larger screens, but I want it to look good on smaller screens as well. It needs to “fit” instead of seeing only stars, or only the top stripe etc. That is where background-size comes into play. Depending on the shape of the block, you may want “contain” or “cover”. for me, cover was the right choice.

Of course, text color over red white and blue is a little tough, it has to be legible. I opted for yellow with a bit of text-shadow to make it stand out.

So my final code looks like:

<div style="background-image:url('flag.jpg');background-repeat:no-repeat,no-repeat;background-size:cover;width:100%;height:75px;color:yellow;text-align:center;line-height:75px;"><span style="text-shadow:0px 0px 10px black;"><b>Note: We will be closed for the 4th of July.</b></span></div>
Note: We will be closed for the 4th of July.

Bing bang boom, bob’s your uncle.