Posted in: data,php | ( 5 ) Comments
21 Sep 2010To set the stage we will start with a programming problem, this will keep the discussion anchored to a relevant context. The problem is this : We want to write a function that takes a image file as an argument and tells us whether the file is a GIF image; irrelevant with whatever the extension the file may have. We are not to use any GD library functions.
With the requirement that we are not allowed to use any graphics functions, to solve the problem we need to get the relevant data from the GIF file itself. Unlike a HTML or XML or other text format files, a GIF file and most other image formats are stored in a binary format. Most binary files carry a header at the top of the file which provides the meta information regarding the particular file. We can use this information to find out the type of the file and other things, such as height an width in case of a GIF file. A typical raw GIF header is shown below, using a hex editor such as WinHex.
Posted in: data,html,php | ( 7 ) Comments
17 Sep 2010A little quirk of PHP $_POST var I encountered while fixing a Salesforce web-to-Lead bug. A WordPress site was using a form to submit user requests to the Salesforce web-service. The form that submitted the data had the following fields along with the others. The problem was with the multi-select field, only the last value selected in the multi-select was getting captured.
Posted in: php,tip | ( 1 ) Comment
15 Sep 2010Frequently when you are displaying images on a page using a fixed width and height , the images come out stretched or squeezed. This is because the aspect ratio of the images have not been maintained. In such a case you can use the following script to correctly display the images according to the aspect-ratio of the image without actually resizing the image in a editor. This can be handy when you need to quickly correct image distortions. Here you need to keep either the width or the height fixed, so the other dimension can be calculated. Below the width is kept fixed at 200px, and the height is varied depending on the aspect ratio of the image, thus displaying the image without any distortion.
<?php $imageFile = 'path/to/image'; /* Set the width fixed at 200px; */ $width = 200; /* Get the image info */ $info = getimagesize($imageFile); /* Calculate aspect ratio by dividing height by width */ $aspectRatio = $info[1] / $info[0]; /* Keep the width fix at 100px, but change the height according to the aspect ratio */ $newHeight = (int)($aspectRatio * $width) . "px"; /* Use the 'newHeight' in the CSS height property below. */ $width .= "px"; echo "<img style=\"width: $width; height: $newHeight;\" src=\"$imageFile\" />"; |
Posted in: testing | ( 14 ) Comments
12 Sep 2010Posted in: mysql | ( 7 ) Comments
10 Sep 2010Auto-increment sequences are a common way to define a primary key in MySQL. This types of artificial keys, known as surrogate keys are commonly used by developers to quickly construct a database table. A common reason developers use artificial keys is due to the fact that most do not take the time to search for a natural key in their model, especially SQL newbies. The question of whether we should use a surrogate key or a natural key is a debate we should leave to the database experts. The purpose of this post is to present some queries to find if a auto-increment sequence contains gaps.
Posted in: html,tools | ( 6 ) Comments
8 Sep 2010It has been a while since I’ve used a template engine during development, the last one I used was Smarty. Now there are a plethora of template systems, but most are a rehash of Smarty. Readers may beg to differ, but Smarty gets the work done, which is all that matters. The one that I found really interesting is Haml.
Posted in: html | Comments Off
29 Aug 2010With HTML5 quickly making inroads in the web world, information relating to the workings of the same are on the increase. One resource I find quite useful and thorough is the one by Google – html5rocks. Filled with various tutorials and a playground to work with HTML5 code, the site makes a quite useful destination for those learning the new HTML 5 standard.
Posted in: mysql | Comments Off
18 Aug 2010
One of the most common used attributes in MySQL is definitely AUTO_INCREMENT. This is quite helpful when one needs to generate unique identities for the table rows. By default when a AUTO_INCREMENT column is the only column in a index, whether PRIMARY KEY or UNIQUE, it generates a single monotonic sequence of numbers : 1,2,3,4,… etc. But for MyISAM storage engine it is possible to create complex sequences in a table containing an AUTO_INCREMENT column.
Posted in: libraries,php | ( 1 ) Comment
12 Aug 2010Usually after completing a long project I find that I’ve created various extraneous files in the project directory; like zip files or maybe a few big graphic files or some huge MySQL dumps. If the directory sizes are small I can manually delete those unwanted files. But if the directory sizes are big or if they are nested deeply, than it can be quite time consuming. Maybe you left a couple of huge MySQL dumps somewhere and forgot to delete them, thus increasing the project file size. And if you are trying to do the cleanup on a online server then it can be even more painful.
Or maybe you are just curious to find how various types of files are taking up your directory space.
Whatever the reason, below is a small php script that displays the distribution of files in a particular directory and its sub-directories by its type. This can be handy if you would like to see which files are taking up space in your project. Although you can easily do such kind of things with a variety of desktop tools, the following code can easily be used online, or integrated into your existing php application.