<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users816</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/816" rel="self"/>
  <title>Juha Hollanti</title>
  <updated>Wed Aug 20 11:19:40 -0700 2008</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor15370</id>
    <published>2008-08-20T11:19:40-07:00</published>
    <title>[JavaScript] On Setting focus after alert</title>
    <content type="html">&lt;p&gt;Form elements can be disabled. Ofcourse in this case the focus is set after enabling the element. But would that suit your needs? I was just curious, why do you need to disable the events?&lt;/p&gt;

&lt;pre&gt;//msg is the string to show in the alert.
//obj is the input to focus
function msgFocus(msg, obj){
    obj.disabled = true;
    alert(msg);
    setTimeout(function() { 
        obj.disabled = false; 
        obj.focus(); 
    }, 50);
}&lt;/pre&gt;</content>
    <author>
      <name>Juha Hollanti</name>
      <email>juha.hollanti@newave.fi</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/442-setting-focus-after-alert/refactors/15370" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor15080</id>
    <published>2008-08-15T09:47:40-07:00</published>
    <title>[PHP] On Getter and Setter</title>
    <content type="html">&lt;p&gt;&amp;quot;Should you create a getter/setter function for every single var you send to the class?&amp;quot;&lt;/p&gt;

&lt;p&gt;I guess that's the general idea. Call me sloppy if you will but sometimes i tend to go around setters and getters, simply because it's simpler. Don't get me wrong though, i wouldn't want to encourage that kind of behaviour 'cause in the end it's a time bomb. &lt;/p&gt;

&lt;p&gt;But i guess that if you find yourself in a situation where you have loads of variables in a class that you need to be able to set and get from the outside then it might mean that the class design isn't very good, or that it should be divided into a set of smaller classes.&lt;/p&gt;

&lt;p&gt;In my very humble opinion a perfectly valid way to set a large amount of variables is through the constructor method when initializing new instances. But that won't help you if you want to get a hold of them at a later point. &lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Juha Hollanti</name>
      <email>juha.hollanti@newave.fi</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/425-getter-and-setter/refactors/15080" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor15015</id>
    <published>2008-08-14T08:11:19-07:00</published>
    <title>[PHP] On Resize image to canvas</title>
    <content type="html">&lt;p&gt;Checking for GD lib might actually be prudent. I once lost hours trying to get something like a picture upload working on a new server. For some reason nothing gave out any error messages. &lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Juha Hollanti</name>
      <email>juha.hollanti@newave.fi</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/433-resize-image-to-canvas/refactors/15015" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor14966</id>
    <published>2008-08-13T13:53:29-07:00</published>
    <title>[PHP] On Adding seconds to a MySQL timestamp</title>
    <content type="html">&lt;p&gt;Wow, that's neat! And you're right, it is faster. What i really dig about it though is that it's much more readable. Good stuff!&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Juha Hollanti</name>
      <email>juha.hollanti@newave.fi</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/363-adding-seconds-to-a-mysql-timestamp/refactors/14966" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code433</id>
    <published>2008-08-13T11:50:19-07:00</published>
    <updated>2011-04-14T16:16:00-07:00</updated>
    <title>[PHP] Resize image to canvas</title>
    <content type="html">&lt;p&gt;The built in image functions found in PHP manage to boggle my mind to a point where i can no longer understand anything of what's going on. I'll just end up messing around long enough to get things right. &lt;/p&gt;

&lt;p&gt;For this reason i'm always trying to write up functions (which often end up horribly wrong) that would obfuscate all the mind boggling soup out of my sight. &lt;/p&gt;

&lt;p&gt;Anything to make the following more bearable to live with is very, very welcome. Thanks.&lt;/p&gt;

&lt;pre&gt;/**
 * Scale an image according to input canvas dimensions. 
 * No cropping will occur, the scaled image is &amp;quot;centered&amp;quot; to canvas.
 */

function imageToCanvas ($_image, $_canvasWidth, $_canvasHeight)
{
    if (is_string($_image)) {
        if (is_file($_image)) {
            $_image = imagecreatefromjpeg($_image);
        } else {
            return &amp;quot;Incorrect imagepath &amp;gt;&amp;gt; No file here &amp;quot; . $_image;
        }
    } 
    
    $width = imagesx($_image);
    $height = imagesy($_image);
    $image_aspect_ratio = $width / $height;
    $canvas_aspect_ratio = $_canvasWidth / $_canvasHeight;
    
    // scale by height
    if ($image_aspect_ratio &amp;lt; $canvas_aspect_ratio) {
        $new_height = $_canvasHeight;
        $new_width = ($new_height/$height) * $width;
    } 
    // scale by width
    else {
        $new_width = $_canvasWidth;
        $new_height = ($new_width/$width) * $height;
    }
    
    # offset values (ie. center the resized image to canvas)
    $xoffset = ($_canvasWidth - $new_width) / 2;
    $yoffset = ($_canvasHeight - $new_height) / 2;
    
    $image_resized = imagecreatetruecolor($_canvasWidth, $_canvasHeight);
    
    # fill colour
    $white = imagecolorallocate($image_resized, 255, 255, 255);
    imagefill($image_resized, 0, 0, $white);
    imagecopyresampled($image_resized, $_image, $xoffset, $yoffset, 0, 0, $new_width, $new_height, $width, $height);
    
    imagedestroy($_image);
    return $image_resized;
}&lt;/pre&gt;</content>
    <author>
      <name>Juha Hollanti</name>
      <email>juha.hollanti@newave.fi</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/433-resize-image-to-canvas" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor14903</id>
    <published>2008-08-12T19:26:09-07:00</published>
    <title>[PHP] On Array and ForEach Loop</title>
    <content type="html">&lt;p&gt;Yeah you're right, parsing XML &amp;quot;manually&amp;quot; isn't usually a very bright idea. But i don't see a problem with creating XML &amp;quot;manually&amp;quot; as long as it's quite simple and straightforward, though. &lt;/p&gt;

&lt;pre&gt;echo '&amp;lt;qdapi&amp;gt;';
foreach($payload as $key =&amp;gt; $value) {
    echo &amp;quot;&amp;lt;$key&amp;gt;$value&amp;lt;/$key&amp;gt;&amp;quot;;
}
echo '&amp;lt;/qdapi&amp;gt;';&lt;/pre&gt;</content>
    <author>
      <name>Juha Hollanti</name>
      <email>juha.hollanti@newave.fi</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/430-array-and-foreach-loop/refactors/14903" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor14880</id>
    <published>2008-08-12T10:28:35-07:00</published>
    <title>[ActionScript] On Isometric X/Y Grid pattern depth sort</title>
    <content type="html">&lt;p&gt;Umm, i don't know what you're trying to do. Maybe you could explain your snippet of code a bit more? I kind of got the idea that you're trying to do some 3D stuff, right? Am i close or am i just completely off the map?&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Juha Hollanti</name>
      <email>juha.hollanti@newave.fi</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/424-isometric-x-y-grid-pattern-depth-sort/refactors/14880" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor13170</id>
    <published>2008-07-15T19:42:30-07:00</published>
    <title>[PHP] On Adding seconds to a MySQL timestamp</title>
    <content type="html">&lt;p&gt;Good stuff!&lt;/p&gt;

&lt;p&gt;Gotta hate myself for letting that typo creep in :) I checked the input like three times before submitting but there it still is. &lt;/p&gt;

&lt;p&gt;I'm not entirely with you on the fourth amendment though. I wanted to leave it to a row of it's own only so that it wouldn't get obfuscated with the rest of the mess. After all the lines are pretty long as it is. &lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Juha Hollanti</name>
      <email>juha.hollanti@newave.fi</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/363-adding-seconds-to-a-mysql-timestamp/refactors/13170" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code363</id>
    <published>2008-07-14T13:38:11-07:00</published>
    <updated>2009-12-30T12:44:09-08:00</updated>
    <title>[PHP] Adding seconds to a MySQL timestamp</title>
    <content type="html">&lt;p&gt;Suggestions are welcome. I'd expect some internationalization or performance issues perhaps? &lt;/p&gt;

&lt;pre&gt;/**
 * Split the supplied timestamp string, add the seconds and concatenate the whole thing back into timestamp format. 
 * Note! that the mktime function will perform even if the supplied amount of seconds exceeds 59. It just adds them 
 * as minutes, hours, days, etc. and returns the Unix timestamp from which we can form the MySQL timestamp. 
 */
function addSecondsToTimestamp ($_timestamp, $_amount) 
{
    ist($year, $month, $day, $hours, $minutes, $seconds) = preg_split(&amp;quot;/-| |:/i&amp;quot;, $_timestamp);
    $seconds += $_amount;
    return date('Y-m-d H:i:s', mktime($hours, $minutes, $seconds, $month, $day, $year));
}&lt;/pre&gt;</content>
    <author>
      <name>Juha Hollanti</name>
      <email>juha.hollanti@newave.fi</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/363-adding-seconds-to-a-mysql-timestamp" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor13048</id>
    <published>2008-07-13T21:04:15-07:00</published>
    <title>[ActionScript] On Get color from string</title>
    <content type="html">&lt;p&gt;Ahh, ok. I misunderstood the goal a bit. Maybe we can get through by building upon your earlier code and replacing the '+' quantifier with '{6}' quantifier. Is this more on the way that you meant it?&lt;/p&gt;

&lt;pre&gt;public static function getColorFromString(color:String):int
{
    return parseInt(new RegExp(/[0-9a-fA-F]{6}/).exec(color),16);
}&lt;/pre&gt;</content>
    <author>
      <name>Juha Hollanti</name>
      <email>juha.hollanti@newave.fi</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/361-get-color-from-string/refactors/13048" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor13030</id>
    <published>2008-07-13T12:08:39-07:00</published>
    <title>[ActionScript] On Get color from string</title>
    <content type="html">&lt;p&gt;I can't picture getting around this faster without using RegExes. &lt;/p&gt;

&lt;p&gt;However i might have a few options to speed things up a bit. I remember reading that RegEx literals would be faster than creating an alltogether new RegEx object. Atleast when you're using it only once like in this case. You could make the RegEx object a class variable? &lt;/p&gt;

&lt;p&gt;My version below with the slightly altered RegEx scored an average of 57ms when put through a loop of 10 000. Your code got an average of 116ms in the same test. &lt;/p&gt;

&lt;pre&gt;public static function getColorFromString(color:String):int
{
    // Convert '#' to '0x'. Everything starting with '0x' is interpreted by AS automatically as a hex number.
    return parseInt( color.replace(/^#/, &amp;quot;0x&amp;quot;) );
}&lt;/pre&gt;</content>
    <author>
      <name>Juha Hollanti</name>
      <email>juha.hollanti@newave.fi</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/361-get-color-from-string/refactors/13030" rel="alternate"/>
  </entry>
</feed>

