<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://wiki.nginx.org/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://wiki.nginx.org/index.php?title=EmbeddedPerlImageResize&amp;feed=atom&amp;action=history</id>
		<title>EmbeddedPerlImageResize - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://wiki.nginx.org/index.php?title=EmbeddedPerlImageResize&amp;feed=atom&amp;action=history"/>
		<link rel="alternate" type="text/html" href="http://wiki.nginx.org/index.php?title=EmbeddedPerlImageResize&amp;action=history"/>
		<updated>2013-06-20T05:50:09Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.19.0</generator>

	<entry>
		<id>http://wiki.nginx.org/index.php?title=EmbeddedPerlImageResize&amp;diff=253&amp;oldid=prev</id>
		<title>MichaelLustfield: moved NginxEmbeddedPerlImageResize to EmbeddedPerlImageResize:&amp;#32;Removing Nginx prefix from page titles</title>
		<link rel="alternate" type="text/html" href="http://wiki.nginx.org/index.php?title=EmbeddedPerlImageResize&amp;diff=253&amp;oldid=prev"/>
				<updated>2010-09-22T19:24:09Z</updated>
		
		<summary type="html">&lt;p&gt;moved &lt;a href=&quot;/NginxEmbeddedPerlImageResize&quot; class=&quot;mw-redirect&quot; title=&quot;NginxEmbeddedPerlImageResize&quot;&gt;NginxEmbeddedPerlImageResize&lt;/a&gt; to &lt;a href=&quot;/EmbeddedPerlImageResize&quot; title=&quot;EmbeddedPerlImageResize&quot;&gt;EmbeddedPerlImageResize&lt;/a&gt;: Removing Nginx prefix from page titles&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;!-- page was renamed from NginxEmbeddedPerlImageReize&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
== Example module to resize images using image magick ==&lt;br /&gt;
&lt;br /&gt;
This module will allow us to view images in different sizes. If the image in that size exists, it will be shown. If it does not exist then it will be created.&lt;br /&gt;
&lt;br /&gt;
This code is in production since 01/2008.&lt;br /&gt;
&lt;br /&gt;
You can call an image such as /atifghaffar.jpg (this will return the origional image)&lt;br /&gt;
&lt;br /&gt;
or you can call /atifghaffar.resize_to.XxY.jpg (this will create and return the XxY image, on further requests, it will serve the copy from the disk)&lt;br /&gt;
for example&lt;br /&gt;
/atifghaffar.resize_to.100x100.jpg&lt;br /&gt;
&lt;br /&gt;
If you want propotional image, you may leave y=0&lt;br /&gt;
for example&lt;br /&gt;
&lt;br /&gt;
/atifghaffar.resize_to.80x0.jpg&lt;br /&gt;
&lt;br /&gt;
Here are some live examples running with the same code.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Origional image&lt;br /&gt;
http://images.worldsoft-cms.info/t/technik-forum.worldsoft.info/profile/1263957/ament.jpg  &lt;br /&gt;
&lt;br /&gt;
scaled to 100x100&lt;br /&gt;
http://images.worldsoft-cms.info/t/technik-forum.worldsoft.info/profile/1263957/ament.resize_to.100x100.jpg&lt;br /&gt;
&lt;br /&gt;
Scaled to 80 width and propotional height&lt;br /&gt;
http://images.worldsoft-cms.info/t/technik-forum.worldsoft.info/profile/1263957/ament.resize_to.80x0.jpg&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
nginx.conf&lt;br /&gt;
&amp;lt;geshi lang=&amp;quot;nginx&amp;quot;&amp;gt;&lt;br /&gt;
http {&lt;br /&gt;
  perl_modules perl/lib; &lt;br /&gt;
  perl_require resize.pm; &lt;br /&gt;
&lt;br /&gt;
  server {&lt;br /&gt;
    location / {&lt;br /&gt;
      root /var/www;&lt;br /&gt;
      if (!-f $request_filename) {&lt;br /&gt;
        rewrite ^(.*)(.jpg|.JPG|.gif|.GIF|.png|.PNG)$ /resize$1$2 last; &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
&lt;br /&gt;
    location /resize {&lt;br /&gt;
      perl resize::handler; &lt;br /&gt;
    } &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/geshi&amp;gt;&lt;br /&gt;
&lt;br /&gt;
perl/lib/resizer.pm&lt;br /&gt;
&amp;lt;geshi lang=&amp;quot;perl&amp;quot;&amp;gt;&lt;br /&gt;
package resize;&lt;br /&gt;
use nginx;&lt;br /&gt;
use Image::Magick;&lt;br /&gt;
our $base_dir=&amp;quot;/var/www&amp;quot;;&lt;br /&gt;
our $image;&lt;br /&gt;
&lt;br /&gt;
sub handler {&lt;br /&gt;
  my $r = shift;&lt;br /&gt;
  return DECLINED unless $r-&amp;gt;uri =~ m/\.resize_to\.\d{1,}?x\d{1,}?\./;&lt;br /&gt;
  my $uri=$r-&amp;gt;uri;&lt;br /&gt;
  $uri=~ s!^/resize!!;&lt;br /&gt;
&lt;br /&gt;
  my $dest_file=&amp;quot;$base_dir/$uri&amp;quot;;&lt;br /&gt;
  my @path_tokens=split(&amp;quot;/&amp;quot;, $uri);&lt;br /&gt;
  my $filename=pop @path_tokens;&lt;br /&gt;
  my @filename_tokens=split('\.', $filename);&lt;br /&gt;
&lt;br /&gt;
  # We know  the last part is the extension;&lt;br /&gt;
  # We know the one before that is the dimensions&lt;br /&gt;
  # We know that the one before that is the resize_to string&lt;br /&gt;
&lt;br /&gt;
  my $ext=pop @filename_tokens;&lt;br /&gt;
  my $dimensions=pop @filename_tokens;&lt;br /&gt;
  pop @filename_tokens;&lt;br /&gt;
  $filename=join('.', @filename_tokens, $ext);&lt;br /&gt;
&lt;br /&gt;
  my $real_file_path=join(&amp;quot;/&amp;quot;,   $base_dir, @path_tokens, $filename);&lt;br /&gt;
  return DECLINED unless -f $real_file_path;&lt;br /&gt;
&lt;br /&gt;
  my ($width,$height)=split(&amp;quot;x&amp;quot;, $dimensions);&lt;br /&gt;
  if ($height&amp;lt;1) {&lt;br /&gt;
    $dimensions=$width;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  $image= new Image::Magick;&lt;br /&gt;
  $image-&amp;gt;Read($real_file_path);&lt;br /&gt;
  $image-&amp;gt;Scale($dimensions);&lt;br /&gt;
  $image-&amp;gt;Write($dest_file);&lt;br /&gt;
  $r-&amp;gt;sendfile($dest_file);&lt;br /&gt;
  return OK;&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
1;&lt;br /&gt;
__END__&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/geshi&amp;gt;&lt;/div&gt;</summary>
		<author><name>MichaelLustfield</name></author>	</entry>

	</feed>