Batch compress image using google guetzli

You can use the following code written by me to run batch compression of images with google’s guetzli image compression tool. It creates a compressed folder and saves all the compressed images of the folder inside the folder. Note: Right now this only compresses image from the location that is given and not the sub-folders.

Usage:

Syntax: base [stcriptname] [relative folder location] [percentage]

Implementation: bash batch-compresssion . 84

#/usr/bin/bash

if [ ! -d "$1/compressed" ]; then
	mkdir "$1/compressed"
fi

for file in $1/*.png
do
  echo $file
  guetzli --quality $2 "$file" "$1/compressed/$file"
done

for file in $1/*.jpg
do
  echo $file
  guetzli --quality $2 "$file" "$1/compressed/$file"
done

for file in $1/*.jpeg
do
  echo $file
  guetzli --quality $2 "$file" "$1/compressed/$file"
done
Advertisements

Adding virtual host to bitnami magento 2 virtual machine

To setup virtual host on the bitnami virtual machine two files will have to be changed. They are htaccess.conf and httpd-vhosts.conf. These files are located at /opt/bitnami/apps/magento/conf/ folder.

First we have to update the htaccess file and add the following lines at the end –

SetEnvIf Host ^m2\.dev MAGE_RUN_CODE=base
SetEnvIf Host ^m2\.dev MAGE_RUN_TYPE=website

Here, the MAGE_RUN_CODE’s value “base” is the code for the default magento stores front-end and “^m2\.dev” is regular expression for setting domain staring with m2.dev

Once the htaccess file is updated. Now, we have to update the httpd-vhosts.conf file and add the m2.dev alias.

<VirtualHost *:80>
  ServerName yourdomain.com
  ServerAlias www.yourdomain.com m2.dev www.m2.dev
  DocumentRoot "/opt/bitnami/apps/magento/htdocs/"
  Include "/opt/bitnami/apps/magento/conf/httpd-app.conf"
</VirtualHost>

Once these two files are updated we have to restart apache server by running the following command –

sudo /opt/bitnami/ctlscript.sh restart apache

Now, we have to login to the admin using IP address one last time and go to stores > configuration > web. We have to update the “Base URL” to http://m2.dev/ and “Secure Base URL” to https://m2.dev/ and click save. After saving the user will be logged out of the system as the m2.dev domain is applied. Login back to the admin panel and now you will see notification to refresh cache. Follow the link and refresh the cache. Now you are ready to use the virtual host with this virtual machine.

Don’t forget to add the virtual host IP in the host computers /etc/hosts file.

192.168.1.108 m2.dev www.m2.dev

To keep the virtual host setup from getting reset follow the following steps.

Additional Task:
Every time the virtual machine is restarted the IP is automatically assigned as the base url of magento. This is done to ensure the application adapt to a changing IP address on each restart. But in our case this becomes a problem as on each restart we have to go and set the virtual host domain again. To stop this from happening we have to rename two files at /opt/bitnami/apps/magento/ the files that needs to be renamed are updateip and bnconfig.

mv /opt/bitnami/apps/magento/updateip /opt/bitnami/apps/magento/updateip.old
mv /opt/bitnami/apps/magento/bnconfig /opt/bitnami/apps/magento/bnconfig.old

Source: http://bit.ly/2qgRjTQ

Faster magento 2 development environment

The biggest impediment that I faced while learning to develop on Magento 2 is to setup a development environment that is not slow as a snail. After a lot of experiments I ended up selecting Bitnami’s virtual machine for Magento 2. It is the fastest of the available solutions that I have found so far.

First you have to download the virtual machine from the Bitnami website. You can download the virtual machine from this page http://bit.ly/2rQrqvo. Before you install the virtual machine you have to have Virtual Box installed in your system. Download Virtual Box from http://virtualbox.org and install before you go on to import the virtual machine. After virtual box is available on your system, double click the downloaded virtual machine appliance (bitnami-magento-2.1.6-2-linux-ubuntu-14.04-x86_64.ova) and follow the instruction on screen to add the virtual machine to your virtual box application.

After the virtual machine is imported, you should see the newly imported virtual machine on the available virtual machines list at the left side-bar. Select the machine and click the start button on top menu to start the machine.

At the beginning its going to take some time to initialize the machine. Once initialization is finished you will be asked to give the ubuntu username and password. For bitnami stack of magento 2 the username is bitnami and the password is also bitnami.

Once logged in you will be show the IP address where the virtual box is running from. For me the address was 192.168.1.108. Enter the IP on any one of you browser and you should see the magento 2 home page. To access admin panel you have to goto http://192.168.1.108/admin with username as user and password as bitnami1.

To enable ssh access use following code to activate ssh config and start ssh server.

sudo mv /etc/init/ssh.conf.back /etc/init/ssh.conf
sudo start ssh

It is possible to use virtual host instead of the IP address. Further detail on that will come in a followup post.

Add-on: Recent debian 8 version of the appliance does not work with this blog post.

To enable ssh on the debian 8 version do –


sudo rm -f /etc/ssh/sshd_not_to_be_run

sudo systemctl enable ssh
sudo systemctl start ssh

htaccess code for redirecting get request from index.php to index.html for temporary landing page

Sometimes for marketing purposes if you have a static landing page index.html and in the background there is another application running via index.php you can redirect all get request to the index.php to be redirected to the index.html using the following code. But if the request is not GET request or if the request has query parameters then it will not apply this rule and take user to the index.php instead.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule index\.php$ http://companydomain.com/index.html [R=301,L]
</IfModule>

Host file updater

Background:

If you are a developer managing multiple virtual hosts on your system you probably have to update the /etc/hosts file quite a lot. This script gives a easy to use way to add and remove domain from the etc file.

Code:

Create a new file named “hostupdater.sh” then paste and save the code.

#!/bin/sh
# This script was developed by faisal ahmed and is provided as is without any warranty.

if grep "$2$" /etc/hosts; then
  if [ "$1" = "del" ]; then
    sed -i "/.*$2$/d" /etc/hosts
  else
    sed -i "s/.*$2$/$1 $2/g" /etc/hosts
  fi
else
  echo $1 $2 >> /home/vagrant/hosts
fi

Usage:

To add a new site type

./hostupdater.sh 192.168.33.12 test1.com

To remove the site type

./hostupdater.sh del test1.com

Setting up wordpress test environment

Step 1 : Getting your virtual environment up and running.

cd ~/Public
mkdir faisal
cd faisal
git clone https://github.com/scotch-io/scotch-box.git .
vagrant up
vagrant ssh

Step 2: Install SVN to get test wordpress codebase from wordpress.

sudo apt-get update
sudo apt-get install subversion

Step 3: Install phpUnit to run your unit tests

curl -O https://phar.phpunit.de/phpunit-4.8.phar
sudo chmod +x phpunit-4.8.phar
sudo mv phpunit-4.8.phar /usr/local/bin/phpunit
phpunit --version

StepĀ 4: Install WP-CLI to setupĀ the test environment

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
sudo chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp-cli
wp-cli --version

Step 5: Setting up the test plugin folder

curl -O https://wordpress.org/latest.tar.gz
tar xvzf latest.tar.gz .
cd wp-content/plugins/
mkdir demo
cd demo
touch demo.php
wp scaffold plugin-tests demo

Step 6: Setting up the test environment

cd ~/Public/faisal
bash bin/install-wp-tests.sh wordpress_test root root localhost latest

Step 6: Check if test is running or not.

cd ~/Public/faisal/wp-content/plugins/demo/
phpunit

You should now see something similar to this –