<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-142925038823267790</id><updated>2012-02-16T02:54:11.590-08:00</updated><category term='php image crop'/><category term='object oriented programming'/><category term='image manipulation'/><category term='image resize'/><category term='design patterns'/><category term='MySQL'/><category term='zip codes'/><category term='watermark'/><category term='php'/><category term='calculate distance using zip codes'/><category term='longitude and latitude distance calculator'/><category term='drupal'/><category term='modify drupal theme'/><category term='date_format'/><category term='singleton design pattern'/><category term='png watermark'/><category term='gd2'/><category term='distance calculator'/><category term='drupal theme engine'/><category term='resize funtion'/><category term='calculate age'/><category term='php center image'/><category term='factory design pattern'/><title type='text'>PHP How To Articles</title><subtitle type='html'>See here small scripts for different problems ... all explained!</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://php-help.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/142925038823267790/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://php-help.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Adi</name><uri>http://www.blogger.com/profile/16262430025050344583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>9</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-142925038823267790.post-5401411720914268630</id><published>2012-01-23T09:03:00.000-08:00</published><updated>2012-01-23T09:05:14.706-08:00</updated><title type='text'>Drupal xss weakness</title><content type='html'>A major flaw in drupal form api is the missing of a real filtering&lt;br&gt;module. The current filter of drupal runs only at the display of field&lt;br&gt;content, but not when the field data is saved in the database.&lt;br&gt;This places in high risk the site admins, that have full access, so&lt;br&gt;the bad scripts are showing when editing a form, for example a&lt;br&gt;comment. There is no problem with that, but if the user has a ckeditor&lt;br&gt;or tinymce active, then it is a risk that the mallitious scripts to be&lt;br&gt;executed by the web browser, putting in danger the user by sending&lt;br&gt;private information on mallitious sites.&lt;br&gt;To solve the problem, i&amp;#39;ve made a module that filters all xss from all&lt;br&gt;formes, but be carefull because you might have scripts in one or more&lt;br&gt;of your nodes, for example google adsense or google map. Those scripts&lt;br&gt;will disapear if the form is not listed in exceptions.&lt;br&gt;You can download the module from here!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/142925038823267790-5401411720914268630?l=php-help.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://php-help.blogspot.com/feeds/5401411720914268630/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://php-help.blogspot.com/2012/01/drupal-xss-weakness.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/142925038823267790/posts/default/5401411720914268630'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/142925038823267790/posts/default/5401411720914268630'/><link rel='alternate' type='text/html' href='http://php-help.blogspot.com/2012/01/drupal-xss-weakness.html' title='Drupal xss weakness'/><author><name>Adi</name><uri>http://www.blogger.com/profile/16262430025050344583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-142925038823267790.post-7788019225011787233</id><published>2012-01-22T08:05:00.000-08:00</published><updated>2012-01-22T08:08:03.587-08:00</updated><title type='text'>Making javascript uc_first function</title><content type='html'>&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/142925038823267790-7788019225011787233?l=php-help.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://php-help.blogspot.com/feeds/7788019225011787233/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://php-help.blogspot.com/2012/01/making-javascript-ucfirst-function.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/142925038823267790/posts/default/7788019225011787233'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/142925038823267790/posts/default/7788019225011787233'/><link rel='alternate' type='text/html' href='http://php-help.blogspot.com/2012/01/making-javascript-ucfirst-function.html' title='Making javascript uc_first function'/><author><name>Adi</name><uri>http://www.blogger.com/profile/16262430025050344583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-142925038823267790.post-5328133871302220740</id><published>2012-01-22T08:02:00.000-08:00</published><updated>2012-01-22T08:04:29.242-08:00</updated><title type='text'>Solve the drupal 7 problem of css and js aggregation</title><content type='html'>Drupal 7 has reached a mature state in it&amp;#39;s development so more and&lt;br&gt;more sites are upgrading to drupal 7.&lt;br&gt;But this does not mean it is flawless. The other days i installed&lt;br&gt;drupal on a website and when i enabled css and js aggregation ...&lt;br&gt;surprize ... all the style and javascript were no more.&lt;br&gt;The reason was that while drupal tried to aggregate my files, it&lt;br&gt;couldn&amp;#39;t save them to the publuc://css location. Seems like while on&lt;br&gt;some servers public:// works, on others does not so insted of trying&lt;br&gt;to use realpath, drupal returns an error in our log:&lt;br&gt;&amp;quot;The file temp://xxx cannot be copied to public://css/xxx.css&amp;quot;&lt;p&gt;And no stylesheet/js apears on your site.&lt;br&gt;I didn&amp;#39;t have time to investigate the reason why this happens, but&lt;br&gt;there is a fix, and this means to edit the includes/file.inc, and in&lt;br&gt;the last if statement of function file_copy write&lt;br&gt;$real_source instead of $source&lt;br&gt;and&lt;br&gt;$real_destination instead of $destination&lt;p&gt;or just put another if in the old one so instead of giving that error,&lt;br&gt;try to copy with real paths instead.&lt;p&gt;Have fun!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/142925038823267790-5328133871302220740?l=php-help.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://php-help.blogspot.com/feeds/5328133871302220740/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://php-help.blogspot.com/2012/01/solve-drupal-7-problem-of-css-and-js.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/142925038823267790/posts/default/5328133871302220740'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/142925038823267790/posts/default/5328133871302220740'/><link rel='alternate' type='text/html' href='http://php-help.blogspot.com/2012/01/solve-drupal-7-problem-of-css-and-js.html' title='Solve the drupal 7 problem of css and js aggregation'/><author><name>Adi</name><uri>http://www.blogger.com/profile/16262430025050344583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-142925038823267790.post-8599053922820956930</id><published>2012-01-13T10:11:00.001-08:00</published><updated>2012-01-13T11:53:51.572-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='modify drupal theme'/><category scheme='http://www.blogger.com/atom/ns#' term='drupal'/><category scheme='http://www.blogger.com/atom/ns#' term='drupal theme engine'/><title type='text'>How to programatically change a template file in drupal 6</title><content type='html'>When building modules in drupal, you often would like to change the template file of a specific hook, for example page.tpl.php. &lt;br /&gt;Drupal does not have a special function to do this, but you can play with these 2 functiones:&lt;br /&gt;&lt;br /&gt;&lt;i&gt;init_theme()&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;and&lt;br /&gt;&lt;br /&gt;&lt;i&gt;theme_get_registry().&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;For this to work you need to put a code into your menu callback function.&lt;br /&gt;First, we need to build the theme registry:&lt;br /&gt;&lt;br /&gt;&lt;i&gt;init_theme();&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;Now, you can get the theme registry in an array to modify it, like this:&lt;br /&gt;&lt;br /&gt;&lt;i&gt;$registry = theme_get_registry();&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;So in registry we have an array that we can modify in any way we like. For example, the array&lt;br /&gt;&lt;br /&gt;&lt;i&gt;$registry['page']&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;keeps the template name and the location for page.tpl.php.&lt;br /&gt;&lt;br /&gt;If we change the location or anything else in this array now, it will take effect only in our custom module, it will not be recorded in the cache.&lt;br /&gt;&lt;br /&gt;After we modify what we want here, we save the array by running&lt;br /&gt;&lt;br /&gt;&lt;i&gt;theme_get_registry($registry);&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;That's all, we now have a different template for that hook, that will take effect only on this page.&lt;/b&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/142925038823267790-8599053922820956930?l=php-help.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://php-help.blogspot.com/feeds/8599053922820956930/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://php-help.blogspot.com/2012/01/how-to-programatically-change-template.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/142925038823267790/posts/default/8599053922820956930'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/142925038823267790/posts/default/8599053922820956930'/><link rel='alternate' type='text/html' href='http://php-help.blogspot.com/2012/01/how-to-programatically-change-template.html' title='How to programatically change a template file in drupal 6'/><author><name>Adi</name><uri>http://www.blogger.com/profile/16262430025050344583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-142925038823267790.post-8140194994491864212</id><published>2011-07-20T01:03:00.000-07:00</published><updated>2011-07-20T01:25:18.260-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='factory design pattern'/><category scheme='http://www.blogger.com/atom/ns#' term='design patterns'/><category scheme='http://www.blogger.com/atom/ns#' term='php'/><category scheme='http://www.blogger.com/atom/ns#' term='singleton design pattern'/><category scheme='http://www.blogger.com/atom/ns#' term='object oriented programming'/><title type='text'>Singleton design pattern in PHP</title><content type='html'>You can find all the information you need on the web if you google singleton design pattern. So I'm not gonna fill your head with all the theory.&lt;br /&gt;&lt;br /&gt;I'll tell you how you can implement it.&lt;br /&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;So, we have a application that lists the products from the database. To do this, we have a class named 'Product'.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;class Product{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public $_data;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; function __construct($id = 0){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if($id != 0)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $this-&amp;gt;loadProduct($id);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; function loadProduct($id){&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if(!$id) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $this-&amp;gt;_data = mysql_fetch_array(&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mysql_query("SELECT * FROM products WHERE id = ".intval($id))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; );&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;This is a basic method of selecting a product from the database.&lt;br /&gt;&lt;br /&gt;Now, if we select the product just once, there's no problem. But if our product appears on more then one part of the site, let's say for meta info, for special products on the right side, instantiating our class for 3 times consumes many resources and time.&lt;br /&gt;&lt;br /&gt;The idea is to keep out instances in a static variable, and to use that variable to return the already selected product. Now the program uses less resources and is faster.&lt;br /&gt;&lt;br /&gt;To do this for our class, we need a static method, called by default (so other programmers know what is about) "getInstance".&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;class Product {&lt;br /&gt;    static $_instances;&lt;br /&gt;    &lt;br /&gt;    public $_data;&lt;br /&gt;    &lt;br /&gt;    private function __construct($id = 0){&lt;br /&gt;        if($id != 0)&lt;br /&gt;            $this-&amp;gt;loadProduct($id);&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    public function loadProduct($id){&lt;br /&gt;        ...&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    public static function getInstance($id){&lt;br /&gt;        if(!array_key_exists($id,self::$_instances)&lt;br /&gt;               || !(self::$_instances[$id] instanceof self) ){&lt;br /&gt;            self::$_instances[$id] = new self($id);&lt;br /&gt;        }&lt;br /&gt;        return self::$_instances[$id];&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So we have a static variable $_instances declared in the class. We do this because we might need that variable to be visible across the class, for other methods like one that checks if that product is already instantiated. This helps if you want to be sure that the product is not listed twice in the site - like once in the product listing page, and twice in the column that shows the special offers.&lt;br /&gt;&lt;br /&gt;I put the constructor private to make sure another instance of the class cannot be made.&lt;br /&gt;&lt;br /&gt;This is a very useful method, but not for all objects. It is useful for the database class (in combination with factory design patters, which I'll explain at another time), for session class (this too is better with factory), for product class, basically for any class that might be used all over the site. It is not very useful at a pagination class for example.&lt;br /&gt;&lt;br /&gt;Have fun&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/142925038823267790-8140194994491864212?l=php-help.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://php-help.blogspot.com/feeds/8140194994491864212/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://php-help.blogspot.com/2011/07/singleton-design-pattern-in-php.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/142925038823267790/posts/default/8140194994491864212'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/142925038823267790/posts/default/8140194994491864212'/><link rel='alternate' type='text/html' href='http://php-help.blogspot.com/2011/07/singleton-design-pattern-in-php.html' title='Singleton design pattern in PHP'/><author><name>Adi</name><uri>http://www.blogger.com/profile/16262430025050344583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-142925038823267790.post-8810702676661392691</id><published>2011-06-23T00:38:00.000-07:00</published><updated>2011-06-23T00:38:00.325-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='resize funtion'/><category scheme='http://www.blogger.com/atom/ns#' term='php center image'/><category scheme='http://www.blogger.com/atom/ns#' term='image resize'/><category scheme='http://www.blogger.com/atom/ns#' term='gd2'/><category scheme='http://www.blogger.com/atom/ns#' term='php image crop'/><category scheme='http://www.blogger.com/atom/ns#' term='php'/><category scheme='http://www.blogger.com/atom/ns#' term='image manipulation'/><title type='text'>How to fit an image in a rectangle area in PHP using GD</title><content type='html'>Have you ever had a fixed space in your design, and you must put an image that fits exactly there, while preserving the aspect ratio? Well, this is what i'll explain in this article.  &lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;Let's say we have this image:  &lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-N-kQ__-zSTk/TgG_WrKy1tI/AAAAAAAAABA/rnOS_C1AIhw/s1600/animal_101.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="240" src="http://3.bp.blogspot.com/-N-kQ__-zSTk/TgG_WrKy1tI/AAAAAAAAABA/rnOS_C1AIhw/s320/animal_101.jpg" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;To fit this image in a 100x100 box while keeping the image aspect ratio, we have 2 options:&lt;br /&gt;The first (and most used) is to resize and crop it.&lt;br /&gt;&lt;br /&gt;If we have a image resource $img :&lt;br /&gt;&lt;blockquote&gt;&lt;pre&gt;$w = imagesx($this-&amp;gt;_img);&lt;br /&gt;$h = imagesy($this-&amp;gt;_img);&lt;br /&gt;&lt;br /&gt;$nw = 100;&lt;br /&gt;$nh = 100;&lt;br /&gt;&lt;br /&gt;$fw = $nw;&lt;br /&gt;$fh = $nh;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;$nh_ = ( $h * $nw )/ $w;&lt;br /&gt;&lt;br /&gt;$fit = true;&lt;br /&gt;if($fit === true ){&lt;br /&gt; if($nh_ &amp;lt; $nh){&lt;br /&gt;  $fh = $nh;&lt;br /&gt;  $fw = ( $w * $nw )/ $h;&lt;br /&gt; }else{&lt;br /&gt;  $fw = $nw;&lt;br /&gt;  $fh = ( $h * $nh )/ $w;&lt;br /&gt; }&lt;br /&gt;}else{&lt;br /&gt; if($nh_ &amp;gt; $nh){&lt;br /&gt;  $fw = ( $w * $nh )/ $h;&lt;br /&gt;  $fh = $nh;&lt;br /&gt; }else{&lt;br /&gt;  $fw = $nw;&lt;br /&gt;  $fh = ( $h * $nw )/ $w;&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;$tmp=imagecreatetruecolor($fw,$fh);&lt;br /&gt;&lt;br /&gt;imagecopyresampled($tmp,$img,0,0,0,0,$fw,$fh,$w,$h);&lt;br /&gt;$img = $tmp;&lt;br /&gt;uset($tmp);&lt;br /&gt;&lt;/pre&gt;&lt;/blockquote&gt;If we have the $fit variable set to true, then ve have this picture resized to a rectangle that can hold a 100x100 px square.  If we set the $fit variable to false, we will have the image that will fit in a 100x100 px square. In both cases, the image is not yet a 100x100 px square. If we take our image, in the first case, is a rectangle with 100px height, in the seccond is a rectangle with 100px width.  Now, to make it 100x100 px, we have to crop it. To do that, we center it, then crop it to the exact size:  &lt;br /&gt;&lt;blockquote&gt;&lt;pre&gt;// we recalculate the image size, because it is different now&lt;br /&gt;$w = imagesx($img);&lt;br /&gt;$h = imagesy($img);&lt;br /&gt;&lt;br /&gt;// we calculate the x and y position of the point that we will use to start cropping&lt;br /&gt;// so that we can fit a 100x100px square&lt;br /&gt;$x = ($w - $nw)/2;&lt;br /&gt;$y = ($h - $nh)/2;&lt;br /&gt;&lt;/pre&gt;&lt;/blockquote&gt;After the calculations are over, we start cropping:   &lt;br /&gt;&lt;blockquote&gt;&lt;pre&gt;// we make a 100 x 100 px image&lt;br /&gt;$tmp = imagecreatetruecolor($nw,$nh);&lt;br /&gt;// we create a color value for the background&lt;br /&gt;$clr = imagecolorallocate($tmp, 255,255,255);&lt;br /&gt;// we fill the image with our background&lt;br /&gt;imagefilledrectangle ($tmp, 0, 0, $nw, $nh, $clr);&lt;br /&gt;&lt;br /&gt;// we resample the image&lt;br /&gt;imagecopyresampled($tmp, $img, 0, 0, $x, $y, $nw, $nh, $nw, $nh);&lt;br /&gt;$img = $tmp;&lt;br /&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;br /&gt;Now $img is an image resource with a 100x100 px image in it, that we can do aditional opperations, or display it.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/142925038823267790-8810702676661392691?l=php-help.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://php-help.blogspot.com/feeds/8810702676661392691/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://php-help.blogspot.com/2011/06/how-to-fit-image-in-rectangle-area-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/142925038823267790/posts/default/8810702676661392691'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/142925038823267790/posts/default/8810702676661392691'/><link rel='alternate' type='text/html' href='http://php-help.blogspot.com/2011/06/how-to-fit-image-in-rectangle-area-in.html' title='How to fit an image in a rectangle area in PHP using GD'/><author><name>Adi</name><uri>http://www.blogger.com/profile/16262430025050344583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-N-kQ__-zSTk/TgG_WrKy1tI/AAAAAAAAABA/rnOS_C1AIhw/s72-c/animal_101.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-142925038823267790.post-8504802616269756163</id><published>2011-06-21T05:14:00.000-07:00</published><updated>2011-06-22T02:54:31.590-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='png watermark'/><category scheme='http://www.blogger.com/atom/ns#' term='gd2'/><category scheme='http://www.blogger.com/atom/ns#' term='php'/><category scheme='http://www.blogger.com/atom/ns#' term='watermark'/><category scheme='http://www.blogger.com/atom/ns#' term='image manipulation'/><title type='text'>Using a transparent PNG image as watermark in PHP</title><content type='html'>Sure there are plenty of scripts on the internet that does this thing ... but very few explain this.&lt;br /&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;Let's use this function:&lt;br /&gt;&lt;blockquote&gt;function watermark($jpgimage, $pngimage){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; $image= imagecreatefromjpg($jpgimage);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; $watermark = imagecreatefrompng($pngimage);&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; $ww = imagesx($watermark);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; $wh = imagesy($watermark);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; $iw = imagesx($image);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; $ih = imagesy($image);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; // watermark position&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; $x = ( $iw - $ww ) / 2;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; $y = ( $ih - $wh ) / 2;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; imagealphablending( $image, true);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; imagecopyresampled($image, $watermark, $x, $y, 0, 0, $ww, $wh, $ww, $wh);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; imagealphablending($image, false);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; imagesavealpha($image, true);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return $image;&lt;br /&gt;}&lt;/blockquote&gt;&lt;br /&gt;This function puts a PNG watermark on a JPG picture while saving the alpha channel, and returns an image resource.&lt;br /&gt;&lt;br /&gt;To do this, we first create an image resource from the jpg image and another one from the png(the watermark).&lt;br /&gt;&lt;blockquote&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;   $image= imagecreatefromjpg($jpgimage);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; $watermark = imagecreatefrompng($pngimage);&lt;/blockquote&gt;After that, we take the images x and y sizes and calculate the watermark position.&lt;br /&gt;&lt;blockquote&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $ww = imagesx($watermark);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; $wh = imagesy($watermark);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; $iw = imagesx($image);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; $ih = imagesy($image);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; // watermark position&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; $x = ( $iw - $ww ) / 2;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; $y = ( $ih - $wh ) / 2;&lt;/blockquote&gt;&lt;br /&gt;Then, we set the blending mode to true and add the watermark:&lt;br /&gt;&lt;blockquote&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; imagealphablending( $image, true);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; imagecopyresampled($image, $watermark, $x, $y, 0, 0, $ww, $wh, $ww, $wh);&lt;/blockquote&gt;Finally, we set the blending mode false and save the alpha channel information for transparency:&lt;br /&gt;&lt;blockquote&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; imagealphablending($image, false);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; imagesavealpha($image, true);&lt;/blockquote&gt;That's it. We return the image resource for further manipulation.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/142925038823267790-8504802616269756163?l=php-help.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://php-help.blogspot.com/feeds/8504802616269756163/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://php-help.blogspot.com/2011/06/using-alpha-png-as-watermark-in-php.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/142925038823267790/posts/default/8504802616269756163'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/142925038823267790/posts/default/8504802616269756163'/><link rel='alternate' type='text/html' href='http://php-help.blogspot.com/2011/06/using-alpha-png-as-watermark-in-php.html' title='Using a transparent PNG image as watermark in PHP'/><author><name>Adi</name><uri>http://www.blogger.com/profile/16262430025050344583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-142925038823267790.post-7154954025976339023</id><published>2011-06-16T01:40:00.000-07:00</published><updated>2011-06-22T02:54:04.293-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='MySQL'/><category scheme='http://www.blogger.com/atom/ns#' term='longitude and latitude distance calculator'/><category scheme='http://www.blogger.com/atom/ns#' term='zip codes'/><category scheme='http://www.blogger.com/atom/ns#' term='distance calculator'/><category scheme='http://www.blogger.com/atom/ns#' term='calculate distance using zip codes'/><title type='text'>Calculate the distance using lattitude, longitude and ZIP codes in MySQL</title><content type='html'>So, assuming we have a table:&lt;br /&gt;&lt;br /&gt;ZIP_CODES&lt;br /&gt;zip_code | lattitude | longitude&lt;br /&gt;&lt;br /&gt;We take this zip code from NY&lt;br /&gt;$zip =  10001&lt;br /&gt;$lat = 40.750742&lt;br /&gt;$lon =-74.52976&lt;br /&gt;&lt;br /&gt;What we need is the Earth radius (6371 km).&lt;br /&gt;&lt;br /&gt;If we need to search the zip codes that are in a 10 km area, we could use this 'small' sintax:&lt;br /&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;2 * 6371 * ATAN2( (SIN( ( ($lat - lattidude) * PI() / 180 ) / 2 ) * SIN( ( ($lat - lattidude) * PI() / 180 ) / 2 ) + SIN( ( ($lon - longitude) * PI() / 180 ) / 2 ) * SIN( ( ($lon - longitude) * PI() / 180 ) / 2 ) * COS(lattitude) * COS($lat)) , ( 1 - (SIN( ( ($lat - lattidude) * PI() / 180 ) / 2 ) * SIN( ( ($lat - lattidude) * PI() / 180 ) / 2 ) + SIN( ( ($lon - longitude) * PI() / 180 ) / 2 ) * SIN( ( ($lon - longitude) * PI() / 180 ) / 2 ) * COS(lattitude) * COS($lat)) ) ) &amp;lt; 10&lt;/i&gt;&lt;/blockquote&gt;&lt;br /&gt;For this calculation, i used the harvesine formula to calculate the great-circle distance between two points (the shortest distance over the earth's surface - ignoring any hills).&lt;br /&gt;&lt;br /&gt;This formula remains particularly well-conditioned for numerical computation even at small distances – unlike calculations based on the spherical law of cosines.&lt;br /&gt;&lt;br /&gt;Hope this helps.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/142925038823267790-7154954025976339023?l=php-help.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://php-help.blogspot.com/feeds/7154954025976339023/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://php-help.blogspot.com/2011/06/calculate-distance-using-lattitude.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/142925038823267790/posts/default/7154954025976339023'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/142925038823267790/posts/default/7154954025976339023'/><link rel='alternate' type='text/html' href='http://php-help.blogspot.com/2011/06/calculate-distance-using-lattitude.html' title='Calculate the distance using lattitude, longitude and ZIP codes in MySQL'/><author><name>Adi</name><uri>http://www.blogger.com/profile/16262430025050344583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-142925038823267790.post-7674530408337911773</id><published>2011-06-16T01:24:00.000-07:00</published><updated>2011-06-22T02:53:41.560-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='MySQL'/><category scheme='http://www.blogger.com/atom/ns#' term='date_format'/><category scheme='http://www.blogger.com/atom/ns#' term='calculate age'/><title type='text'>How to calculate age in MySQL</title><content type='html'>When programming, you hit yourself with many problems, that apparently are very simple, but require a rather difficult syntax.&lt;br /&gt;&lt;br /&gt;On a site, i found a small problem - to calculate the age of the person and see if that person is between 2 selected ages.&lt;br /&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;For that, i used the MySQL DATE_FORMAT function and NOW function like this:&lt;br /&gt;&lt;br /&gt;&lt;blockquote style="font-style: italic;"&gt;DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(dob, '%Y') -  (DATE_FORMAT(NOW(), '00-%m-%d') &amp;gt; DATE_FORMAT(dob, '00-%m-%d'))&lt;/blockquote&gt;Where 'dob' is the date field for date of birth.&lt;br /&gt;Explanation:&lt;br /&gt;&lt;blockquote style="font-style: italic;"&gt;DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(dob, '%Y')&lt;/blockquote&gt;This returns the difference between the birth year and the current year.&lt;br /&gt;To make this function work if the user has not celebrated his birth date this year, we must decrease one. So we do:&lt;br /&gt;&lt;blockquote style="font-style: italic;"&gt;DATE_FORMAT(NOW(), '00-%m-%d') &amp;gt; DATE_FORMAT(dob, '00-%m-%d')&lt;/blockquote&gt;This operation returns 1 if the user has not reached his birth date, and 0 otherwise.&lt;br /&gt;&lt;br /&gt;Hope this will help you.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/142925038823267790-7674530408337911773?l=php-help.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://php-help.blogspot.com/feeds/7674530408337911773/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://php-help.blogspot.com/2011/06/how-to-calculate-age-in-mysql.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/142925038823267790/posts/default/7674530408337911773'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/142925038823267790/posts/default/7674530408337911773'/><link rel='alternate' type='text/html' href='http://php-help.blogspot.com/2011/06/how-to-calculate-age-in-mysql.html' title='How to calculate age in MySQL'/><author><name>Adi</name><uri>http://www.blogger.com/profile/16262430025050344583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
