How to install Imagick in PHP Docker?

The PHP docker image is build from the source and hence the usual apt-get install php-imagick doesn’t work at all. This might be frustrating for you as most of the solutions have a version attached to it. However, I found a way to always have it updated. Here is how –

Dockerfile for Imagick

This is how docker file should look like:

FROM php:7.3-apache
RUN apt-get update && apt-get install -y libmagickwand-dev --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN printf "\n" | pecl install imagick
RUN docker-php-ext-enable imagick

Build Output


configuration option "php_ini" is not set to php.ini location
You should add "" to php.ini
Removing intermediate container 1c032fba6d45
 ---> 517795f672ac
Step 4/4 : RUN docker-php-ext-enable imagick
 ---> Running in 976ecd4a9627
Removing intermediate container 976ecd4a9627
 ---> 4b978dd49756
Successfully built 4b978dd49756

Git Repositor and Docker

Tell your network!

How to write good “If” statements?

Writing a good code is an art and it can be useful in so trivial things that most developers often ignore. Usually, amateurs are programmed to write an ‘if’ statement which is always accompanied by an ‘else’ . However, it is important to realize that code quality can significantly be improved if else is omitted. For instance, consider this code.

function getUser($id){
  if($id < 1){
   $user = [];
   //some big codes
  return $user

Here some big codes can be so big that it will return statement will be lost in it. Better would be:

function getUser($id){
  if($id < 1){
   return [];
  //some big codes
  return $user

Tell your network!

How to write the high-performance application in PHP?

We coded Product Kits app and it worked pretty well. Peak hits were 5000 hits per seconds. Read Story of Product Kits from 100 to 100000 hits per minute. We had a lot of issues, but issues with PHP wasn’t scalability. We were able to handle everything but the problem came when we had to know what other workers were doing.

The first question that might come to your mind is if you have chosen the write tech stack? Since we are aware of the fact that PHP doesn’t support multi-threading. However, there is a trade-off between the speed of development and the performance. PHP is not very fast, in fact, it is slow – but it is fast enough. As long as you don’t want individual PHP scripts to know the state of each other, you are in a pretty good shape – most of the time.

Trade-Off – Scalable VS Speed

While using PHP, our major concern was RAM, it was much easier to get high RAM usage and CPU. We had to deal with a lot of data and data most of the time either stays in RAM or required us to increase the HIT if we wanted to keep it outside. If your PHP codes are using a lot of RAM, you will have to solve a scalability problem. However, if your app doesn’t require a lot of RAM, better is to optimize it for speed.

Writing the Right Codes:

  1. Rely on always running PHP codes – If a worker is written in PHP, tie the worker in an infinite loop which will wait for an event (A Queue, MySQL entry) instead of invoking PHP every second or so.
  2. Cache sooner – Although there are a couple of cache options in PHP – OPcache and Memcache. However, Redis is favourite which can further help you scaling by having multiple master or other topology. Combination of opcache and redis will be best.
  3. Load fewer classes – Ensure that you are not loading a lot of classes, rely on dynamic loading. This will increase the speed and reduce the memory.
  4. Keep over-writing variables – This is a pretty bad practice but it ensures that your memory is limited.
  5. Make smaller blocks – A heavy code or multiple functional calls under the loop are your sworn enemy. It is better to write multiple loops few smaller blocks than to have one large block.
  6. Use JSON instead of XML – JSON is a new standard and takes lesser memory.
  7. Use classes – Obvious but – having functions inside class will make it less memory hogger as long as you are loading classes when needed.

Micro Optimization of your codes:

These optimizations are not something you should do after the development as it has a very little effect. However, right from the beginning, a good practice is to follow it.

  1. Promote ‘static’ – This alone can increase the execution speed by 3X.
  2. Use single quotes – ‘ – As long as there is variable inside.
  3. Use str_replace instead of preg_replace_all
  4. Use ‘===’ instead of ‘==’
  5. Use ‘isset’ instead of count/size

Tell your network!