Skip to content

Commit

Permalink
Fixed issue where PNG files wouldn't be optimized anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
devedse committed Jul 26, 2017
1 parent 3791639 commit 530c767
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
13 changes: 11 additions & 2 deletions DeveImageOptimizer/FileProcessing/FileOptimizerProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DeveImageOptimizer.Helpers;
using ExifLibrary;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
Expand All @@ -25,7 +26,12 @@ public async Task<bool> OptimizeFile(string fileToOptimize, bool saveFailedOptim

await AsyncFileHelper.CopyFileAsync(fileToOptimize, tempFilePath, true);

var oldRotation = await ExifImageRotator.UnrotateImageAsync(tempFilePath);
Orientation jpegFileOrientation = Orientation.Normal;
bool shouldUseJpgWorkaround = FileTypeHelper.IsJpgFile(tempFilePath);
if (shouldUseJpgWorkaround)
{
jpegFileOrientation = await ExifImageRotator.UnrotateImageAsync(tempFilePath);
}

var processStartInfo = new ProcessStartInfo(_pathToFileOptimizer, tempFilePath)
{
Expand All @@ -36,7 +42,10 @@ public async Task<bool> OptimizeFile(string fileToOptimize, bool saveFailedOptim

await ProcessRunner.RunProcessAsync(processStartInfo);

await ExifImageRotator.RerotateImageAsync(tempFilePath, oldRotation);
if (shouldUseJpgWorkaround)
{
await ExifImageRotator.RerotateImageAsync(tempFilePath, jpegFileOrientation);
}

var imagesEqual = await ImageComparer2.AreImagesEqualAsync(fileToOptimize, tempFilePath);

Expand Down
20 changes: 20 additions & 0 deletions DeveImageOptimizer/Helpers/FileTypeHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace DeveImageOptimizer.Helpers
{
public static class FileTypeHelper
{
public static bool IsJpgFile(string file)
{
var extension = Path.GetExtension(file);
if (string.Equals(extension, ".jpg", StringComparison.OrdinalIgnoreCase) || string.Equals(extension, ".jpeg", StringComparison.OrdinalIgnoreCase))
{
return true;
}
return false;
}
}
}
3 changes: 1 addition & 2 deletions DeveImageOptimizer/Helpers/ImageComparer2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ private static string GetRotatedAmount(Image<Rgba32> source)

private static Image<Rgba32> LoadImageHelper(string imagePath, List<string> tempFiles, bool useImageSharpBugWorkaround)
{
var imageExtension = Path.GetExtension(imagePath);
if (useImageSharpBugWorkaround && string.Equals(imageExtension, ".jpg", StringComparison.OrdinalIgnoreCase) || string.Equals(imageExtension, ".jpeg", StringComparison.OrdinalIgnoreCase))
if (useImageSharpBugWorkaround && FileTypeHelper.IsJpgFile(imagePath))
{
var imageAsPngPath = ImageConverter.ConvertJpgToPngWithAutoRotate(imagePath);
tempFiles.Add(imageAsPngPath);
Expand Down

0 comments on commit 530c767

Please sign in to comment.