I am trying to modify an image resize Python project. The project
(contributed by a good guy) and it works perfect, the only problem is
that I only want to resize a bigger image to smaller image, instead of
resizing smaller to bigger.
I need a parameter in the URL to control it e.g. r=1 then only
minimize the picture. I tried 3 days already to modify the code but I
have no luck since I found that appspot always return error! The
problem is I don't know how to program Python.
I am wondering anyone could do me a favour to help modify the code.
Sorry for my bad English!
Ashley
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/
>.
from google.appengine.api import images, memcache, urlfetch
from google.appengine.ext.webapp import RequestHandler, template,
util, WSGIApplication
from os import path
class ScaleHandler(RequestHandler):
def get(self):
# Retrieve the request parameters.
url = self.request.get("url")
width = self.request.get("width", "0")
height = self.request.get("height", "0")
r = self.request.get("r", "0")
# If the url parameter has not been set, display the help
document.
if not url:
topbar = "http://www.yahoo.com"
template_path = path.join(path.dirname(__file__),
"imagescaler.html")
template_vars = {"topbar": urlfetch.fetch(topbar).content}
self.response.out.write(template.render(template_path,
template_vars))
return
# Determine the cache key for the scaled image according to
the request parameters.
key = "".join([width, "|", height, "|", url])
# Search for the scaled image as a PNG first, then as a JPEG.
thumbnail = memcache.get(key, "png")
if thumbnail is None:
thumbnail = memcache.get(key, "jpeg")
if thumbnail is None:
image = memcache.get(url, "png")
if image is None:
image = memcache.get(url, "jpeg")
if image is None:
# Retrieve the original image from the remote
server.
file = urlfetch.Fetch(url)
image = file.content
# Determine the namespace from the HTTP
response headers.
if file.headers["content-type"] == "image/
jpeg":
namespace = "jpeg"
else:
namespace = "png"
# Attempt to cache the original image if size
allows.
try:
memcache.add(url, image, 0, 0, namespace)
except Exception:
pass
else:
namespace = "jpeg"
else:
namespace = "png"
# Wrap the image in the Image API object.
image = images.Image(image)
# Resize the image according to the requested
dimensions.
image.resize(int(width), int(height))
# Generate the new version of the image.
if namespace == "jpeg":
thumbnail =
image.execute_transforms(output_encoding=images.JPEG)
else:
thumbnail = image.execute_transforms()
# Cache the new version of the image.
memcache.add(key, thumbnail, 0, 0, namespace)
else:
namespace = "jpeg"
else:
namespace = "png"
# Generate the output to be sent back to the user agent.
self.response.headers["content-type"] = "image/" + namespace
self.response.out.write(thumbnail)
if __name__ == "__main__":
util.run_wsgi_app(WSGIApplication([("/", ScaleHandler)],
debug=True))