Building a php based image to data url (base64) function tool for img src attribute

Hi Guys, There are times when you would want to save HTTP requests to your servers. One thing you can do is to "include" your image in the html file itself. Sure it will make your HTML file a bit heavier but the client won't be hitting your server for that extra call to load the image. Other reason why you would want to include your image as base64 within the HTML file itself is, just if you want to protect the folder structure on your website (people won't know where your images are stored). You can also use a technique called CSS sprites which you can read more about from this brief tutorial on w3schools. This will result in loading a single image just once and most browsers will cache it, thereon you can use CSS to manipulate your image positioning. Here is what I would use with PHP 5.3 Using fileinfo [crayon-61afad109305a019259620/] and for older version of PHP using deprecated function mime_content_type we can rewrite the above code as Using mime_content_type [crayon-61afad1093062725373754/] fileinfo functions/class methods are based on magic mime database. sometime you will get an error where it fails to load the magic database. fileinfo extension need to be enabled to use fileinfo functions. Make sure that you have it enabled as shown below. (I am on windows box thats why you see .dll extension) [crayon-61afad1093064248061992/] I hope that this little tip could benefit you. If you see any problem with listed code above please leave your comments. Also you can ask questions regarding what's going on above. If I can't answer then there must be someone who can. Cheers,   … [Read more...]

plupload and RackSpace cloud files

Hi Guys, There are a few uploaders that one can like. Mine favourite is plupload and there are heaps of reason behind that, but this post is not about discussing how good plupload is rather we will concentrate on how we can use plupload to upload files to your server and then send a signal to your server to upload that file to RackSpace cloud files. This post assume that average file size to be uploaded is not more than few 100 kilobytes else a bit more sophisticated error handling will be required so that files are reliably sent to RackSpace Cloud. You should know we will need to upload a file to the server hosting plupload and then upload it to RackSpace cloud. Doing it this way does have advantages as it offers greater flexibility. Here is an activity diagram of what we are going to do. This is just a basic activity diagram explaining core steps only. You can have better error handling. Above is just a skeleton activity diagram. In the code provided the upload failure to RS Cloud is not implemented and I leave it to you to do that. Lets get started. Create a folder pltest under your htdocs or html folder. This is the location where your web server find files to return to the user when queried. On linux path could be /var/www/html and on windows path could be something like this depending on where you've installed Apache. C:\Apache2\htdocs Download plupload First step is to download plupload and this can be done by visiting their website and follow the download instructions. Here is the link http://www.plupload.com/ Extract downloaded plupload in pltest folder. Getting Cloud Files PHP API Next we will need to download Cloud Files PHP API which you can find here https://github.com/rackspace/php-cloudfiles Extract PHP files in archive to to our pltest folder. Your folder structure should look like this now. Also make sure that you extract share folder under pltest. Sorry that is not part of the screenshot … [Read more...]

ExtJS 4 changing Grid row background color

Hello, ExtJS's grid system is really powerful module that for me is backbone of their framework. In this post I am going to show you a couple of methods you can use to change background colors of some random Grid rows. First up is to define couple of CSS classes which we will make use of later in our code. The CSS [geshi lang="css" nums="1" target="_self" ] [crayon-61afad10950b8042208573/] Now that we have our CSS classes to play with and apply to our Grid rows, let me take you through a full example from creating a new simple Grid to applying styles. The Markup [crayon-61afad10950be201581709/] The Grid [crayon-61afad10950c0657937027/] We our Grid is ready and properly rendered on our page. From here we can do few things. If we already know which rows to highlight that would be easy to do by using viewConfig property as shown below [crayon-61afad10950c2302289915/] Probably its a good thing that you override .x-grid-cell class definition and remove background-color property from it. In the above code I am just putting my custom classes for odd and even rows. But you can see that I have access to record object, thus I can apply conditions based on the data on hand. This is OK but what if you grid data is changed every 10 seconds and you are not sure which row to highlight with color red for example. I will do that sort of handling on store.load event and will do something like this [crayon-61afad10950c3014865631/] We are doing a check on every store reload and changing the background color according to the new data. So just to simulate consider adding this code to test it [crayon-61afad10950c4880084079/] Above code will refresh our store after 3 seconds (once only). This method also give you granular control to stylize individual columns too.   Demo http://jaspreetchahal.org/examples/extjs-grid-row-background-color.html   This is just my working solution. If you can come up … [Read more...]

Upload multiple files using php curl library

Hi Guys, This one took a while for me to figure out. Thing is that no matter what I did it won't upload a file. In other words php curl script on Server A wouldn't send file contents to my upload.php script locate on server B. As we all know that we have to set our form encoding type to enctype="multipart/form-data" so that the file contents are sent with the post. That's typical HTML way of doing things but we sometimes need to write a script that will do this from command line. Well this is exactly what we will look at now Here is a quick PHP snippet to upload a single file form Server A to Server B The PHP Here is a snippet to upload single file using curl library in php [crayon-61afad1096720532143714/] [crayon-61afad1096728166499121/] [crayon-61afad1096729684491605/] [crayon-61afad109672a277285898/] At the server side So at the server side (upload.php) just try this [crayon-61afad109672b633847594/] [crayon-61afad109672c178403369/] The result of a demo post for me yielded this [crayon-61afad109672d082659643/] Now we know how to upload a single file. What about multiple files in one go Nothing fancy just usual stuff. Add another File in the upload array as shown below     [crayon-61afad109672e510821033/] So the above will yield this result [crayon-61afad109672f735001640/] Ok so this turn out to be quite simple. Note that we are using sign (@)  with file path. This is the trick with sending files to destination script. Read section 4.3 in this document http://curl.haxx.se/docs/httpscripting.html   I hope this helps. If you see any error in the code above please let me know so that I can fix it. PHP curl library reference: http://www.php.net/manual/en/book.curl.php   Cheers … [Read more...]

php force echo output to browser while page is still loading

Hello, This post is very brief and a quick tip. In this age where Ajax has taken over as preferred method for background tasks there are many a time that we still have heavy ended loops that holds the page load till they finish their execution. Well! normal scenarios is when we do reports stuff. In this post I would like to share a PHP way that works. First of the method is to use [crayon-61afad10969eb771160076/] So your script will look like this [crayon-61afad10969ef446111200/] [crayon-61afad10969f0852194468/] [crayon-61afad10969f1792880580/] [crayon-61afad10969f2033628544/] But as per some reports such as this one https://bugs.php.net/bug.php?id=23877 The above method may not work. But the one that I use is the one given below Preferred way [crayon-61afad10969f3577958315/] [crayon-61afad10969f4801728920/] [crayon-61afad10969f5050426714/] [crayon-61afad10969f6054108304/] [crayon-61afad10969f7166025595/] As you can see that I've called ob_start() as the first line of above example. ob_start() will enable the output buffering for our page. Then you see that I am using function ob_end_flush() and flush() after the echo function. This means that whatever is in the current output buffer, flush it. Thus whatever is printed will appear on your browser. Excercise Copy paste this code into a new file called test.php and save it to the root of your test website's root folder and then run it from browser to see if it works. [crayon-61afad10969f8634054112/] [crayon-61afad10969f9498570738/] [crayon-61afad10969fa924653061/] [crayon-61afad10969fb314392614/] Lot of people would have loads of problems with this stuff. Its your webserver that has to be set correctly and even then the code above may not work. If the above does not work then its work add the header to the start of the script as shown below [crayon-61afad10969fc473694101/]   Above should print 0 till 9 and with … [Read more...]

PHP Bit.ly API v3 Shorten and expand URLs tutorial

Hello, I thought I should do a write up on this topic not because I like using bit.ly for various reasons but because bit.ly is a platform that you will end up using one day hopefully. That day for you is today by the way that is one reason you are reading this article. Ok so lets not waste time to make this article lengthy and I don't want you to loose interest half way through. Lets do it step by step. I assume that you've already created your bit.ly account and are ready to roll. Now this post assume that you are going to use bit.ly for your personal website and are not shortening URLs on behalf of your clients, that is totally different topic and I will write about it later. Ok first up goto http://dev.bitly.com and click on Get started button. Wow! that step was easy. Don't stress the next steps are even easier. The dev home page talk about few things and we are just going to concentrate on shortening URLs for our site Grab your bit.ly API key from here http://bitly.com/a/your_api_key Your bit.ly username and API key are shown on this page as shown below Now that you got hold of your username and API key lets jump straight into the PHP part and see how we can shorten the URLs easily.   The simple bitly PHP class [crayon-61afad1097fc6072499055/] Usage [crayon-61afad1097fd0034988408/]   ALright so that's it. Now one more thing. Once you create a short URL you can always see it from your account and get important stats such as number of times its clicked etc as shown below   I hope that you enjoyed this post, just in case you would like to add more to the above mentioned stuff go ahead and post your comments Cheers. … [Read more...]

error setting certificate verify locations: CAfile while using tmhOAuth

Hello, While I was working with twitter API and using one of the known PHP Twitter API wrapper called tmhOAuth, written by Matt Harris I fumble upon this error. error setting certificate verify locations: CAfile What the? I did some reading and then I realized that my cacert.pem was not copied to the desired folder. So I copied the downloaded file from the Repo and re-ran my twitter API tests. Again same error happened. Now I was pulling my hair apart at this time. Then I realized that it could be possible that my .pem is outdated. Thus I downloaded my cacert.pem file from here http://curl.haxx.se/ca/cacert.pem You can either use CURL itself to download our certificate like this curl http://curl.haxx.se/ca/cacert.pem  > /myloc/cacert.pem Or you can also right click on the above link and save it somewhere. You have to keep up with this the latest .pem file for just in case situations. I hope that this helps. I was able to resolve my errors by updating my cacert.pem file Cheers,       … [Read more...]

PHP Timezones select box

Hey guys, Just in case you need a list with timezones with GMT values, here is what I've compiled from various places. I hope that it will be helpful. Just in case if I missed putting a timezone up please leave your comment and I'll update this list.   [crayon-61afad1099778304756174/] Or you can have this as strings [geshi lang="html5" nums="1" target="_self" ] [crayon-61afad1099780594181943/]   By the way if you are using mySQL as your database then you can download timezones from the link below http://dev.mysql.com/downloads/timezones.html If you operating system is either of these then it does have zoneinfo available Linux FreeBSD Sun Solaris Read this article to import zoneinfo into mysql tables http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html I hope this helps Cheers … [Read more...]

Nusphere PHPed 7.0 dark theme

Hello, While looking around on the nusphere website I couldn't find any useful pointer to where I can just go and download a theme that is easy on my eyes. But then I found this page http://donatstudios.com/PhpED-Dark-Theme Now when I downloaded the theme and try to install it on my PHPed it didn't work so I thought may be I should edit it as per what I preferred because I already had a launching pad, thanks for guys at doantstudios. Here is a screenshot of my editor appearance settings Let me go through what you will need to do to install this theme. I could have written a quick setup.exe too but I remember my friend saying that its not always a good idea to put everything in a installer. But let me know I can do that to make it easier for you to install it. Ok now lets go through the install steps one by one Depending on your operating system go to either [crayon-61afad1099b65064797194/] or [crayon-61afad1099b69615338692/]     Once you are in do this Close NuSphere PHPed 7.0 Make copy of file hl.cfg   --- very important so that you can restore your old theme just in case if the new one doesn't work for you or you did not like it. Now download the hl.zip below Extract the archive somewhere. Copy h1.cfg over existing file thus overwrite the existing file. If windows ask you to overwrite the file, you must click yes Download Donations This is important bit. I generally fund my own stuff only except my Tea and Coke :). So if you are interested donating a penny for my work. Consider donations! I don't get many because generally people skip this part and go directly to downloads :) but its always worth asking. Every penny is guaranteed to put up for nice work ;)   Key details Here are some key details for the theme Font family:  Consolas (Monospace) Font Size: 12pt Background: rgb(0,0,0) Default Text color: rgb(255,255,255) Line highlight: rgb(42,42,42) String: … [Read more...]

Yii Framework handling cookies

Hello, In the past we have used setcookie() function and $_COOKIE global array to create and access cookies for our domain that expires in N number of days. If you are using Yii framework, it comes with couple of handy classes that you can manage your cookies utilizing Yii poweress. These two classes are called CCookieCollection Doc URL: http://www.yiiframework.com/doc/api/1.1/CCookieCollection  CHttpCookie Doc URL: http://www.yiiframework.com/doc/api/1.1/CHttpCookie CHttpCookie is responsible for creating a cookie. An instance stores a single cookie that includes cookie name cookie value domain it is valid for path expiry date indication if cookie is on secure channel CCookieCollection is a collection of cookies Generally you will get all the cookies set by you as [crayon-61afad1099d95125619132/] where Yii::app()->request is an instance of CHttpRequest Ok so lets create a cookie A simple cookie can be created as [crayon-61afad1099d9a093966390/] as per the documentation the default constructor for CHttpCookie is shown below [crayon-61afad1099d9c608136838/]   Where options can be domain, expire, path, httpOnly, secure In our example above we are setting our cookie to expire after 365 days and to send this newly created cookie to our cookie collection we will do the following Send cookie to cookie collection [crayon-61afad1099d9d104689199/] [crayon-61afad1099d9f240485230/] How do we read cookie value You can use the code below to read your cookie value. [crayon-61afad1099da1227333226/] Now $value will have value 'my_cookie_value' How to remove a cookie Its really as simple as this. The code below will remove the cookie altogether. [crayon-61afad1099da2579225070/] There are a few handy methods in CCookieCollection such as add(), remove() etc that you can use to add or remove cookies if you prefer to use class functions. Idea of this post was to … [Read more...]