Posts tagged with “php”
Copyright Year Smarty Plugin
I've been seeing a lot of people handling the copyright year quite wrong, even though it is a very simple matter, one that should always be automated since it requires an yearly maintenance otherwise. The copyright year should be handled as it follows:
- display the current year only if the website/blog was created this year
- display the creation year and the current year if the website/blog was created one or more years ago
Pretty simple and a lot easier to automatize than to always remember to change the year when it actually needs changing. And since it is somewhere between the back-end and the front-end I think Smarty (or you templating engine of choice) is the way to go for this one and that is why I built a tiny but quite useful Smarty plugin to do this for you.
You just have to provide the "company name" (or your name, or your projects name, you got the point) and the year that the project was created and it handles the rest. Pretty straight-forward.
Example 1:
{copyright company="George Maicovschi" created="2009"}
will output
All content copyright © 2009 George Maicovschi
Example 2:
{copyright company="George Maicovschi" created="2006"}
will output
All content copyright © 2006-2009 George Maicovschi
In order to use the code just copy and paste it in a file name function.copyright.php that is located in Smarty's plugins directory.
<?php
function smarty_function_copyright($params,&$smarty)
{
//The `company` parameter cannot be empty. Check for it;
if (empty($params['company']))
{
$smarty->_trigger_fatal_error("[copyright] param `company` missing");
return;
}
//The `created` parameter cannot be empty. Check for it;
if (empty($params['created']))
{
$smarty->_trigger_fatal_error("[copyright] param `created` missing");
return;
}
$current_year=date('Y',time());
return "All content copyright © ".($current_year>$params['created']?$params['created'].'-'.$current_year:$current_year)." {$params['company']}";
}
?>
Enjoy!
03:32 PM | 2 Comments | Tags: smarty, php, copyright, year, development, pluginWhen Nice Becomes Nicer
As I was saying yesterday, niceURL is an evolving project. It launched yesterday with nothing more than a page where you would enter the long and ugly URL and get a nice and short one, perfect for sharing on microbloging sites (read Twitter) and other places where keeping it short is a must.
But that was niceURL v0.1 ...
A day has passed and niceURL has grown [ it's v0.2 right now :-) ], becoming even nicer and a lot more useful. And it's pretty easy to see that I'm right when I say this: just head over to the website and look just beneath the huge box where you enter the URL you want to get shortened. You'll see a pink select box. Well, using that select box you can generate short URLs using not just niceURL but other services as well (TinyURL, Bit.Ly and Is.Gd at the moment). And when you are selecting one of the two domains that niceURL is available on (http://nurl.me and http://niceurl.me) you will also be able to create a niceURL with a custom slug, like http://nurl.me/nice which goes to the initial niceURL post here on my blog.
Another nice addition was developed with other developers in mind and that is an API. It was an expected step with a project like this and it helps both the 3rd party developer have access to a URL shortening service and niceURL grow. A win-win situation.
Having said this I urge you to keep an eye open for even more features coming to the project. Really soon.
11:32 PM | 0 Comments | Tags: php, niceurl, url, bit.ly, tinyurl, projects, developmentMaking It Short and Nice: niceURL.me
Before I even start talking about niceURL I want to say that YES, I know there are a lot of URL shortening services around. And yes, I know that at first sight there is no point in using it! But please, read on... and at the end you might think the exact opposite!
Having said that, we can move on to the juicy part.
Yes. This is a project I've been developing. And yes, at the moment it is quite primitive. As it just takes a long and/or ugly URL and turns it into a short and much more appealing one. But that's it at the moment. Hmm...wait! Have you looked at the badge next to the logo? No? Than take a look. Do it now, it's right here. I think you got the point!
Now...why would anyone build an URL shortener instead of using one of the many already available? Simple. Because niceURL is not just an URL shortener, at least not for long. Version 0.2 is coming a lot sooner than you think and it brings some much desired functionality. Functionality that you can't find on any other URL shortener and that every power-user wants. And when I say power-user I don't refer to a maniac that wants to shorten URLs all day long. I'm talking about someone that uses a microbloging platform quite often and that wants to be able to post links that are the shortest and look the best. And wants to do it fast and easy!
With all the respect for TinyURL, SnipURL, Bit.ly and many other huge URL shorteners, I hate using them. Their UI and UX suck. TinyURL looks and SnipURL and Bit.ly manage to flood the main page with a lot of non-sense. I don't get why SnipURL calls "Vital stats" the number of URLs they have shortened! That number won't save my life and I can easily live without knowing it. I would be more than happy if they moved it to a separate page if they really feel the need for an ego-boost.
With all this in mind and while, i admit, looking at the Google homepage I realized that is what I missed when it came to URL shorteners! A page with just a logo and a box to "drop" my long and nasty URL in. That's it. That is how niceURL's interface was born. And that is the principle I promise I'll support throughout this whole project.
All in all, today niceURL is a good-looking, super-accessible. But it might be a lot more in a matter of days. You'll just have to keep an eye open!
Oh, almost forgot. One really nice advantage of niceURL is that it makes a plain 301 redirect to your original URL so you will not be losing all that link juice you are getting through the sites where you post the shortened URL! ;-)
12:07 AM | 0 Comments | Tags: php, projects, development, vision, user experience, design, tinyurl, bit.ly, url, twitterTitle Case Smarty Plugin
I've been spending quite some time lately documenting for some coming and ongoing projects and there is a common mistake I see in a vast majority of the websites. And that is missing a proper formatting of the article's title, or, as it is described in English grammar books, the use of title case
Basically tile case is an English grammar standard for writing titles for articles, books, newspaper columns, etc. And the rules are not that easy to follow: all the words in a title must begin with a capital letter except articles, prepositions and conjunctions if they are not the first word of the title.Pretty simple, right?
Well, even if it's a pretty basic thing to do a lot of us tend to forget about it. And then we go on rambling about W3C standards but forgetting some of the more basic, communication related ones. That is why I decided to create a little PHP function that would do just that, format the title so it would fit the title case standard. But after a few seconds I realized that this is actually related more to the front-end and thus shouldn't stay in the back-end of an webapp/blog/whatever so that's when making it a smarty plugin became natural. And since Smarty is written in PHP (d'oh!) you can always just copy-paste the function and use it straight from PHP.
Aside from the classical requirements I also implemented some other modifiers to make it a little more useful and adaptable for special cases:
- strict - [ boolean ] - when set to false it allows you to have upper case letters inside the words. The default value is TRUE
- forceLower - [ string ] - can contain a comma separated list of words that you want to force to be lower case for the current instance only
But enough with the talk. Lets see some usage examples!
Example 1 - standard usage
{titlecase title='snow white and the seven dwarfs'}
This example will output: Snow White and the Seven Dwarfs
Example 2 - turning `strict` off
{titlecase title='snow whIte and the seven dwarfs'}
This will output: Snow White and the Seven Dwarfs
but is used like
{titlecase title='snow whIte and the seven dwarfs' strict=false}
it will output: Snow WhIte and the Seven Dwarfs
Example 3 - using `forceLower`
{titlecase title='snow White and the seven dwarfs' forceLower='white,dwarfs'}
In this case the output will be: Snow white and the Seven dwarfs
I hope that these examples clarify how the hole plugin works so it's time for you to get your code :-). In order to use the code just copy and paste it in a file name function.titlecase.php that is located in Smarty's plugins directory.
<?php
function smarty_function_titlecase($params,&$smarty)
{
//The `title` parameter cannot be empty. Trigger a fatal error if it is
if (empty($params['title']))
{
$smarty->_trigger_fatal_error("[titlecase] param `title` missing");
return;
}
//Check if we are running in strict more or not
if (!isset($params['strict'])) $params['strict']=true;
//Add words that you want to always be lower-case only to this array
$force_lower=array('about','above','across','after','against','around','at','before','behind','below',
'beneath','beside','besides','between','beyond','by','down','during','except','for','from','in','inside',
'into','like','near','of','off','on','out','outside','over','since','through','throughout','till','to',
'toward','under','until','up','upon','with','without','according to','because of','by way of',
'in addition to','in front of','in place of','in spite of','instead of','on account of','out of','a',
'an','the','and','or','but');
//Check if there are any instance-only forced lower case words
if (!empty($params['forceLower'])) $force_lower=array_merge($force_lower,explode(',',$params['forceLower']));
//Build the regex patterns for whole-word matching based on the words forced to lower case
foreach ($force_lower as $word) $regex["/(\b({$word})\b)/i"]=$word;
//And finally rebuild the string in title case
return ucfirst(preg_replace(array_keys($regex),array_values($regex),ucwords(($params['strict']?strtolower($params['title']):$params['title']))));
}
?>
Enjoy!
02:43 AM | 0 Comments | Tags: php, smarty, title case, english, language, standards, plugin, developmentGenerating an Image's Color Scheme Using Imagick
UPDATE:It seems that the TinyMCE environment I used to edit this post stripped the <pre> I used to wrap the code. Everything should be fine now though!
When I got the idea behing TwitColors the first question I asked myself was "What's the best way to get the colors out of an image?". And since it was the main component for the whole application and it analyzed about 60 full-size images it also had to be fast, since this was only the beginning of the whole analytic process that is behind TwitColors.
This being said I almost instantly eliminated (or `hated` to be more exact) the idea of iterating through all the image's pixels and finding out their color and then using a math formula to "merge" the colors. It could be done but it would take too much time that the app doesn't have. After forgeting about this approach I remembered seeing an interesting function in the Imagick library. That function is Imagick::quantizeImage(). And it does exactly what I neede it to do: it "cuts" the number of colors inside an image to the number you give it. And I wanted 10.
Without any more hustle I'm going to present you with the code I used to extract 10 unique colors from an image:
$im=new Imagick($image);
$im->quantizeImage( 10, Imagick::COLORSPACE_RGB, 0, false, false );
$im->uniqueImageColors();
$it=$im->getPixelIterator();
foreach ($it as $row=>$pixels)
foreach ($pixels as $column=>$pixel)
{
$rgba=$pixel->getColor();
}
Basically everything it does is to load the image as a new Imagick object, reduce the number of colors in the image to 10 RGB colors (using the Imagick::quantizeImage() function I was talking about earlier), delete all unrequired pixels from the image keeping only one pixel for any unique color (that is what Imagick::uniqueImageColors() does). Up until this point the full-size image has been converted to a tiny image that is 10px wide and 1px high. Now everything that remains to be done is to get the colors of those pixels in a PHP readable format - that is either and HEX value or an RGB array. This is also done with Imagick by creating a new PixelIterator object using the Imagick::getPixelIterator() function and then using a simple loop to go through the matrix of pixels and getting their color using the ImagickPixel::getColor() function that returns an RGBA (red, green, blue, alpha) array of the color.
That's it. Now you can easily get the color scheme of any image without needing any specialized software and straight from your PHP scripts.
03:09 PM | 0 Comments | Tags: php, imagick, projects, twitcolors, color, scheme, backend