diff --git a/apps/gallery/ajax/viewImage.php b/apps/gallery/ajax/viewImage.php
new file mode 100644
index 0000000000000000000000000000000000000000..4f7af1496fcd5d3d8157d1820c371e91694792f8
--- /dev/null
+++ b/apps/gallery/ajax/viewImage.php
@@ -0,0 +1,34 @@
+<?php
+
+/**
+ * ownCloud - gallery application
+ *
+ * @author Ike Devolder
+ * @copyright 2012 Ike Devolder
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either 
+ * version 3 of the License, or any later version.
+ * 
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *  
+ * You should have received a copy of the GNU Lesser General Public 
+ * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
+ * 
+ */
+
+
+OCP\JSON::checkLoggedIn();
+OCP\JSON::checkAppEnabled('gallery');
+
+$img = $_GET['img'];
+
+$image = OC_Gallery_Photo::getViewImage($img);
+if ($image) {
+    OCP\Response::enableCaching(3600 * 24); // 24 hour
+    $image->show();
+}
diff --git a/apps/gallery/js/albums.js b/apps/gallery/js/albums.js
index 413c71471a34bebe1f7639598d625b7dac54adbb..62d3f783eceded951993c3499e02b1ee7631cdbb 100644
--- a/apps/gallery/js/albums.js
+++ b/apps/gallery/js/albums.js
@@ -79,7 +79,7 @@ Albums={
 			});
 			element.append(local);
 		}
-		var photoDisplayTemplate = '<div class="gallery_box"><div class="dummy"></div><div><a rel="images" href="'+OC.linkTo('files','download.php')+'?file=URLPATH"><img src="'+OC.filePath('gallery','ajax','thumbnail.php')+'?img=IMGPATH"></a></div></div>';
+        var photoDisplayTemplate = '<div class="gallery_box"><div class="dummy"></div><div><a rel="images" href="'+OC.linkTo('gallery/ajax','viewImage.php')+'?img=URLPATH"><img src="'+OC.filePath('gallery','ajax','thumbnail.php')+'?img=IMGPATH"></a></div></div>';
 		for (var i in Albums.photos) {
 			element.append(photoDisplayTemplate.replace("IMGPATH", escape(Albums.photos[i])).replace("URLPATH", escape(Albums.photos[i])));
 		}
diff --git a/apps/gallery/lib/photo.php b/apps/gallery/lib/photo.php
index f9527cb5fdbe8301ad70265b25c40e2e01a5ca58..38a6690c63b9c9446860048ee69ff7baebd3b7a0 100644
--- a/apps/gallery/lib/photo.php
+++ b/apps/gallery/lib/photo.php
@@ -97,6 +97,37 @@ class OC_Gallery_Photo {
 		return null;
 	}
 
+    public static function getViewImage($image_name, $owner = null) {
+        if (!$owner) $owner = OCP\USER::getUser();
+        $save_dir = OCP\Config::getSystemValue("datadirectory").'/'. $owner .'/gallery/';
+        $save_dir .= dirname($image_name). '/view/';
+        $image_path = $image_name;
+        $view_file = $save_dir . basename($image_name);
+        if (!is_dir($save_dir)) {
+            mkdir($save_dir, 0777, true);
+        }
+        if (file_exists($view_file)) {
+            $image = new OC_Image($view_file);
+        } else {
+            $image_path = OC_Filesystem::getLocalFile($image_path);
+            if(!file_exists($image_path)) {
+                return null;
+            }
+            $image = new OC_Image($image_path);
+            if ($image->valid()) {
+                $image->resize(1200);
+                $image->fixOrientation();
+                $image->save($view_file);
+            }
+        }
+        if ($image->valid()) {
+            return $image;
+        }else{
+            $image->destroy();
+        }
+        return null;
+    }
+
 	public static function getGalleryRoot() {
 		return OCP\Config::getUserValue(OCP\USER::getUser(), 'gallery', 'root', '');
 	}