PHP Wordpress

WordPress: Black and White Post Overlay Image

One of my client ask us to create Black and white thumbnail for wordpress blog post that on mouse over turn coloured. This is pretty common effect now a days and with my custom PHP or ASP.NET programming we usually create black and white image at time of upload and save it. This is what we need to do in WordPress. To save time in research [or rather I found this good article] Where author try to give a good way of creating thumbnail.

At first I was impressed and feel lucky to find a good looking tutorial/article and thought it will be piece of cake now, however as I progress I found that main article miss few things

1. It doesn’t work. Yes the article in its current state didn’t work.

2. It do not filter image if it is of same size as size you need on thumbnail [as define by one of commenter].

3. It fail to create Black and white image

4. It fail to get Black and White image in HTML piece of code.

Starting from #1, ofcourse I get it to working now. So that will you will see later. I follow the commenter’s code to fix the point 2,and frankly I personnally don’t know if it is any good or not. But check looks good in current state so I keep it. Now the problem is it doesn’t create Black and white image for some user and for me. After searching for couple of minutes I found that it does create black and white image, but at different location than the code suggest.

Author is picking the wp_upload_dir() function which return the latest upload path, however in wordpress all upload goes in folder hirerchy as YYYY/MM (Year/month). And if you are uploading in older post then WordPress will upload media for that post in month and year of that post’s publish date and not current year and date. What this does is that it broke the path of black and white image upload and hence imagefilter() php function start giving error. Solution was rather simple. I use $meta’s original upload file path, strip file name and put image path in same folder as original image.

The Next problem is that on HTML page it doesn’t pick the black and white image. Again the code forgot or miss to change the Thumbnail-bw path to it’s new file. Some commenter did mention it but I didn’t get their point until at the time of writting of this article. These two crucial things once fixed the code works just fine for me. So, nothing gets change except the BW_Filter function which I have given below. Rest of code is same as original post. [link above]

function bw_images_filter($meta) {	

	$file = wp_upload_dir();
	// Need to check to see if their was even a generated 'thumb-m' in case the image uploaded was the appropriate size
	// and therefore will be used in it's largest form
	if (isset($meta['sizes']['thumbnail-bw'])) {
		$tmpfile = trailingslashit($file['basedir']);
		$explodedPath = explode("/", $meta['file']);
		unset($explodedPath[count($explodedPath)-1]); //Remove file name from path
		$explodedPath = implode("/", $explodedPath);
		$file = $tmpfile.trailingslashit($explodedPath).$meta['sizes']['thumbnail-bw']['file'];
		$updateFileName = explode(".",$meta['sizes']['thumbnail-bw']['file']);
		$ext = $updateFileName[count($updateFileName)-1];
		unset ($updateFileName[count($updateFileName)-1]); //remove it for now.
		$updateFileName = implode(".", $updateFileName);
		$updateFileName = $updateFileName ."-bw.". $ext;
		$meta['sizes']['thumbnail-bw']['file'] = $updateFileName;
	// The thumbnail didn't get created because the uploaded image was the correct size before scaling / cropping.
	// Need to get base directory instead of path here because the original file already has the year/month structure in it's file attribute.
	} else {
		$file = trailingslashit($file['basedir']).$meta['file'];
		$explodedPath = explode("/", $meta['file']);
		// Manually insert the thumbnail-bw that didn't get created upon upload and append '-bw' to the filename
		$explodedFileName = explode(".", $exploded[2]);
		$updatedFileName = $explodedFileName[0] . '-bw.' . $explodedFileName[1];
		$meta['sizes']['thumbnail-bw']['file'] = $updatedFileName;		
	$meta['sizes']['thumbnail-bw']['width'] = 421; // Set these dimensions to match the dimensions for the add_image_size above
	$meta['sizes']['thumbnail-bw']['height'] = 220;

	list($orig_w, $orig_h, $orig_type) = @getimagesize($file);
	$image = wp_load_image($file);
	imagefilter($image, IMG_FILTER_GRAYSCALE);
	//imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR);
	switch ($orig_type) {
			$file = str_replace(".gif", "-bw.gif", $file);
			imagegif( $image, $file );
			$file = str_replace(".png", "-bw.png", $file);
			imagepng( $image, $file );
			$file = str_replace(".jpg", "-bw.jpg", $file);
			imagejpeg( $image, $file );
	return $meta;

Hope this code works for you. If not please share the fix and solution, I will try to keep it updates as I found better than this.

Bookmarked PHP Programming

PHP Mobile Detection

Just a quick note, to detect the mobile/tablet/desktop this script looks promising : I try to detect it my Windows Mango phone, it does detect it as Mobile, though I hate that it doesn’t recognize Windows Mango yet. But looks promising and it is easier to implement it as well.

Article ASP.NET Bookmarked C#.NET PHP Programming VB.NET/VB

WKHTMLTOPDF: A Free PDF solution

As any other developer we often find it difficult to get a Website to print something accurately, specially when printing labels for mailing as they are outside the default print area for most browser and no client want to adjust or memorize the print setting to adjust them manually. Also it doesn’t make sense to relay on client to do adjustments.

So the only universal solution I found is.. of course PDF files. But how do I create PDF. Well easy enough use some library. I did that I use iTextPDF very good Library that works for my C# project well for couple of years. But then what is the problem now? Well iTextPDF is now have commercial license at ridiculous pricing in fact it is now most expensive to any other paid software. Yet another Open Source project going paid on success. 

Also, there is a problem with library like iTextPDF that they need you to draw each and every element programmatically more often or not. And you find yourself caught in a web of long written code. I remember one of my report’s PDF generation code goes 1000 lines. I hate it. So, the best solution is to Generate PDF from a HTML source. We can handle HTML much better way and rather easily with our skills as web programmer. And even a designer in team can change PDF design giving time for programmer for other work. 

In this regard I look for free alternative of iTEXTPDF, which does have HTML parser to generate PDF from HTML file, and my search for now ends on (wkhtmltopdf ) project. A Simple Executable that I can call from a my code to execute and it does the parsing. 

It is not really a good idea to call it through web page, but we should create a Windows service and use it there, but I have successfully test it to work on web server with quite a load and at least with my limitation to access Windows Service I can live with it running through HTML page, after all my PDF is just 1-2 page and it works fine. Will post code for it some other day. But do try it if you want a good and quick solution to your problem.

Operating System PHP Server Configuration

PHP Upgrade on Plesk/Linux

Well if you search topic on google you will find lot about it. But here is quick guide for me with little explanation.

1. Plesk do not ship PHP as it’s integrated part, so it ask you to upgrade outside plesk and they do not provide support on it.

2. On Linux you can use default APT or YUM to upgrade PHP. In my case it is CentOS/Red hat flavour so this article apply to that mainly. Now when I try to upgrade from PHP 5.1 to PHP5.3 I cannot do that as default CentOS repo shows PHP53 as different package and do not come under default upgrade. And it though gets in conflict with PHP5.1’s current install.

3. I try to Erase existing package and thought to install PHP 5.3 afresh. But it shows a list of 110 dependencies which mainly consist of Plesk’s module. Otherwise it was fairly okay to YUM ERASE PHP. So, I have to opt for this solution which works 100% for where I read about it.

4. Atomic Corp create a script then on Red Hat favour enable 5.1 to 5.3 upgrade (Their WIKI: ) Once you run this SH script you can re-run YUM to do the update. Steps are:

# wget -q -O -
# yum upgrade php
# service httpd restart

Bingo !!! PHP upgraded. No server restart needed.

Operating System PHP Server Configuration

.htaccess Hack

Today, I got another wordpress that stop working and start giving 404 error. Previously client got it fixed by removing .htaccess from his installation and get it working. I thought he might mess his wordpress himself. But when we got similar error today I search and little and find that in .htaccess there is a php injected to wordpress which reside at  “/tmp/25454b22bf39c75795851f39d5e347c4”, after opening the file it looks like professional white script. But knowing computer programmer, only a hacker or idiot can place an important file in /tmp folder. Anyways, I don’t know the cause of hack yet, but I saw following pattern:

1.  Hack is known for wordpress and OsCommerce as of now. [I personnely see only wordpress below 3.0.5 been hacked, rather more specific to 3.0.4 version]

2. Hack need .htaccess file and /tmp folder, so only Linux [can they use windows temp? not know] and surely for Apache user it is an issue.

I cannot say wordpress or oscommerce is broken, but definately there is a control upload script that copy file to /tmp folder which is usually public readable. and then getting .htaccess is problem.

So if you are on Linux/apache and your software does the .htaccess read/write then you need to be beware.


1. Make your .htaccess read only by user and group at most not for public

2. Create a empty non writable file /tmp/25454b22bf39c75795851f39d5e347c4 so in case someone try to copy it fails as file already exists.

3. Change your Passwords

4. Upgrade your software to latest version

5. Do not upload any theme or plugin which is not from known or tested source.

6. Look for vendor recommendation before installing any plugin.

Phew, so far my wordpress is safe, been on windows hosting I am sure the hack will be different if any, lets wait and see.

EDIT:  After few days of writing this article I found few instance where I can safely says that it is not a wordpress or OsCommerce hack, but rather a hack related to either Linux Operating System, or Apache Web server or Plesk Control panel. For sites I see this hack only those three are in common. All those site I saw get hacked in above way are not written by one developer, not belong to one server [except they all use plesk and apache], not using only Mysql [do you really thing mysql query can create ,htaccess?]  So, I give Clean chit to any Open source software as of now from this hack. This is indeed a server hack, Still no reason known to me.

Good luck guys !!!

.NET ASP.NET Blog: My thoughts PHP

Make Web Not War

This is what Microsoft has to say through it’s WEB portal ( ), where it distribute one of leading platform for Web application development and deployment.

Well this message can be a direct/indirect attack on those who thing Microsoft is up again only making $$$ and not services. Well guys you are wrong, check this website out. You will find PHP and hence any software made on PHP works fine on Windows server and that to with easy. I am using PHP on Windows from IIS 5.5 and it always work fine for me, but with IIS 7 and MS WEB it is really getting much easier to install PHP and any application on it.

I like the concept and idea, love to see same thing from Apache when they start support for .NET framework ?

Operating System PHP Server Configuration hosting – WordPress upgrade failure

Today I was upgrading one of my client’s wordpress installation, but it was failing, later I found in some forum that this failure is because hosting doesn’t have PHP enable by default and that we need to map php5 compiler properly. For this all you need to do is just add

AddType x-mapp-php5 .php
AddHandler x-mapp-php5 .php

in your .htaccess and the upgrade will work fine. Also, this is just one of many reason for upgrade failure, and you should perform this after you check all permission and stuff on your application.