I am trying to resize the images uploaded to my container in order to create thumbnails and various other versions of my images for my site.
The images I upload have to correct content-type "image/jpeg" but when I create a new version of them using the code below it turns out as "application/octet-stream".
What am I missing here?
using ImageResizer;
using ImageResizer.ExtensionMethods;
public static void Run(Stream myBlob, string blobname, string blobextension, Stream outputBlob, TraceWriter log)
{
log.Info($"C# Blob trigger function Processed blob\n Name:{blobname} \n Size: {myBlob.Length} Bytes");
var instructions = new Instructions
{
Width = 570,
Mode = FitMode.Crop,
Scale = ScaleMode.Both,
};
ImageBuilder.Current.Build(new ImageJob(myBlob, outputBlob, instructions));
}
Edit: The solution.
#r "Microsoft.WindowsAzure.Storage"
using ImageResizer;
using ImageResizer.ExtensionMethods;
using Microsoft.WindowsAzure.Storage.Blob;
public static void Run(Stream myBlob, string blobname, string blobextension, CloudBlockBlob outputBlob, TraceWriter log)
{
log.Info($"C# Blob trigger function Processed blob\n Name:{blobname} \n Size: {myBlob.Length} Bytes");
var instructions = new Instructions
{
Width = 570,
Mode = FitMode.Crop,
Scale = ScaleMode.Both
};
Stream stream = new MemoryStream();
ImageBuilder.Current.Build(new ImageJob(myBlob, stream, instructions));
stream.Seek(0, SeekOrigin.Begin);
outputBlob.Properties.ContentType = "image/jpeg";
outputBlob.UploadFromStream(stream);
}
When you use the stream output, functions will default your content-type to application/octet-stream.
Use one of the ICloudBlob types, which should allow you to specify the content type of your blob.
Here's a cheatsheet of types you can bind to as parameters: https://jhaleyfiles2016.blob.core.windows.net/public/Azure%20WebJobs%20SDK%20Cheat%20Sheet%202014.pdf
Here's a complete working example of an image-resizing Azure Function:
run.csx
#r "Microsoft.WindowsAzure.Storage"
using Microsoft.WindowsAzure.Storage.Blob;
using ImageResizer;
using ImageResizer.ExtensionMethods;
public static void Run(Stream imageStream, string blobName, CloudBlockBlob outputBlob, TraceWriter log)
{
log.Info($"Function triggered by blob\n Name:{blobName} \n Size: {imageStream.Length} Bytes");
var instructions = new Instructions
{
Width = 400,
Height = 350,
Mode = FitMode.Max,
OutputFormat = OutputFormat.Jpeg,
JpegQuality = 85
};
using (var outputStream = new MemoryStream())
{
ImageBuilder.Current.Build(new ImageJob(imageStream, outputStream, instructions));
outputStream.Position = 0;
outputBlob.Properties.ContentType = "image/jpeg";
outputBlob.UploadFromStream(outputStream);
}
}
function.json
{
"bindings": [
{
"name": "imageStream",
"type": "blobTrigger",
"direction": "in",
"path": "watched-container/{blobName}.jpg",
"connection": "AzureWebJobsDashboard"
},
{
"type": "blob",
"name": "outputBlob",
"path": "output-container/{blobName}.jpg",
"connection": "AzureWebJobsDashboard",
"direction": "inout"
}
],
"disabled": false
}
project.json
{
"frameworks": {
"net46":{
"dependencies": {
"ImageResizer": "4.0.5"
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With