How to resize product photos in batch (Center, White background with Aspect ratio)?

Today I had to resize large no of photos for an e-commerce website. Maybe it is the same reason why you read this article :).

Images were downloaded from the internet, therefore they were in various sizes varying from 650px to 5000px. Here are the requirements I had when I started this job.

  1. Final image size should be 500 x 500px.
  2. If the original image is larger it should be resized, with respect to the aspect ratio.
  3. If the image width and height differ, the lower side should be filled with white background.
  4. Original image should be in the Center of the Final image.
Most especially I am not a fan of wasting my time on repetitive works so AUTOMATION was the most important requirement:).

I tried many solutions and finally found a convenient method that can address all of my above requirements. It was with the Mogrify tool comes with the ImageMagic package.

This is a Linux tool but it's very simple so please don't run away :).

Installing ImageMagick

I am using Debian derivative OS for this example. If you have any other OS, please use the respective installation command.
sudo apt-get update
sudo apt-get install imagemagick

Processing images

Note: In my situation, all images were in jpg format but if you have images in various formats please convert all of them to jpg before proceeding.

Upload all of your photos to a folder in the Linux box and open that folder from the CLI.
cd /image_folder
mogrify -resize 500x500 -extent 500x500 -gravity center -background white *.jpg
That's it! :). Download your uploaded images and they should be in the required format by now.

You can do much more things with this amazing tool. Please check the official documentation of mogrify to learn more.

Hope this helped!





Magento country wise annual sales report directly from the database

There are many extensions out there which you can use to get country wise sales reports. However, you can retrieve much more things if you be little familiar with SQL queries. You can use below queries to get annual sales figures based on countries. 

*Please note that the order values displayed are the subtotals before taxes.

Detailed Report
SELECT orders.increment_id as id, orders.state, addresses.firstname, addresses.lastname, addresses.country_id, orders.subtotal as subtotal, orders.created_at
FROM sales_flat_order AS orders
JOIN sales_flat_order_address AS addresses
ON addresses.parent_id=orders.entity_id
WHERE relation_child_real_id IS NULL
AND addresses.address_type='shipping'
AND YEAR(orders.created_at) = YEAR(CURDATE())

Summery Report
SELECT addresses.country_id, sum(orders.subtotal) as subtotal
FROM sales_flat_order AS orders
JOIN sales_flat_order_address AS addresses
ON addresses.parent_id=orders.entity_id
WHERE relation_child_real_id IS NULL
AND addresses.address_type='shipping'
AND YEAR(orders.created_at) = YEAR(CURDATE())
GROUP BY addresses.country_id

Hope this helped! 

Create a Bootable USB thumb drive in 1 Minute

Here I share with you the quickest and easiest way to make a Bootable USB thumb drive on a windows computer. For this guide I will use a windows 10 environment.

We will use the built in disk partitioning tool of the Microsoft windows, which is refereed to as DiskPart in short.

Tested on

I have successfully tested this method on following operating systems.
  • Windows XP
  • Windows Vista
  • Windows 7
  • Windows 8
  • Windows 10
  • Windows Server 2003
  • Windows Server 2008
  • Windows Server 2012

Steps

1. Plug your USB drive and note the capacity (This comes handy when selecting the drive).

2. Open diskpart prompt (You can follow either Command prompt or Run box approach for this)

Command prompt
Windows XP:  All Programmes -> Accessories -> Command Prompt
Windows Vista and later: Just search cmd on the start menu and click on the black console icon. 
Type diskpart in the cmd console and press enter to open the diskpart. 

Run box
Press Win + R from your keyboard. Type diskpart on the input box and click OK to open the diskpart.
Open diskpart from run screenshot

3. Once diskpart prompt is open use below command to list all drives.
list disk
Diskpart list disk screenshot

4. then carefully select your USB disk. Yes the only clue is the capacity and that's why we noted the disk capacity in the first step :).
select disk 2
Diskpart select disk screenshot

5. We should wipe out everything on the disk with the "clean" command
clean
Diskpart clean screenshot

6. Let's create a partition
create partition primary
Diskpart create partition primary screenshot

7. and then we need to select the created partition.
select partition 1
Diskpart select partition screenshot

8. once selected we can activate the partition
active
Diskpart active screenshot

9. Now we need to format the partition in NTFS format. Make sure you use the quick mode unless you have some prison break episode with you to watch while formatting happens :).
format fs=ntfs quick
Diskpart format ntfs quick screenshot

10. We just created a bootable USB drive and say goodbye to diskpart
exit
Diskpart exit screenshot

Finally, your pen-drive is ready to copy any of your booting media and carry with you as a portable bootable device, to boot USB boot supported device.

Hope this helped.

How to convert any text to slug with JavaScript?

Probably you are here to find a way to convert your text string in to a slug. Me too wanted the same and could create the following quick and neat function after reading couple of resources.
function makeSlug(String)
    {
        return String
            .toLowerCase()           //Convert the whole string to lowercase
            .replace(/[^\w ]+/g,'')  //Remove all the non-word characters
            .replace(/ +/g,'-')         //Replace white spaces with hyphens
            .replace(/^-/g,'')          //Remove leading hyphens (caused by leading spaces)
            .replace(/-$/g,'')          //Remove trailing hyphens (caused by trailing spaces)
    }

Then you can call the function with the text as your parameter.

var text = "Can you convert me to a slug?"
var slug = makeSlug(text);

And the result will be a hyphen separated, clean, seo friendly slug url.
can-you-convert-me-to-a-slug

Hope this helped :)