Tips and Tricks About Computers, Web Development, Linux, the Internet and the Like
Archive for March, 2009
Batch images conversion – color to transparency from the linux terminal
Mar 31st
I’ve been re-theming and old forum and the themes that are available are a little less than perfect for our needs. I found a few suitable templates but it still results in me creating a mash-up. Some of the images (though gif) set the image background color to that of the site background. As I was using a different color, this obviously looked bad but I wasn’t about to manually edit all the files to give them transparency.
Being aware of the amazing command line tool for Linux, ImageMagick, I set out to find a tool to help me. I found a transparency flag, calculated the RGB values with the eyedropper in GIMP, then after a test run, I through it in a for loop.

So a directory down from my image source, I created a folder called “new” and ran the following color and like magic, they were all converted.
for image in *.gif; do convert -transparent 'RGB(48,71,94)' $image ../new/$image; done
Creating a simple news script that reads from an XML file
Mar 29th
I recently recoded a website for a friend because his markup was pretty ugly (thanks to WYSIWYG editors). While I was doing that, he mentioned he’d like to have an xml file he could edit to update his news. Simple enough I said and banged out the following which includes a little helpful function that wraps paragraphs in HTML p tags. My friend alpha helped me rewrite some ugly regex with a novel idea:
print join '', map {qq[
$_
]} split (/\r?\n/,$object->content)Of course, he’s a big perl coder so I had to create a php equivalent, nls2p.
Edit: Mookow mentioned he wanted to write links, so obviously a system would have to be put in place to support this. Using HTML tags in XML gets a little sloppy so I opted to use bbcode which is a very popular method to format text in web applications and very simple to add more filters.
The news post creator:
<?php
if (file_exists('news.xml')) {
$xml = simplexml_load_file('news.xml');
foreach ($xml->post as $post) {
echo "
<h4>".$post->title." - ".$post->date."</h4>
\n";
echo "
<div>".bbcode(nls2p($post->content))."</div>
\n";
echo "
--".$post->author."
\n";
}
} else {
echo "
Failed to open news source
";
}
The functions for formatting:
// Wrap paragraphs with
tags
function nls2p($str) {
$split_str = preg_split('#[\t]+?[\r\n]{1}#',$str);
foreach ($split_str as $line) {
$new_str = $new_str."
".preg_replace("/[\t\n]/","",$line)."
\n";
}
return $new_str;
}
// Parse bbcode as HTML
function bbcode($string) {
$search = array(
'@\[(?i)b\](.*?)\[/(?i)b\]@si',
'@\[(?i)i\](.*?)\[/(?i)i\]@si',
'@\[(?i)u\](.*?)\[/(?i)u\]@si',
'@\[(?i)img\](.*?)\[/(?i)img\]@si',
'@\[(?i)url=(.*?)\](.*?)\[/(?i)url\]@si',
'@\[(?i)code\](.*?)\[/(?i)code\]@si'
);
$replace = array(
'<b>\\1</b>',
'<i>\\1</i>',
'<u>\\1</u>',
'<img src="\\1">',
'<a href="\\1" target="_blank">\\2</a>',
'<code>\\1</code>'
);
return preg_replace($search , $replace, $string);
}
?>
The XML data file:
<?xml version='1.0' standalone='yes'?> <news> <post> <date>03-29-09</date> <author>Moo</author> <content>The site has been completely recoded by a friend of mine, -z- from NexuizNinjaz.com. He even added this fancy news script. Now, to update the news, I don't even have to touch index.php! Alright, I'm out for now. OMG A [b]NEW LINE[/b]! Now with [i]italics[/i] and [u]underlines[/u] and [url=http://www.nexuizninjaz.com]links[/url] [code]code blocks[/code] and images [img]http://www.xepic.net/pics/pic_091622001182920068.jpg[/img] </content> </post> <post> <date>03-29-09</date> <author>Moo</author> <content>Yeah, I removed the stupid gray background, because after a while it got boring. I changed the "Random Shiznit" column into something that could be a bit more productive. Once all of the other sites are up, I'll update the main page with the latest news from those. This main news column will be for the more intriguing news. I also decided to add titles to the news articles...Duuhhhhhhh OMG A NEW LINE! and again, LOL! </content> </post> <post> <date>03-25-09</date> <author>Moo</author> <content>I added a boring gray background to the main page, as you can see. The hover text-changing effect was removed because of its stupidity. A cool guy named Fabzor re-colored the header for me.</content> </post> <post> <date>03-23-09</date> <author>Moo</author> <content>Website updated, not much. Cool hover effects were added, but we still don't have much color in the page. Hopefully, I can find a good background and get this show on the road.</content> </post> <post> <date>03-22-09</date> <author>Moo</author> <content>Most of the links up top will not work for the time being, this site is just getting edited. Soon, we'll have a better webpage than this one, but for now, deal with this one!</content> </post> </news>
That’s pretty much it verbatim, including mookow’s news ^_^. Hope this is helpful to someone.
Yahoo! hosting is total crap, seriously, don't use it.
Mar 20th
I’ve used many hosts in my time on the internet, using all sorts of backends handed off to me by clients or given to me by friends. Not all of them have been great, limited by their weak control panels and low settings… but none and I mean NONE have been as bad as Yahoo hosting. This is the third time I’ve had a problem with them and it’s not over simple mistakes, it’s about HORRIBLE coding and BAD decisions. My first issue with them was a O.o and a half. While clicking around their backend that looks like it was designed in the early 90′s, I seemingly BROKE the backend. I locked myself out, only to always get an error page when trying to get back in. I had to call support to verify that I had “found a bug” and it took them 3-4 days to resolve. You have to write some pretty bad code to make this happen.
After they fixed that bug and I was able to login again, I tried to start off on my original goal of setting up a MySQL database for use by a web application. Didn’t get to far there! I don’t remember this part too in depth, nor do I care to fact check the details in fear I may break this frail control panel but this innovative solution included using an iframe that passed your credentials to what seemed to be a stripped down version of phpMyAdmin that failed along the way… I tried it again and ended up in failtown twice. I can’t delete the databases because some information was lost along the way and now I’m stuck with two folders I can’t delete either.
Want SSL? HAHAHAHA, HAHAHAHA, Ha! If you want to use SSL, you can drop files in a folder they give you… HOW CONVENIENT!
Hmm, well I guess this actually brings me to my 4th issue with them. The popular method used for doing all sorts of nifty tricks with apache without having access to the httpd.conf, including URL rewrites/redirects (which I was using it for), the .htaccess file is not useable on Yahoo hosting. Yahoo’s faq says, o hai guys, here’s some info on htaccess but YOU CAN’T USE IT!. Now… one of the intents of my recent upgrades was to reduce the number of pages on the website (a legacy issue I was slowly working to resolve). The solution to streamline this process and avoid a bunch of 404s / bad links on search engines is to create rewrites/redirects to point users to the new pages based on the old URLs. With only 5 lines, I managed to cover all pages locally… but I can’t upload the .htaccess file to yahoo.
The solution? Recreate every old page and use a 0 second redirect…. soo…. that means I’d technically be increasing the number of files on the site by ~120% or build xml sitemaps and take the hit for a few weeks while the search engines re0index everything. Pardon my French but that’s some bullshit.
So just a warning, DON’T USE Yahoo hosting if you want to do anything more than host a few html files.
I much happier with Dreamhost, who is cheap, effective, scalable and uses a MUCH better (custom) control panel than any other host I’ve had. I can even use SSH without jumping through hoops our using a stupid autogenerated login name (sup 1and1?).