Anis Ahmad

December 8, 2014

Sometimes LESS screws an expression inside calc(). To avoid this situation, I do a very basic trick.

Let’s say I want to implement the following to keep a div of 510px width at the center of something.

left: calc(50% - 255px);

Now, the trick is, instead of using calc(), I just use left 50% and add a negative margin to pull it back a half of it’s width (255px, in this case).

margin-left: -255px;
left: 50%;

Dumb and easy, right?
Hope this trick will save someone’s time!

Solving PHP error – Allowed memory size of xxxx bytes exhausted

If you see the following error while executing a php script, that means the execution is trying to use more memory then the maximum amount of memory a script may consume.

PHP Fatal error:  Allowed memory size of 262144 bytes exhausted (tried to allocate xxxx bytes) in /The/Full/file/path/file_name.php on line xx

So, how to fix it? Easy, just increase the allowed memory size. But where to change? There are several points. Let’s say you want to set it to 512MB. Then –

  1. If you want to change it for that script only, modify ini setting at the beginning of file.
    ini_set('memory_limit', '512M');
  2. If you need to change for entire project, add this line in .htaccess of projects root directory (apache only)
    php_value memory_limit 512M
  3. For making the change systemwide, in php.ini, find and set
    memory_limit = 512M

Troubleshooting

Okay. Now, if you see the error is still showing, check the following things –

  • Most common mistake- if you’ve changed php.ini, don’t forget to restart web server
  • Check phpinfo(), if the value you’ve set is not there, maybe your’e changing in wrong php.ini file
  • Check the line you modified in ini file is not commented. (there is no ; at the beginning of line)
  • The value will be 512M not 512MB
  • Check if your script require more memory even after doing the change. (You may set the value to ‘-1′ to make it unlimited. But, think seriously before doing it!)

These are all the mistakes regarding this issue I did and saw people doing. If you’ve experienced something else, please let me know. Thank you.

Building Large Scale Javascript Application

Digital World 2014 was a 4 day (4th – 7th June, 2014) ICT event at Bangladesh. It was organized by ICT Division of Telecom and IT Ministry of the country. There was a full series of technical sessions with various interesting topics throughout the event.

One of them was Building Large Scale Javascript Application. It was presented by me together with Emran Hasan, CTO of eMicrograph. Here is the slide that I prepared in very short time.


Continue reading

Converting Doctrine MongoDB Document toJSON or toArray

This is a common need to convert Doctrine ODM Documents to simpler format. Generally we need to convert them to Array or serialized as JSON. And there are many ways to do it. So, here is my way, my simple Trait that simply works.

By useing this trait you’ll get 2 methods to convert your Document to Array or JSON. It will take care of embedded Documents too.

<?php
// Assuming in a Symfony2 Controller
// If you're not, then make your DocmentManager as you want
$dm = $this->get('doctrine_mongodb')->getManager();
$report = $dm->getRepository('YourCoreBundle:Report')->find($id);

// Will return simple PHP array
$docArray = $report->toArray();

// Will return JSON string
$docJSON = $report->toJSON();

How to use?

3 simple steps.

Continue reading

PHP HTTP extension not working?

The version 2.x.x of PHP HTTP extension is totally different from it’s version 1.x. And it’s not backward compatible at all. So, if you have updated the http extension or installed updated version, you may see it’s not working in previously working source.

So, what to do now? The important thing to note is, http://php.net/http has the documentation for older version (version 1.x.x). Here are 2 links that can help you to quickly start the using new version or to fix your old code.

Happy coding!

Backbone.Marionette + RequireJS + Bootstrap boilerplate

Few days ago, I was searching around for a boilerplate setup for Backbone.Marionette with RequireJS. I was looking for something that is ready for use instantly without any prerequisite or additional dependency. I found few good solutions. But unfortunately none of them seems to be as simple and straightforward as I was expecting.

marionette is ready

So, I had to write my own boilerplate.
Continue reading

Rounded thumb image with CSS 3

In modern Mobile apps, RIA or fancy websites like to display thumb images with a little rounded corners. We can do it on web with very little effort using a little CSS3 technique.

Rounded thumbnail

Just 3 simple 3 steps:

  1. Instead of using img, use a placeholder like a or span. Make it display:block.
  2. Place the image as background image of this placeholder.
  3. Set a border radius and a little shadow for the placeholder.

Sample CSS and HTML code:

<style type="text/css">
	a.user-image {
		background: transparent no-repeat top left;
		display: block;
		text-indent: -999em;
		width: 50px;
		height: 50px;
		border-radius: 5px;
                box-shadow: 0px 0px 3px #666;
	}
</style>

<a class="user-image" href="http://www.facebook.com/ajaxray" style="background-image: url(http://graph.facebook.com/667896332/picture)" title="Visit Anis's profile">Anis Ahmad</a>

<!--
Post: http://ajaxray.com/blog/rounded-thumb-image-with-css-3
Demo: http://ajaxray.com/Examples/cropped-thumb.html
-->

See code on Gist.

You’re done! Check the live demo.

Kohana 3.x Log Viewer module

Kohana Logs are a good way of debugging and monitoring application flow. It has various types of log writers for storing and showing logs. But the most commonly used one is the File Writer. One painful thing with this file logs is, downloading, reading and clearing when testing on a remote server.  To reduce this pain, I wrote a module to explore the log files on server with a beautiful interface. It may help many of you who are developing with Kohana.

You can get it from github –  https://github.com/ajaxray/Kohana-Log-Viewer

Kohana Log Viewer screenshot

Continue reading

The first Kohana book : Kohana 3.0 Beginner’s Guide

Kohana 3.0 Beginner’s Guide Cover page

Kohana 3.0 Beginner’s Guide

Codeigniter was my first PHP framework. Though it works nicely for wide range of app types, slowly I felt uncomfortable in some points. For example, lack of OOP practices, untestable structure etc. (Sorry to say, unit-testing in codeigniter was a horror!) Later, among PHP frameworks, I worked a lot with Zend Framework and a little with Symfony . One day Emran Hasan shared IBuildings article about HMVC which presented Kohana as example HMVC framework. That was the starting, in a few days Kohana took the position of primary PHP framework for me. I built website, Enterprise application, Service layer for SOA and few more apps with Kohana comfortably.

But when framework newbies asks me with which one he should start, need to think twice before suggesting Kohana. The main reason is, it’s documentation was not as rich as Codeigniter or Zend Framework. And there was no book. BTW, Kohana unofficial wiki is a BIG try to help in this issue. Besides, recently Packt has published the first book on Kohana “Kohana 3.0 Beginner’s Guide“. Jason D. Straughan wrote this book for Kohana version 3.x. Few days ago, Snehman Kohli from Packtpub has contacted me and requested for reviewing this book. As a fan of Kohana, I’ve happily accepted. So, here is my view on this book.

Continue reading