Categories
Code Snippets PHP

How to get current directory, filename and code line number in PHP

Categories
Code Snippets General PHP Wordpress

[] operator not supported for strings – PHP

I had a problem with Revolution Slider on a WordPress installation when upgrading to PHP 7.2+. It would generate the error – PHP Fatal error: [] operator not supported for strings in C:[path]\wp-content\plugins\revslider\includes\framework\base-admin.class.php:71

The problem was from the plugin attempting to use the short array push syntax on a string:

$box = array();
$box['title'] = $title;
$box['location'] = $location;
$box['content'] = $content;
$box['draw_function'] = $customDrawFunction;
self::$arrMetaBoxes[] = $box;

The solution was to modify the last line as such:

self::$arrMetaBoxes = $box;

No explosions, so all is well!

Why was the operator not supported?

A discussion on Stack Overflow gave me a nudge in the right direction. The suggestion was that PHP 7 has problems with empty-index array push syntax.

These are ok and work properly in PHP 7+.

$undeclared_variable_name[] = 'value'; // creates an array and adds one entry

$emptyArray = []; // creates an array
$emptyArray[] = 'value'; // pushes in an entry

An attempt to use empty index push on any variable declared as a string, number, object, etc, doesn’t works however. ie

$declaredAsString = '';
$declaredAsString[] = 'value';

$declaredAsNumber = 1;
$declaredAsNumber[] = 'value';

$declaredAsObject = new stdclass();
$declaredAsObject[] = 'value';

Theses examples all fail with a PHP error, which in my case was “[] operator not supported for strings”

For more PHP related posts, check here

Categories
Javascript PHP

Display TIFF Files on a Web Site in Chrome and Firefox

If you need to display TIFF files on the web, you might have problems. TIFF files used to be displayed by all browsers, but as they are not really suitable for web use, support has been dropping. See Image Support by Browser:  https://en.wikipedia.org/wiki/Comparison_of_web_browsers#Image_format_support

This makes sense of course, if you are only trying to use the TIFF files as images as part of your layout or content. TIFF files are often much larger than what you want if you are trying to have a decent page speed score and good user experience. This is why TIFF support on the web is being dropped and Chrome and Firefox, of the major browsers no longer support the format. In fact only Safari and Microsoft Edge do.

Display TIFF Files on the Web

But what if you have a legitimate need for displaying this format? I recently created a file management application where the requirement was to be able to quickly upload an image and have it displayed in their gallery for download. Many of the images were for print materials and  therefore in the TIFF format, so they wouldn’t display on Chrome or Firefox. Instead they would open a download window upon loading the page.

We could have had a field for the main image and then a thumbnail, but the requirement was a streamlined process where they didn’t even want to title or caption the images – they just named the files intuitively for the listings. Generating a converted thumbnail upon upload wasn’t an option as the hosting environment was restrictive and didn’t have the ImageMagick extension for PHP enabled. They were unwilling to change hosts.

Are you having trouble paying attention this long? Maybe you need an ADHD test! Fine, I’ll get to the point.

After a lot of digging, I ended up finding UTIF.js which was created for the Photopea online photo editor. This library processes your TIFF files and allows you to display them in HTML. This was very easy to install and use. I had to modify the functionality a little bit as there was a delay in having all of the images loaded. Also some new images would be loaded via AJAX as the user navigates the folder tree That made the body onload implementation was unsuitable. Beyond that it was quick and easy to get going.

Display TIFF Files on a Website
This is a TIFF file

You can use TIFF images directly inside the <img> element and then, you just have to to call UTIF.replaceIMG() once at some point. Eg:

<body onload=”UTIF.replaceIMG()”>

<img src=”image.tif” /> <img src=”dog.tif” /> …

Other Options to Display TIFF files in Chrome and Firefox

Categories
Code Snippets PHP Wordpress

WordPress Custom Sortable Columns in Post/Page/CPT Listings

Categories
PHP Visual Studio Wordpress

WordPress IDE – Visual Studio Code

I have decided to test out another WordPress IDE – Visual Studio Code

*(or should I do PHP Tools for Visual Studio first?)

Step 1: Install the Visual Studio Code IDE from: https://code.visualstudio.com/

References:
Autocomplete and snippetrs: https://marketplace.visualstudio.com/items?itemName=tungvn.wordpress-snippet
Review with recommended plugins, etc: https://tommcfarlin.com/vs-code-wordpress/

https://code.visualstudio.com/docs/languages/php