CS0535: 'WAC' does not implement interface member 'Umbraco.Core.IApplicationEventHandler.OnApplicationInitialized(Umbraco.Core.UmbracoApplicationBase, Umbraco.Core.ApplicationContext)'
Yes. Waffel AutoCompress is currently not V6 compatible due to changes in the Umbraco event handlers. I'll add a note on the package frontpage until I get some more time to look at it.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929
It seems to me that it's having problems creating the new directory. I'm running it on localhost, so I don't think permissions would be the problem.
Anyway, the new code for WAC.cs:
using Umbraco.Core; using Umbraco.Web; using umbraco.BusinessLogic; using umbraco.cms.businesslogic; using umbraco.cms.businesslogic.web; using umbraco.cms.businesslogic.media;
/// <summary> /// Summary description for WAC /// </summary>
public class WAC : IApplicationEventHandler { public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { } public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { }
The error came from trying to saving the new image in a folder that did not exist. Perhaps Umbraco 6 no longer use the Id of the upload field as the folder name? Did a little rewrite that seems to hade done it. Could you test the following and let me know how it works?
private static void ImageDownscale(FileStream file, Media sender, string ext)
One more thing. When original image is not jpg the code above fails to update media parameters. Replacing the last part (from the "//delete original file if"-line) with the following should do it :
Should be possible yes. Maby I'll make it an option some time. Not sure the code from your link preserves image formats. If I'm not mistaken System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[1] is the jpg encoder, which is the one used in the example.
Anyways. Thnx for trying out the package :) Hope you make it work for you.
Just a heads up: Released v1.1 for Umbraco 6. Added an experimental setting that allows you to preserve and resize png's with transparency. Better late than never ;)
Does not install correctly in 6.0.2
CS0535: 'WAC' does not implement interface member 'Umbraco.Core.IApplicationEventHandler.OnApplicationInitialized(Umbraco.Core.UmbracoApplicationBase, Umbraco.Core.ApplicationContext)'
Yes. Waffel AutoCompress is currently not V6 compatible due to changes in the Umbraco event handlers. I'll add a note on the package frontpage until I get some more time to look at it.
I have the event handler part worked out, but now having another problem.
line:103 in WaffelImageCompressor.cs
throws:
Server Error in '/' Application.
A generic error occurred in GDI+.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929
It seems to me that it's having problems creating the new directory. I'm running it on localhost, so I don't think permissions would be the problem.
Anyway, the new code for WAC.cs:
The error came from trying to saving the new image in a folder that did not exist. Perhaps Umbraco 6 no longer use the Id of the upload field as the folder name? Did a little rewrite that seems to hade done it. Could you test the following and let me know how it works?
private static void ImageDownscale(FileStream file, Media sender, string ext)
{
//Get settings
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(HttpContext.Current.Server.MapPath("~/config/WaffelAutoCompress.config"));
var targetWidth = System.Convert.ToInt32(xmlDoc.GetElementsByTagName("targetwidth")[0].InnerText);
var allowUpscale = xmlDoc.GetElementsByTagName("allowupscale")[0].InnerText;
var propertyAlias = xmlDoc.GetElementsByTagName("propertyalias")[0].InnerText;
var jpgQuality = xmlDoc.GetElementsByTagName("jpgquality")[0].InnerText;
long compression = System.Convert.ToInt32(jpgQuality);
string fullFilePath = HttpContext.Current.Server.MapPath(sender.getProperty(propertyAlias).Value.ToString());
var fileNameWOExt = Path.GetFileNameWithoutExtension(file.Name.ToString());
//Create new bitmap from uploaded file
Bitmap originalBMP = new Bitmap(file);
//Check if upscaling is allowed and if rule is relevant for current bitmap
bool upscaleConflict = false;
int longest = originalBMP.Width > originalBMP.Height ? originalBMP.Width : originalBMP.Height;
if (allowUpscale != "true" && longest < targetWidth) upscaleConflict = true;
//If no upscale conflict is found proceed with image scaling and compression.
if (!upscaleConflict)
{
//Set correct width and height for scaled image
int imgWidth, imgHeight;
if (originalBMP.Width > originalBMP.Height)
{
imgWidth = targetWidth;
imgHeight = originalBMP.Height * targetWidth / originalBMP.Width;
}
else
{
imgWidth = originalBMP.Width * targetWidth / originalBMP.Height;
imgHeight = targetWidth;
}
//Create scaled bitmap
Bitmap imgBMP = new Bitmap(originalBMP, imgWidth, imgHeight);
Graphics iGraphics = Graphics.FromImage(imgBMP);
if (ext == "png" || ext == "gif") iGraphics.Clear(Color.White); //Sets white background for transparent png and gif
iGraphics.SmoothingMode = SmoothingMode.HighSpeed; iGraphics.InterpolationMode = InterpolationMode.Default;
iGraphics.DrawImage(originalBMP, 0, 0, imgWidth, imgHeight);
System.Drawing.Imaging.ImageCodecInfo codec = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[1]; //jpg
System.Drawing.Imaging.EncoderParameters eParams = new System.Drawing.Imaging.EncoderParameters(1);
eParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, compression); //jpg compression
file.Dispose();
originalBMP.Dispose();
//Save new scaled and compressed jpg
string mediaFolderName = new DirectoryInfo(sender.getProperty(propertyAlias).Value.ToString()).Parent.Name;
string targetDirectory = HttpContext.Current.Server.MapPath(string.Format("~/media/" + mediaFolderName + "/"));
imgBMP.Save(targetDirectory + fileNameWOExt + ".jpg", codec, eParams);
imgBMP.Dispose();
iGraphics.Dispose();
//Delete original file if not jpg
if (ext != "jpg")
{
File.Delete(HttpContext.Current.Server.MapPath(sender.getProperty(propertyAlias).Value.ToString()));
}
//Populate upload property and standard Image properties if they exist
sender.getProperty(propertyAlias).Value = "/media/" + mediaFolderName + "/" + fileNameWOExt + ".jpg";
System.IO.FileInfo fi = new FileInfo(fullFilePath);
if (sender.getProperty("umbracoBytes") != null) sender.getProperty("umbracoBytes").Value = fi.Length.ToString();
if (sender.getProperty("umbracoExtension") != null) sender.getProperty("umbracoExtension").Value = "jpg";
if (sender.getProperty("umbracoWidth") != null) sender.getProperty("umbracoWidth").Value = imgWidth.ToString();
if (sender.getProperty("umbracoHeight") != null) sender.getProperty("umbracoHeight").Value = imgHeight.ToString();
sender.Save();
}
else
{
//If upscaleconflict is detected dispose and do nothing
file.Dispose();
originalBMP.Dispose();
}
}
One more thing. When original image is not jpg the code above fails to update media parameters. Replacing the last part (from the "//delete original file if"-line) with the following should do it :
string originalFilePath = HttpContext.Current.Server.MapPath(sender.getProperty(propertyAlias).Value.ToString());
//Populate upload property and standard Image properties if they exist
sender.getProperty(propertyAlias).Value = "/media/" + mediaFolderName + "/" + fileNameWOExt + ".jpg";
System.IO.FileInfo fi = new FileInfo(fullFilePath);
if (sender.getProperty("umbracoBytes") != null) sender.getProperty("umbracoBytes").Value = fi.Length.ToString();
if (sender.getProperty("umbracoExtension") != null) sender.getProperty("umbracoExtension").Value = "jpg";
if (sender.getProperty("umbracoWidth") != null) sender.getProperty("umbracoWidth").Value = imgWidth.ToString();
if (sender.getProperty("umbracoHeight") != null) sender.getProperty("umbracoHeight").Value = imgHeight.ToString();
sender.Save();
//Delete original file if not jpg
if (ext != "jpg")
{
File.Delete(originalFilePath);
}
Thanks. Got it working.
Would it be possible to make it support png and gif just like jpg? I mean without losing transparency or changing file extensions.
Jeroen Breuer shared some code here [ http://our.umbraco.org/forum/developers/extending-umbraco/31388-Resizing-image-on-upload ] I was playing around with as well, but haven't gotten it to work yet. I believe his maintains the transparency & extension.
Anyway, thanks for the help!
Should be possible yes. Maby I'll make it an option some time. Not sure the code from your link preserves image formats. If I'm not mistaken System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[1] is the jpg encoder, which is the one used in the example.
Anyways. Thnx for trying out the package :) Hope you make it work for you.
You might be right. I'm not too familiar with the image libraries. Anyway, thanks again.Great work!
Just a heads up: Released v1.1 for Umbraco 6. Added an experimental setting that allows you to preserve and resize png's with transparency. Better late than never ;)
is working on a reply...