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-6691464f67e3b300748119/] and for older version of PHP using deprecated function mime_content_type we can rewrite the above code as Using mime_content_type [crayon-6691464f67e40914335752/] 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-6691464f67e42696368954/] 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 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 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-6691464f68780860938430/] 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-6691464f68784749189266/] The Grid [crayon-6691464f68785207384055/] 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-6691464f68786121279291/] 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-6691464f68787447197882/] 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-6691464f68788441635203/] Above code will refresh our store after 3 seconds (once only). This method also give you granular control to stylize individual columns too.   Demo   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-6691464f68ab5364995738/] [crayon-6691464f68ab9667190930/] [crayon-6691464f68aba245073843/] [crayon-6691464f68abb427763931/] At the server side So at the server side (upload.php) just try this [crayon-6691464f68abc779615349/] [crayon-6691464f68abd588251810/] The result of a demo post for me yielded this [crayon-6691464f68abe258014503/] 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-6691464f68abf255849850/] So the above will yield this result [crayon-6691464f68ac0147770402/] 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   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:   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-6691464f68c50652569759/] So your script will look like this [crayon-6691464f68c54205210334/] [crayon-6691464f68c55137525036/] [crayon-6691464f68c57809094915/] [crayon-6691464f68c58623916739/] But as per some reports such as this one The above method may not work. But the one that I use is the one given below Preferred way [crayon-6691464f68c59305966871/] [crayon-6691464f68c5a203354333/] [crayon-6691464f68c5c554503089/] [crayon-6691464f68c5d219085687/] [crayon-6691464f68c5e137765681/] 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-6691464f68c5f114830561/] [crayon-6691464f68c65011080635/] [crayon-6691464f68c67118618723/] [crayon-6691464f68c68845092720/] 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-6691464f68c69437542711/]   Above should print 0 till 9 and with … [Read more...]

PHP API v3 Shorten and expand URLs tutorial

Hello, I thought I should do a write up on this topic not because I like using for various reasons but because 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 account and are ready to roll. Now this post assume that you are going to use 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 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 API key from here Your 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-6691464f68ecd968648713/] Usage [crayon-6691464f68ed3309620977/]   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 You can either use CURL itself to download our certificate like this curl  > /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-6691464f692e7986867942/] Or you can have this as strings [geshi lang="html5" nums="1" target="_self" ] [crayon-6691464f692ed959913403/]   By the way if you are using mySQL as your database then you can download timezones from the link below 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 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 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-6691464f695d2092876778/] or [crayon-6691464f695d6330253097/]     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 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 [wpdm_file id=39] 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: … [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:  CHttpCookie Doc URL: 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-6691464f696ef038484011/] where Yii::app()->request is an instance of CHttpRequest Ok so lets create a cookie A simple cookie can be created as [crayon-6691464f696f2177025857/] as per the documentation the default constructor for CHttpCookie is shown below [crayon-6691464f696f3670682304/]   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-6691464f696f4363690239/] [crayon-6691464f696f5341377286/] How do we read cookie value You can use the code below to read your cookie value. [crayon-6691464f696f6344716535/] 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-6691464f696f7327995815/] 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...]