This ASP.NET code resizes an image using the ImageResize assembly.
r.aspx?command=resize|rotate&src=src_image_file&dst=dst_image_file&width=new_width&height=new_height&abspaths=1&outformat=gif|jpg&angle=0|90|180|270
The following code must be added/merged to the lib/exe/fetch.php in your Doku Wiki installation
/** * Resizes the given image to the given size * * @author Andreas Gohr <andi@splitbrain.org> */ function get_resized($file, $ext, $w, $h=0){ global $conf; $info = getimagesize($file); if(!$h) $h = round(($w * $info[1]) / $info[0]); // we wont scale up to infinity if($w > 2000 || $h > 2000) return $file; //cache $local = getCacheName($file,'.media.'.$w.'x'.$h.'.'.$ext); $mtime = @filemtime($local); // 0 if not exists if( $mtime > filemtime($file) || resize_imageToolkit($ext,$file,$info[0],$info[1],$local,$w,$h) || resize_imageIM($ext,$file,$info[0],$info[1],$local,$w,$h) || resize_imageGD($ext,$file,$info[0],$info[1],$local,$w,$h) ){ return $local; } //still here? resizing failed return $file; } /* ***** other PHP code ***** */ /** * resize images using another page * * @ Giuseppe Amato (paipai-a.t-tiscalinet-d.o.t-it ) * r.aspx?command=resize|rotate * &src=src_image_file * &dst=dst_image_file * &width=new_width * &height=new_height * &abspaths=1 * &outformat=gif|jpg * &angle=0|90|180|270 */ function resize_imageToolkit($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){ $script_uri = dirname($_SERVER['SCRIPT_NAME']) . "/r.aspx"; $qstring = 'command=resize&aspect=no&abspaths=1&src=' . $from . '&width=' . $to_w . '&height=' . $to_h . "&outformat=" . $ext; $server_name = $_SERVER['SERVER_NAME']; $url_c = "http://" . $server_name . $script_uri . "?" . $qstring; // print "url=" . $url_c; // exit(); return io_download($url_c, $to); }
// FILE: r.aspx <%@ Page Language="C#" Debug="true" %> <%@ Import Namespace="ImageHelper" %> <%@ Import Namespace="System.Drawing.Imaging" %> <%@ Import Namespace="System.IO" %> <% /* (C) 2006 Giuseppe Amato (paipai-a.t-tiscalinet-d.o.t-it ) http://www.napoliwireless.net/ 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 2 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, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ string cmd = Request["command"]; string angle = Request["angle"]; string width = Request["width"]; string height = Request["height"]; string src = Request["src"]; string dst = Request["dst"]; string abspaths=Request["abspaths"]; string outformat = Request["outformat"]; System.IO.Stream dstStream = null; bool dstToFile = false; string ContentType = "image/jpeg"; ImageFormat imageFormat = ImageFormat.Jpeg; if (src != null && src.Length > 0 && (outformat == null || outformat.Length == 0)) { string ext = src.Substring(src.Length -4); if (ext.ToLower() == "gif") { ContentType = "image/gif"; imageFormat = ImageFormat.Gif; } else{ ContentType = "image/jpeg"; imageFormat = ImageFormat.Jpeg; } } if (outformat != null && outformat.ToLower() == "gif") { ContentType = "image/gif"; imageFormat = ImageFormat.Gif; } if (src == null || src.Length == 0) { Response.Write("ERROR: no src specified!"); Response.End(); } else { if (abspaths == null || abspaths.Length == 0) src = Server.MapPath(src); if (!System.IO.File.Exists(src)) { Response.Write("ERROR: specified src file doesnt exists!"); Response.End(); } } if (dst == null || dst.Length == 0) dstStream = Response.OutputStream; else { if (abspaths == null || abspaths.Length == 0) dst = Server.MapPath(dst); dstStream = new System.IO.FileStream(dst, FileMode.Create, FileAccess.Write, FileShare.Read); dstToFile = true; } ImageHelper.ImageResize o = new ImageHelper.ImageResize(); o.File = src; o.UsePercentages = false; if (cmd == "resize") { int iWidth = 0; int iHeight = 0; try{ iWidth = Int32.Parse(width); } catch(Exception) { iWidth = 0; } try{ iHeight = Int32.Parse(height); } catch(Exception) { iHeight = 0; } o.Width = iWidth; o.Height = iHeight; Response.ContentType = ContentType; System.Drawing.Image i = o.GetThumbnail(); i.Save(dstStream, imageFormat); if (dstToFile) { dstStream.Close(); Response.Write("ok"); } } else { int iAngle = 0; try{ iAngle = Int32.Parse(angle); } catch(Exception) { iAngle = 0; } Response.ContentType = ContentType; o.Rotate(iAngle).Save(dstStream, imageFormat); if (dstToFile) { dstStream.Close(); Response.Write("ok"); } } Response.End(); %>
// file: ImageResize.cs using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; namespace ImageHelper { public class ImageResize { private double m_width, m_height; private bool m_use_aspect = true; private bool m_use_percentage = false; private string m_image_path; private Image m_src_image, m_dst_image; private ImageResize m_cache; private Graphics m_graphics; public string File { get { return m_image_path; } set { m_image_path = value; } } public Image Image { get { return m_src_image; } set { m_src_image = value; } } public bool PreserveAspectRatio { get { return m_use_aspect; } set { m_use_aspect = value; } } public bool UsePercentages { get { return m_use_percentage; } set { m_use_percentage = value; } } public double Width { get { return m_width; } set { m_width = value; } } public double Height { get { return m_height; } set { m_height = value; } } protected virtual bool IsSameSrcImage(ImageResize other) { if (other != null) { return (this.File == other.File && this.Image == other.Image); } return false; } protected virtual bool IsSameDstImage(ImageResize other) { if (other != null) { return (this.Width == other.Width && this.Height == other.Height && this.UsePercentages == other.UsePercentages && this.PreserveAspectRatio == other.PreserveAspectRatio); } return false; } public virtual Image Rotate(int angle) { if (!IsSameSrcImage(m_cache)) { if (m_image_path.Length > 0) { // Load via stream rather than Image.FromFile to release the file // handle immediately if (m_src_image != null) m_src_image.Dispose(); // Wrap the FileStream in a "using" directive, to ensure the handle // gets closed when the object goes out of scope using(Stream stream = new FileStream(m_image_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) m_src_image = Image.FromStream(stream); } } RotateFlipType rf = RotateFlipType.RotateNoneFlipNone; if (angle >0 && angle <= 90) rf = RotateFlipType.Rotate90FlipNone; else if (angle >90 && angle <= 180) rf = RotateFlipType.Rotate180FlipNone; else if (angle >180) rf = RotateFlipType.Rotate270FlipNone; if (m_dst_image != null) { m_dst_image.Dispose(); m_graphics.Dispose(); } Bitmap bitmap = new Bitmap((int)m_src_image.Width, (int)m_src_image.Height, PixelFormat.Format24bppRgb); m_graphics = Graphics.FromImage(bitmap); m_graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; m_graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; m_graphics.DrawImage(m_src_image, 0, 0, bitmap.Width, bitmap.Height); m_dst_image = bitmap; m_dst_image.RotateFlip(rf); return m_dst_image; } public virtual Image GetThumbnail() { // Flag whether a new image is required bool recalculate = false; double new_width = Width; double new_height = Height; // Is there a cached source image available? If not, // load the image if a filename was specified; otherwise // use the image in the Image property if (!IsSameSrcImage(m_cache)) { if (m_image_path.Length > 0) { // Load via stream rather than Image.FromFile to release the file // handle immediately if (m_src_image != null) m_src_image.Dispose(); // Wrap the FileStream in a "using" directive, to ensure the handle // gets closed when the object goes out of scope using(Stream stream = new FileStream(m_image_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) m_src_image = Image.FromStream(stream); recalculate = true; } } // Check whether the required destination image properties have // changed if (!IsSameDstImage(m_cache)) { // Yes, so we need to recalculate. // If you opted to specify width and height as percentages of the original // image's width and height, compute these now if (UsePercentages) { if (Width != 0) { new_width = (double) m_src_image.Width * Width / 100; if (PreserveAspectRatio) { new_height = new_width * m_src_image.Height / (double) m_src_image.Width; } } if (Height != 0) { new_height = (double) m_src_image.Height * Height / 100; if (PreserveAspectRatio) { new_width = new_height * m_src_image.Width / (double) m_src_image.Height; } } } else { // If you specified an aspect ratio and absolute width or height, then calculate this // now; if you accidentally specified both a width and height, // the specified values are used as maximum allowed for each dimension if (PreserveAspectRatio) { if (Width != 0 && Height == 0) { new_height = (Width / (double) m_src_image.Width) * m_src_image.Height; } else if (Height != 0 && Width == 0) { new_width = (Height / (double) m_src_image.Height) * m_src_image.Width; } else if (Height != 0 && Width != 0) { new_height = (Width / (double) m_src_image.Width) * m_src_image.Height; new_width = Width; if (new_height > Height) { new_width = (Height / (double) m_src_image.Height) * m_src_image.Width; new_height = Height; } } } } recalculate = true; } if (recalculate) { // Calculate the new image if (m_dst_image != null) { m_dst_image.Dispose(); m_graphics.Dispose(); } Bitmap bitmap = new Bitmap((int)new_width, (int)new_height, PixelFormat.Format24bppRgb); m_graphics = Graphics.FromImage(bitmap); m_graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; m_graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; m_graphics.DrawImage(m_src_image, 0, 0, bitmap.Width, bitmap.Height); m_dst_image = bitmap; // Cache the image and its associated settings m_cache = this.MemberwiseClone() as ImageResize; } return m_dst_image; } ~ImageResize() { // Free resources if (m_dst_image != null) { m_dst_image.Dispose(); m_graphics.Dispose(); } if (m_src_image != null) m_src_image.Dispose(); } } }