Tag - php

Fix Blurry Product Thumbnail Images in the Divi Shop Module
Get the local IP within PHP on a Raspberry Pi (Raspbian)
Copy Magento Category

Fix Blurry Product Thumbnail Images in the Divi Shop Module

If you have built an eCommerce store using Woocommerce and Divi, you will probably know how powerful these plugins are and just how rapidly you can build a professional, SEO optimised storefront almost entirely in the browser with a minimal amount of coding.

Having said that, there are few shortcomings in Divi, particularly with regards to getting things right for SEO.

One such issue being the low resolution thumbnail images in the Divi shop module.

Blurry, Low Resolution Images

The product photos in the grid listing of the Divi shop module are rendered at just 150px by 150px byt the WordPress Thumbnail function. This means that they have to be scaled up by the browser. The net result being blurry photos.

This might be okay if you have 5 products per row, but if you decide to change the module to show 3 products per row, then those 150px images have to be scaled up to 320px wide by the browser. I’m sure that you’re already aware that scaling up an image to more than double it’s original size is obviously going to reduce image quality, quite a lot. The end result is blurry product photos. Not good if you want a nice looking website or online store:

Blurry Product Photos


Increasing Image Resolution

To correct this problem, we will have to tell WordPress (or Divi) to output the images at a higher resolution than 150px. After some digging through the Divi code to trace how the image tags are generated, I found a clean way to set image sizes for product images in archive listings, using WordPress filters.

To increase the size of your shop’s product images, add the following code to the functions.php file within your theme:

function theme_setup() {
function theme_size_of_product_thumb($u)
return array(332, 332,true);
add_filter('single_product_archive_thumbnail_size', 'theme_size_of_product_thumb');
add_action( 'after_setup_theme', 'theme_setup' );

After you have added the code, refresh the frontend of your website and the product images should appear sharp:

Sharp High Resolution Product Photos


In the example here, I used 332×332 for the image dimensions as that was sufficient for the 3 items per row I was displaying.

If your images are displayed larger than 332×332, perhaps if you are showing fewer than 3 items per row, then you might want to render your images at a higher resolution.

Bear in mind though that larger images means larger image filesizes, which in turn leads to slower page load times. As long as you don’t go huge, this shouldn’t be too much of an issue.

Get the local IP within PHP on a Raspberry Pi (Raspbian)

I’m currently working on interfacing a Raspberry Pi with an Arduino. The Pi needs to tell the Arduino it’s IP address on the LAN in order for it to be displayed on an LCD screen.
My code running on the Pi is PHP. It’s usually pretty straightforward to get your local IP address from within PHP.
I just make use of the handy $_SERVER array like so:


failed. SERVER_ADDR wasn’t set. Using my initiative, I threw together the following function, which works a treat:

function getIp()
	exec('/sbin/ifconfig', $resultArray);
	$result = implode(",", $resultArray);
	$ip = preg_match('/eth0.*?inet\saddr:([\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3})\s/', $result, $matches);
	return isset($matches[1])?$matches[1]:false;

It basically calls the linux command ‘ifconfig’. The IP address is then extracted from the results.
This should work for any occasion where $_SERVER[‘SERVER_ADDR’] isn’t available, as a fallback.
Feel free to use this in your own projects!

Copy Magento Category

Since first laying eyes on Magento, the open source eCommerce software, I been impressed by it’s power, flexibility and rigid code structure. But despite Magento’s almost endless list of strengths, it also has some significant weaknesses, that often leave me frustrated and confused. One such weakness is the apparent lack of any functionality in regards to copying products and categories.

I had created a category, with about 10 child categories. Now I just wanted to duplicate that category 5 times, rather than creating another 4 from scratch, each with 10 children (4×10=40 categories!). Rather than spend the next couple of hours adding categories, I knocked together this quick PHP script. Simply pass in the ID of the category you’d like to duplicate, and it will create a copy of the category, along with all of it’s child categories, as deep as they go!

Ideally, this would be a proper Magento module, but I’m just starting out with Magento. Give me 6 months, eh?!

To use the script, simply copy it into a file such as copycat.php in your Magento root directory and call it like so:

http://www.example.com/copycat.php?id=[CATEGORY ID TO COPY]

if(!is_numeric($_GET['id']))die('Please specify a category ID');
$catId = $_GET['id'];
$xml = simplexml_load_file('app/etc/local.xml');
$host = $xml->global->resources->default_setup->connection->host;
$username = $xml->global->resources->default_setup->connection->username;
$password = $xml->global->resources->default_setup->connection->password;
$dbname = $xml->global->resources->default_setup->connection->dbname; 
$res = mysql_pconnect($host, $username, $password);   
$catsDone = 0;
echo $catsDone . ' Categories duplicated.';
function duplicate_entity($id, $parent_id = null){
	global $catsDone;
	// Grab category to copy
	$sql = "SELECT * FROM catalog_category_entity WHERE entity_id = " . $id;
	$query_entity = mysql_query($sql);
	$entity = mysql_fetch_object($query_entity);
	if(!$parent_id)$parent_id = $entity->parent_id;
	mysql_query("INSERT INTO catalog_category_entity (entity_type_id, attribute_set_id, parent_id, created_at, updated_at, path, position, level, children_count)
						VALUES ({$entity->entity_type_id}, {$entity->attribute_set_id}, {$parent_id}, NOW(), NOW(), '', {$entity->position}, {$entity->level}, {$entity->children_count})");
	$newEntityId = mysql_insert_id();
	$query = mysql_query("SELECT path FROM catalog_category_entity WHERE entity_id = " . $parent_id);
	$parent = mysql_fetch_object($query);
	$path = $parent->path . '/' . $newEntityId;
	mysql_query("UPDATE catalog_category_entity SET path='". $path."' WHERE entity_id=". $newEntityId);
	foreach(array('datetime', 'decimal', 'int', 'text', 'varchar') as $dataType){
		$sql = "SELECT * FROM catalog_category_entity_".$dataType."
				WHERE entity_id=" . $entity->entity_id;
		$query = mysql_query($sql);
		while ($value = mysql_fetch_object($query)){
			mysql_query("INSERT INTO catalog_category_entity_".$dataType." (entity_type_id, attribute_id, store_id, entity_id, value)
							VALUES ({$value->entity_type_id}, {$value->attribute_id}, {$value->store_id}, {$newEntityId}, '{$value->value}')");
	$sql = "SELECT entity_id FROM catalog_category_entity WHERE parent_id = " . $id;
	$query = mysql_query($sql);
	while ($entity = mysql_fetch_object($query)){
		duplicate_entity($entity->entity_id, $newEntityId);

Copyright © 2018. Created by Hayden Kibble.