Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Sameer Borate:
Unpacking binary data in PHP
Apr 04, 2018 @ 14:19:40

Sameer Borate has a new post to his Code Deisel blog showing how to unpack binary data in PHP making use of the pack and unpack functions.

Working with binary files in PHP is rarely a requirement. However when needed the PHP ‘pack’ and ‘unpack’ functions can help you tremendously. To set the stage we will start with a programming problem, this will keep the discussion anchored to a relevant context. The problem is this : We want to write a function that takes a image file as an argument and tells us whether the file is a GIF image; irrelevant with whatever the extension the file may have. We are not to use any GD library functions.

He starts by briefly covering the "metadata" that binary files contain in their headers about the type of file and, more specifically, what the content for a GIF looks like. He follows this with an example of using unpack to provide a readable version of the file's binary content. He then uses this in an is_gif function to read the contents of a file, check the header format and ensure the version is either GIF87a or GIF89a. He also includes the code for another method to get attributes about the file like height, width and the aspect ratio. He ends the post with more details on the contents of the header and what each character means.

tagged: binary data unpack pack tutorial gif image

Link: https://www.codediesel.com/php/unpacking-binary-data/

Freek Van der Herten:
Easily optimize images using PHP (and some binaries)
Jul 07, 2017 @ 15:19:27

Freek Van der Herten has a post on his site sharing a new package that's been developed to help optimize images using some PHP and a few other helpful tools.

Our recently released image-optimizer package can shave off some kilobyes of PNGs, JPGs, SVGs and GIFs by running them through a chain of various image optimization tools. In this blog post I’ll tell you all about it.

He starts off by talking about why they built the package - to make sure the images on their site were as optimized as possible - and what kind of tools are involved in making it work. Those tools are free to use but it does take a little manipulation to ensure the right data is being passed into each. He then gets into the code examples, showing how to optimize an image with just a few lines. The package determines based on the kind of image which optimizing tool to use. He also includes example code showing how to customize the optimization process with extra command line flags. Finally he includes an extra section showing how to integrate it with some of their other packages: the image package, laravel-medialibrary and Browsershot.

tagged: tutorial package optimize image binary imagetype tools

Link: https://murze.be/2017/07/easily-optimize-images-using-php-binaries/

Rob Allen:
Inserting binary data into SQL Server with ZF1 & PHP 7
May 22, 2017 @ 14:34:51

In an interesting mix of "old" and "new" Rob Allen as shown in this new post to his site how to push binary data into a SQL Server database from a Zend Framework v1 application.

If you want to insert binary data into SQL Server in Zend Framework 1 then you probably used the trick of setting an array as the parameter's value with the info required by the sqlsrv driver as noted in Some notes on SQL Server blobs with sqlsrv.

[...] Working through the problem, I discovered that this is due to Zend_Db_Statement_Sqlsrv converting the $params array to references with this code. The Sqlsrv driver (v4) for PHP 7 does not like this! As Zend Framework 1 is EOL, we can't get a fix into upstream and update the new release, so we have to write our solution.

He includes the code for the "hack" that you'd normally have to do to push the binary data into the database. Zend Framework v1 is EOL (end of life) so the Zend_Db_Statement_Sqlsrv class can't be updated. Instead, he writes his own replacement, creating a new adapter specific to the application that handles the input as the SQL Server driver is expecting. He then updates the application configuration to force the new adapter to be used when the ZF1 application needs to connect to the SQL Server database.

tagged: insert binary data sqlserver zendframework php7 tutorial adapter

Link: https://akrabat.com/inserting-binary-data-into-sql-server-with-zf1-php-7/

That Podcast:
Episode 30: ...
Apr 25, 2016 @ 14:47:48

That Podcas, hosted by PHP community members Beau Simensen and Dave Marshall, has posted their latest episode - Episode #30: ....

Beau and Dave talk about Beau's recent trip to Cornwall, some family stuff like schools, Linux binaries on windows, parallelshell woes, cloud formation woes and FounderDating.com.

Other topics mentioned in this episode include:

You can listen to this latest episode either through the in-page audio player or by downloading the mp3 directly for listening at your leisure. If you enjoy the show, be sure to subscribe to their feed too.

tagged: thatpodcast ep30 cornwall school linux binary parallelshell cloudformation podcast

Link: https://thatpodcast.io/episodes/episode-30

Stephan Hochdörfer:
Running PHPUnit via Phing on HHVM
Feb 26, 2015 @ 15:16:58

Stephan Hochdörfer has a quick post showing how he has PHPunit up and working on an HHVM instance. His problem was that the tests were actually executing using the "php" binary, not the HHVM one.

For quite some time we run the unit tests for our libs and tools against PHP and HHVM, at least that is what I thought up to now. As it turns out I missed a minor detail. [...] What happens now is that Phing is executed via HHVM but PHPUnit will still be executed via the PHP binary because the PHPUnit shell script will look for the php binary in the PATH configuration. Since we run HHVM side-by-side with PHP on our Jenkins build nodes I was not able to point /usr/bin/php to /usr/bin/hhvm - which would be the easiest and cleanest solution. I

He shares the workaround he created, creating a symbolic link between the hhvm and php binaries and then executing the Phing task to run the tests. This is being run via Jenkins and uses it's "WORKSPACE" as a container so the main "php" binary isn't overwritten.

tagged: hhvm phpunit test unittest execute binary path jenkins phing task

Link: https://blog.bitexpert.de/blog/running-phpunit-via-phing-on-hhvm/

Mikko Koppanen:
Memcached protocol support
Nov 15, 2013 @ 17:15:38

Mikko Koppanen has posted about some updates he's making to the PECL memcached extension, more specifically around binary protocol support.

There are quite a few things still missing and only binary protocol is supported at the moment, but the code seems to work reasonably well in small-scale testing. I am not sure whether this is useful for anyone, but at least it allows things such as quick prototyping of network servers, exposing sqlite database over memcached protocol etc.

An example of code using this new functionality is included in the post showing how to create both a simple sever with attached "get" and "set" handlers and a client to connect to it and enable the binary protocol.

The code is still work in progress but it’s available in github: https://github.com/mkoppanen/php-memcached/tree/feature-server. Note that you need to compile libmemcached with –enable-libmemcachedprotocol and the PECL memcached extension with –enable-memcached-protocol.
tagged: memcached binary support protocol pecl update

Link: http://valokuva.org/memcached-protocol-support/

PHPMaster.com:
Base Converting and Binary Operators
Apr 18, 2013 @ 16:44:05

On PHPMaster.com there's a new tutorial from Timothy Boronczyk focusing on the binary operators in PHP and using them to do some base conversions.

This article discusses PHP’s binary operators and how to convert between different counting systems. Most programming books and articles only dedicate a page or two to such topics, and although using the operators is really quite simple, there’s a fair amount of background knowledge one must have to use them correctly. Instead of giving the same bare-bones treatment that every other reference gives, I’ll first provide you the necessary background in number theory. Armed with that knowledge, you’ll be able to understand the binary counting system and base conversions… and familiarity with binary digits is the key to successful use of the binary operators!

As promised he starts off with some number theory complete with illustrations about base-10 and how the values fit into "buckets" of data. He compares this to base-2 (binary) and only then starts getting into the conversion functions. He shows the usage of things like decbin and dechex to modify the values as well as the use of bitwise and binary operators.

tagged: converting binary bitwise operator tutorial numbertheory

Link: http://phpmaster.com/base-converting-and-binary-operators

Sameer Borate:
Storing images into a database - resolving a contentious matter
Feb 21, 2013 @ 17:16:45

In this new post to his CodeDiesel site Sameer Borate looks at something that's been a controversial topic with developers (not just PHP) about storing binary data, like images, in a database instead of on the local file system.

There is much discussion and argument with no final say on the issue. In one of my recent project the same issue was raised; the client and myself discussing the benefits and drawback of storing the images into a database. The project needed storing around 50,000 images, so it was important to get the question resolved satisfactorily. After much deliberation we settled on using the file system. The major factor in the decision was that we needed the database and images decoupled as we would be having multiple databases using the same set of images.

He goes on to talk about some of the things you should consider when you're deciding if storing images in the database is the right thing for your application including:

  • The bloat that can come with storing binary data (larger database size)
  • Updating images requires two operations - updating the database and updating the cached image locally
  • Images usually serve faster when they come from the filesystem through the web server
  • BLOB (a common type for binary data storage) is variable-width and can degrade performance

You can read the rest of the reasons (and get more detail on the ones above) in the rest of the post.

tagged: images binary data storage database benefits disadvantages

Link:

Igor Wiedler:
Binary parsing with PHP
Sep 25, 2012 @ 17:17:41

Igor Wiedler has a new post to his blog showing how to work with binary data in your PHP applications a few different built-in functions including unpack and bindec.

Binary operations in PHP are a bit strange. Since PHP was originally a templating layer for C code, it still has many of those C-isms. Lots of the function names map directly to C-level APIs, even if they work a bit differently sometimes. For example, PHP's strlen maps directly to STRLEN(3), and there are countless examples of this. However, as soon as it comes to dealing with binary data, things suddenly become very different.

He starts off looking at "the C way" to unpack a string (getting the ASCII values of each character) and shows how *not* to do it in PHP with ord. Instead he uses "unpack", bitwise operators and bindec to work with the actual binary data of the string.

tagged: binary parse ord unpack tutorial bindec bitwise

Link:

Dave Marshall's Blog:
Defending against Cache Stampedes
Jan 13, 2012 @ 19:13:08

Dave Marshall has a new post to his blog showing one method for defending against cache stampedes from assaulting your caching servers and (possibly) bringing them down.

I’ve recently had a problem with a rather large operation (that could probably be optimised considerably, but nevermind), where by if the cached result of the operation expired, several web server threads would attempt the operation, causing some major headaches for our database and web servers. This is something I’ve come across before, and is commonly(?) known as a Cache Stampede. This is bad, this post describes the basics of what I’ve done to deal with it.

His example uses a Zend_Cache interface to Memecache, but it could be applied in other places too. His method uses a binary semaphore to check and see if there's a "lock" and sleep for a bit before checking again. Sample code is included showing how to create this system and how to refresh the data with a new expiration on a cache miss.

tagged: cache stampede zendcache tutorial binary semaphore miss prevention

Link:


Trending Topics: