Bene! chi ha buon occhio e di norma legge e si studia tutti gli script
noterà le lievi differenze del codice, ma in termini di prestazioni
sono molto evidenti
Quindi ecco l'ennesima versione dello script trattato in questo post
Codice PHP:
<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System"%>
<%@ import Namespace="System.IO"%>
<%@ import Namespace="System.Drawing" %>
<%@ import Namespace="System.Drawing.Imaging" %>
<%@ import Namespace="System.Text.RegularExpressions" %>
<script Language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
int ThumbnailWidth = 64;
int ThumbnailHeight = 64;
string strFileName = "";
string destinazione = Server.MapPath("./public/");
double orgImgWidth = 0.0;
double orgImgHeight = 0.0;
double rThumbnailWidth = 0.0;
double rThumbnailHeight = 0.0;
if (Request.QueryString.Count != 0)
{
if (Request.QueryString["w"] != null) ThumbnailWidth = Convert.ToInt32(Request.QueryString["w"]);
if (Request.QueryString["h"] != null) ThumbnailHeight = Convert.ToInt32(Request.QueryString["h"]);
if (Request.QueryString["src"] != null)
{
strFileName = Convert.ToString(Request.QueryString["src"]);
if (strFileName != "")
{
try
{
using(System.Drawing.Image tmpImage = System.Drawing.Image.FromFile(Server.MapPath(strFileName)))
{
rThumbnailWidth = ThumbnailWidth;
rThumbnailHeight = ThumbnailHeight;
orgImgWidth = tmpImage.Width;
orgImgHeight = tmpImage.Height;
// Se l'immagine è più piccola del Thumbnail forziamo le dimensioni di
// quest'ultimo alle dimensioni dell'immagine.
if (orgImgWidth < ThumbnailWidth) ThumbnailWidth = tmpImage.Width;
if (orgImgHeight < ThumbnailHeight) ThumbnailHeight = tmpImage.Height;
// Se le dimensioni dell'immagine e del Thumbnail corrispondono non facciamo nulla.
// quindi se l'immagine è quadrata...
if (tmpImage.Width != tmpImage.Height)
{
// Se l'immagine non è quadrata
// continuiamo a fare i nostri controlli per calcolarne le
// giuste dimensioni e quindi creare la nostra nuova immagine con
// l'aspectratio corretto.
// Se l'immagine è più larga che alta
if (tmpImage.Width > tmpImage.Height)
{
// L > H => L = tL
// La larghezza del TN sarà quella da noi stabilita
// quindi dobbiamo solo assegnarla.
rThumbnailWidth = ThumbnailWidth;
// L'altezza del TN invece dovrà essere ricalcolata
// in proporzione alla larghezza dell'immagine originale.
// L > H => tH = (H * tL)/ L
rThumbnailHeight = (double)((tmpImage.Height * ThumbnailWidth) / tmpImage.Width);
} // if (tmpImage.Width > tmpImage.Height)
else
{
// Altrimenti se l'immagine è più alta
if (tmpImage.Width < tmpImage.Height)
{
// L < H => H = tH
// L'altezza del TN sarà quella da noi stabilita
// quindi dobbiamo solo assegnarla.
rThumbnailHeight = ThumbnailHeight;
// La larghezza del TN invece dovrà essere ricalcolata
// in proporzione alla altezza dell'immagine originale.
// L < H => tL = (L * tH)/ H
rThumbnailWidth = (double)((tmpImage.Width * ThumbnailHeight) / tmpImage.Height);
} // if (tmpImage.Width < tmpImage.Height)
} // else
} // if (tmpImage.Width != tmpImage.Height)
using(Bitmap bitmap = new Bitmap(tmpImage, (int)(rThumbnailWidth), (int)(rThumbnailHeight)))
{
Response.ContentType = tmpImage.RawFormat.ToString();
Response.Clear();
Response.BufferOutput = true;
Response.AddHeader("Content-Disposition", "filename=" + strFileName);
// In questo caso viene creata e salvata la Thumbnail dell'immagine in /thumbs/
if ((tmpImage.Width > ThumbnailWidth) || (tmpImage.Height > ThumbnailHeight))
{
bitmap.Save(Response.OutputStream, tmpImage.RawFormat);
bitmap.Save((destinazione + strFileName), tmpImage.RawFormat);
} // if ((tmpImage.Width > ThumbnailWidth) || (tmpImage.Height > ThumbnailHeight))
else // L'immagine rientra già nella dimensione da noi decisa quindi non serve creargli il Thumbnail.
{
bitmap.Save(Response.OutputStream, tmpImage.RawFormat);
// Se vuoi che l'immagine venga comunque salvata nella cartella dei Thumbnails leva "//" dalla riga sottostante.
bitmap.Save(destinazione + strFileName, tmpImage.RawFormat);
} // else
Response.Flush();
} // using(Bitmap bitmap = new Bitmap(orgImage, (int)(rThumbnailWidth), (int)(rThumbnailHeight)))
} // using(System.Drawing.Image tmpImage = System.Drawing.Image.FromFile(Server.MapPath(strFileName)))
} // try
catch (Exception ex)
{
Response.Write(ex.Message);
} // catch (Exception ex)
finally
{
Response.Write("\nCoded by R.B. Riddick");
}
} // if (strFileName != "")
} // if (Request.QueryString["src"] != "")
} // if (Request.QueryString.Count != 0)
} // void Page_Load(Object sender, EventArgs e)
</script>
in tanto, quando ho tempo voglia e desiderio sto ancora lavorando alla nuova versione