<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:refactormycode.com,2007:users270</id>
  <link type="application/atom+xml" href="http://refactormycode.com/users/270" rel="self"/>
  <title>tempouser.myopenid.com</title>
  <updated>Wed Nov 07 09:33:44 +0000 2007</updated>
  <entry>
    <id>tag:refactormycode.com,2007:Refactor780</id>
    <published>2007-11-07T09:33:44+00:00</published>
    <title>[PHP] On Thumbnail generator</title>
    <content type="html">&lt;p&gt;@ Huber Roksor: its all local stuff, no remote ressources.
&lt;br /&gt;ive tested the imagecopyresampled() function. i will use imagecopyresized, because in my tests, the quality results are not better than expected. in my opinion the images are looking a bit washy.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>tempouser.myopenid.com</name>
      <email></email>
    </author>
    <link type="text/html" href="http://refactormycode.com/codes/122-thumbnail-generator/refactors/780" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:refactormycode.com,2007:Refactor726</id>
    <published>2007-11-05T08:32:53+00:00</published>
    <title>[PHP] On Thumbnail generator</title>
    <content type="html">&lt;p&gt;I noticed, that the imagecreatetruecolor() function can be very slow for images larger than 2000x2000.
&lt;br /&gt;So I added a cache-mode. Maybe there is something to refactor ;)&lt;/p&gt;

&lt;pre&gt;&amp;lt;?
    // cache mode settings
    $cache = true;                    // true/false --&amp;gt; enable/disable cache mode
    $cache_dir = &amp;quot;./cache&amp;quot;;                // server directory -&amp;gt; must be writeable
    $cache_web_dir = &amp;quot;/fotos/cache&amp;quot;;    // web directory

    // requiered argument
    $pic = $_REQUEST[&amp;quot;pic&amp;quot;];
    
    // optional parameters
    $maxwidth = (isset($_REQUEST['width']) ? floor($_REQUEST['width']) : 160);    // default width 160
    $maxheight = (isset($_REQUEST['height']) ? floor($_REQUEST['height']) : 120);    // default height 120

    $getimg = getimagesize($pic);
    // 0. width
    // 1. height
    // 2. typ: 1=GIF, 2=JPEG, 3=PNG
    // 3. html tag (e.g. 'width=&amp;quot;111&amp;quot; height=&amp;quot;24&amp;quot;')
    
    // preventing from accessing non gfx files.
    if ((($getimg[2] &amp;lt; 1) || ($getimg[2] &amp;gt; 3)) || ($getimg[2] == &amp;quot;&amp;quot;)) {
        die(&amp;quot;No valid gfx-source.&amp;quot;);
    }
    
    $oldwidth = $getimg[0];
    $oldheight = $getimg[1];
    
    // if original is smaller then or equal to new image
    if ( $oldwidth &amp;lt;= $maxwidth &amp;amp;&amp;amp; $oldheight &amp;lt;= $maxheight ) {
        header(&amp;quot;HTTP/1.1 302 Found&amp;quot;);
        header(&amp;quot;Location: &amp;quot; . $pic);
    }
    else {
        // image should be resized
        $widthdif = $oldwidth / $maxwidth;
        $heightdif = $oldheight / $maxheight;
        $maxdif = max($widthdif, $heightdif);
        $newwidth = floor($oldwidth / $maxdif);
        $newheight = floor($oldheight / $maxdif);

        // caching enabled
        if ($cache) {
            $char_replace = array(&amp;quot;.&amp;quot;, &amp;quot;/&amp;quot;);
            $cache_dir = $cache_dir . &amp;quot;/&amp;quot; . str_replace($char_replace, &amp;quot;&amp;quot;, dirname($pic)) . &amp;quot;w&amp;quot; . $newwidth . &amp;quot;h&amp;quot; . $newheight . basename($pic);
            $cache_web_dir = $cache_web_dir . &amp;quot;/&amp;quot; . str_replace($char_replace, &amp;quot;&amp;quot;, dirname($pic)) . &amp;quot;w&amp;quot; . $newwidth . &amp;quot;h&amp;quot; . $newheight . basename($pic);
            if (file_exists($cache_dir)) {
                header(&amp;quot;HTTP/1.1 302 Found&amp;quot;);
                header(&amp;quot;Location: &amp;quot; . $cache_web_dir);
                die;
            }
        }

        $smallimg = imagecreatetruecolor($newwidth,$newheight);

        switch ($getimg[2]) {
            case 1:
                // gif image
                $bigimg = imagecreatefromgif($pic);
                imagecopyresized($smallimg, $bigimg, 0, 0, 0, 0, $newwidth, $newheight, $oldwidth, $oldheight);

                if ($cache) {
                    imagegif($smallimg, $cache_dir);
                    header(&amp;quot;HTTP/1.1 302 Found&amp;quot;);
                    header(&amp;quot;Location: &amp;quot; . $cache_web_dir);
                }
                else {    
                    header(&amp;quot;Content-Type: image/gif&amp;quot;);
                    imagegif($smallimg);
                }
                break;
            
            case 2:
                // jpg image
                $bigimg = imagecreatefromjpeg($pic);
                imagecopyresized($smallimg, $bigimg, 0, 0, 0, 0, $newwidth, $newheight, $oldwidth, $oldheight);
                
                if ($cache) {
                    imagejpeg($smallimg, $cache_dir);
                    header(&amp;quot;HTTP/1.1 302 Found&amp;quot;);
                    header(&amp;quot;Location: &amp;quot; . $cache_web_dir);
                }
                else {    
                    header(&amp;quot;Content-Type: image/jpeg&amp;quot;);
                    imagejpeg($smallimg);
                }
                break;
                
            case 3:
                // png image
                $bigimg = imagecreatefrompng($pic);
                imagecopyresized($smallimg, $bigimg, 0, 0, 0, 0, $newwidth, $newheight, $oldwidth, $oldheight);

                if ($cache) {
                    imagepng($smallimg, $cache_dir);
                    header(&amp;quot;HTTP/1.1 302 Found&amp;quot;);
                    header(&amp;quot;Location: &amp;quot; . $cache_web_dir);
                }
                else {    
                    header(&amp;quot;Content-Type: image/png&amp;quot;);
                    imagepng($smallimg);
                }
                break;
        }
    }
?&amp;gt; &lt;/pre&gt;</content>
    <author>
      <name>tempouser.myopenid.com</name>
      <email></email>
    </author>
    <link type="text/html" href="http://refactormycode.com/codes/122-thumbnail-generator/refactors/726" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:refactormycode.com,2007:Refactor696</id>
    <published>2007-11-02T10:03:09+00:00</published>
    <title>[PHP] On Thumbnail generator</title>
    <content type="html">&lt;p&gt;Thanks a lot!&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>tempouser.myopenid.com</name>
      <email></email>
    </author>
    <link type="text/html" href="http://refactormycode.com/codes/122-thumbnail-generator/refactors/696" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:refactormycode.com,2007:Refactor693</id>
    <published>2007-11-02T09:04:22+00:00</published>
    <title>[PHP] On Thumbnail generator</title>
    <content type="html">&lt;p&gt;Hello,&lt;/p&gt;

&lt;p&gt;thanks for the refactoring. I still got the performance issues, but that must not implicitly topic to the php code.&lt;/p&gt;

&lt;p&gt;Great code improvment! Especialy the resize part. Dont know what i was doing in complex stuff, the &amp;quot;while-thing etc&amp;quot;  ;)&lt;/p&gt;

&lt;p&gt;What is this kind of php syntax postet below? Is there an manual entry?&lt;/p&gt;

&lt;pre&gt;$maxwidth = (isset($_REQUEST['width']) ? $_REQUEST['width'] : 160);
&lt;/pre&gt;</content>
    <author>
      <name>tempouser.myopenid.com</name>
      <email></email>
    </author>
    <link type="text/html" href="http://refactormycode.com/codes/122-thumbnail-generator/refactors/693" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:refactormycode.com,2007:Code122</id>
    <published>2007-10-31T13:28:30+00:00</published>
    <updated>2007-11-07T09:33:47+00:00</updated>
    <title>[PHP] Thumbnail generator</title>
    <content type="html">&lt;p&gt;i got this code running on my old webserver. on the new one (better hardware), this script is pretty slow. dont know why. maybe also an apache problem. but maybe someone can altough improve this code. thanks.&lt;/p&gt;

&lt;pre&gt;&amp;lt;?
    #dl(&amp;quot;php_gd2.dll&amp;quot;);

    // requiered argument
    $pic = $_REQUEST[&amp;quot;pic&amp;quot;];
    
    // optional
    $width = $_REQUEST[&amp;quot;width&amp;quot;];
    $height = $_REQUEST[&amp;quot;height&amp;quot;];
    
    if (($width == &amp;quot;&amp;quot;) || ($height == &amp;quot;&amp;quot;)) {
        $maxwidth = &amp;quot;160&amp;quot;;
        $maxheight = &amp;quot;120&amp;quot;;
    }
    else {
        $maxwidth = $width;
        $maxheight = $height;
    }
    
    $getimg = getimagesize($pic);
    // 0. width
    // 1. height
    // 2. Typ: 1=GIF, 2=JPEG, 3=PNG
    // 3. size html tags (e.g. 'width=&amp;quot;111&amp;quot; height=&amp;quot;24&amp;quot;')
    
    // preventing from accessing non gfx files.
    if ((($getimg[2] &amp;lt; 1) &amp;amp;&amp;amp; ($getimg[2] &amp;gt; 3)) || ($getimg[2] == &amp;quot;&amp;quot;)) {
        die(&amp;quot;No valid gfx-source.&amp;quot;);
    }
    
    $oldwidth = $getimg[0];
    $oldheight = $getimg[1];
    
    // If original is smaller then new image
    if (($oldwidth &amp;lt;= $maxwidth) &amp;amp;&amp;amp; ($oldheight &amp;lt;= $maxheight)) {
        // JPEG
        if($getimg[2]==2){
            $img = ImageCreateFromJPEG($pic);
            header(&amp;quot;Content-Type: image/jpeg&amp;quot;);
            imagejpeg($img);
        }
        
        // Gif
        if($getimg[2]==1){
            $img = ImageCreateFromGIF($pic);
            header(&amp;quot;Content-Type: image/gif&amp;quot;);
            imagegif($img);
        }
    
        // PNG
        if($getimg[2]==3){
            $img = ImageCreateFromPNG($pic);
            header(&amp;quot;Content-Type: image/png&amp;quot;);
            imagepng($img);
        }
    }
    else {    
        if ($oldwidth &amp;gt;= $oldheight) {
            $relation = $oldwidth / $oldheight;
            
            $newwidth = $maxwidth;
            $newheight = round($newwidth / $relation);
            
            while (($newwidth &amp;gt; $maxwidth) || ($newheight &amp;gt; $maxheight)) {
                $newwidth--;
                $newheight = round($newwidth / $relation);                    
            }
        }
        else {
            $relation = $oldheight / $oldwidth;
            
            $newheight = $maxheight;
            $newwidth = round($newheight / $relation);
            
            while (($newheight &amp;gt; $maxheight) || ($newwidth &amp;gt; $maxwidth)) {
                $newheight--;
                $newwidth = round($newheight / $relation);                    
            }
        }

        $imgA = imagecreatetruecolor($newwidth,$newheight);    //alte gdlib --&amp;gt; imagecreate()
    
        // JPEG
        if($getimg[2]==2){
                $imgB = ImageCreateFromJPEG($pic);
                imagecopyresized($imgA, $imgB, 0,0, 0,0, $newwidth, $newheight, $oldwidth, $oldheight);
                header(&amp;quot;Content-Type: image/jpeg&amp;quot;);
                imagejpeg($imgA);
        }
    
        // Gif
        if($getimg[2]==1){
            $imgB = ImageCreateFromGIF($pic);    
            imagecopyresized($imgA, $imgB, 0,0, 0,0, $newwidth, $newheight, $oldwidth, $oldheight);        
            header(&amp;quot;Content-Type: image/gif&amp;quot;);
            imagegif($imgA);
        }
    
        // PNG
        if($getimg[2]==3){
            $imgB = ImageCreateFromPNG($pic);
            imagecopyresized($imgA, $imgB, 0,0, 0,0, $newwidth, $newheight, $oldwidth, $oldheight);
            header(&amp;quot;Content-Type: image/png&amp;quot;);
            imagepng($imgA);
        }
    }
?&amp;gt; &lt;/pre&gt;</content>
    <author>
      <name>tempouser.myopenid.com</name>
      <email>no-email@refactormycode.com</email>
    </author>
    <link type="text/html" href="http://refactormycode.com/codes/122-thumbnail-generator" rel="alternate"/>
  </entry>
</feed>
