Tuesday, May 19, 2009

Find and replace in text files recursive using grep and sed

I've created a small shell script for searching and replacing text in multiple files in a subdirectory.
My reason was that I needed a tool to change the ip-address of several mysql-servers in several files named config.ini, all residing in subdirectories.
The syntax is:
$ ./myfile.sh old-ip new-ip

And here is the script:


#!/bin/sh
for fl in `find ./* | grep config.ini$`;
do
(
echo $fl
sed --in-place "s/$1/$2/g" $fl
)
done


The reason for me doing this is that I'm using Amazon EC2. For a fast connection, I need to use the EC2 internal IP. When I for some reason need to change the MySQL server to be called, that internal ip is changed, and hence I need to change my config files.

Wednesday, October 22, 2008

PHP: imagecopyresampled results in part black images

When using imagecopyresampled parts of the destination images was black. I solved this by using a script found on PHP.net that increases the memory limit for PHP, since large source images requires large amount of memory.

I did some modifications though, since I wanted the script to work for jpeg, gif and png, and I also returned the original memory limit for the script so that it would be easy to restore the memory limits after the image processing.

Anyway, here's the script:

/**
* Increases the memory limit for PHP so that images can be loaded
*
* @param string $filename - full path to the image file to be used
* @return int - old memory limit so that you can restore the memory limit after image processing
*/
function _memorySize($filename){
$imageInfo = getimagesize($filename);

if(strpos($filename,'.jpg')===false){
$imageInfo['bits']=24;
$imageInfo['channels']=16;
}
$MB = Pow(1024,2); // number of bytes in 1M
$K64 = Pow(2,16); // number of bytes in 64K
$TWEAKFACTOR = 1.8; // Or whatever works for you
$memoryNeeded = round( ( $imageInfo[0] * $imageInfo[1]
* $imageInfo['bits']
* $imageInfo['channels'] / 8
+ $K64
) * $TWEAKFACTOR
);
$memoryHave = memory_get_usage();
$memoryLimitMB = (integer) ini_get('memory_limit');
$memoryLimit = $memoryLimitMB * $MB;
if ( function_exists('memory_get_usage')
&& (($memoryHave + $memoryNeeded) < $memoryLimit)
) {
$newLimit = $memoryLimitMB + ceil( ( $memoryHave
+ $memoryNeeded
- $memoryLimit
) / $MB
);
ini_set( 'memory_limit', $newLimit . 'M' );
return $memoryLimitMB;
}
return false;
}

Setting margin, padding and designMode enable a javascript created iframe in Firefox

It was a little bit tricky to designMode enable and change the style of a javascript created iframe in firefox.

Anyway, the trick seems to be to wait a little with a setTimeout before setting designMode='on' and changing the style of the javascript created iframe.

My successfull code looks like this [Firefox only, not IE]:


function addEditableField(parent, iframeId){
// Create the iframe, give it an id and attach it to an existing dom-element
var newIFrame = document.createElement('iframe');
newIFrame.id = 'iframeId';
document.getElementById(parent).appendChild(newIFrame);
// Wait a little for the iframe document to be prepared before doing stuff to it
setTimeout("enable('"+iframeId+"')",20);
}

function enable(id){
// Now some time has passed, so now we can play with the iframe:
document.getElementById(id).contentWindow.document.designMode='on';
document.getElementById(id).contentWindow.document.body.style.margin = '3px';
document.getElementById(id).contentWindow.document.body.style.padding = '3px';
}

addEditableField('parentDiv','testField');


And that's it. (Btw, sorry for the long title, but I prefer to know what I'll get when I go somewhere using google).

Tuesday, September 30, 2008

All your graphs are belong to us - Google Visualization



I don't know if I've lived under a stone or something, but I missed the release of Google Visualization. It's a great javascript library for making increadible graphs of all types of the same type as google use on Google Analytics.

I tried it yesterday and the results where amazing. I replaced some old gif graphs with Google graphs, and suddenly I had beautiful, interactive graphs created on the fly!

I know I've said it before - I really, really like Google, and tools like these are one of many many reasons.

Sunday, September 28, 2008

Easy to use Javascript Image Cropper UI with Prototype.js



I just found this great javascript library that is an user interface for cropping images. It depends on both prototype.js and scriptaculous, which I like since I'm using them on all my projects.
The code is easy to use, and I integrated it into my Zend Framework with a couple of hours work (most of the work was actually not related to the cropping ui, but with the actual file handling and image cropping).

Anyway, it's great, big thanks to David Spurr! Use it, love it, tip it!

Monday, January 21, 2008

Readwriteweb - Tips for startups on Software Engineering

ReadWriteWeb has a pretty nice story on 36 tips for web startups, from Software Engineering to PR and more :)

I agree on what they say completely on the software engineering team. In short their tips are:
1) You must have code
2) You must have a technical co-founder
3) Hire A+ engineers who love coding
4) Keep the engineering team small and do not outsource
5) Ask tough questions during the interview
6) Avoid hiring non-technical managers
7) Cultivate an agile culture
8) Do not re-invent the wheel

I think that the second point is the most important point, you need to have industry knowledge in your founding team (just as you should have people in your team that know the target audience)

Read the full post

Web2.0 Startup - What should you use?

This is a short post on what libraries, languages and such that you should use for your web2.0 startup.

1) PHP
PHP is the most used scripting language used by millions of sites. It's proven scalable, easy to learn and every programmer knows. There are also loads of free and ready to use libraries for PHP.
2) MySQL
MySQL is free, it's fast and used by companies as Google and Facebook. Let's face it, if it's good enough for Facebook, it's probably good enough for you.
3) Apache
Apache is the worlds most used server and you find loads of information about it all over the web. Again, it's proven scalable, stable, and most of all, it's FREE!
4) Zend Framework
Zend Framework is a platform for creating MVC (Model/View/Controller) code, mainly developed by a huge user community, IBM and Zend. Using this you get a framework for PHP for free, making it easier to debug and maintain. It also contains many great built in features for many tasks, making PHP a great alternative to platforms as .Net.
5) Use Prototype.js
All sites contain lots of javascript nowadays. Javascript is a pretty nasty language, with different API's for different browsers (read Firefox and Internet Explorer). It also lacks some functionality for many modern websites such as Ajax. Prototype.js enhances javascript making it platform independent and easier to use. There is no need to reinvent the wheel, use this library that has proven a great solution for many sites, it is also pretty well documentet and has a large user community supporting it.
6) Use Scriptaculous
For your modern, Web2.0 website you need animations and transitions. Use Scriptaculous since it meets many of your website's interface needs.

That's it. Use this and you will have easily maintained, scalable code ready for your rapid development!

Apache
PHP
MySQL
Zend Framework
Prototype.js
Scriptaculous