ImgApp icon indicating copy to clipboard operation
ImgApp copied to clipboard

Feature Request: Resize and convert

Open kenman345 opened this issue 6 years ago • 10 comments

According to this: https://developers.google.com/apps-script/reference/drive/file#getAs(String)

It appears we can get the blob as a different type. I am wondering if we can make doResize optionally always return the image as a passed in mime-type?

I think it can be used when we first get the blob to convert it or alternatively, to make use of this functionality right before returning the new blob when performing the resize.

kenman345 avatar Nov 15 '18 19:11 kenman345

Thank you for your comments. When you want to change the mimeType of the resized image, you can use the following script.

var id = "### file ID of jpeg image ###";
var res = ImgApp.doResize(id, 100);
var pngBlob = res.blob.getAs("image/png");

In this script, jpeg image is resized and converted to png.

Is my understanding of your comment correct?

tanaikech avatar Nov 15 '18 22:11 tanaikech

I am confused about where you are saying that would go, this is currently what I have done with your library and what it returns to achieve the functionality I am looking for.

The following function returns a boolean to indicate if we were successful or not adding the file to a github repository/branch which we've already authenticated to the repository in an earlier function. The comments in it should indicate a lot about what I am doing. The important part is the part where I use the identification information to check if we have a PNG or not. If we do not, then I use .getAs() to get as a png and then save and replace out the file we started with so I can continue with the rest of the processing.

function tryAddImage(url, branch, category, name) {
  var docfile = null;
  var addedImage = false;
  var folder = '';
  var logo = '';
  
  try {
    // Grab File
    var imgFile = UrlFetchApp.fetch(url);
    var imgFilePath = 'img/' +  category.toLowerCase() + '/' + name + '.png';
    var imgBlob = imgFile.getBlob();
    
    // Use ImgApp library to find out the size of the blob
    var imgData = ImgApp.getSize(imgBlob);
    Logger.log(imgData);
    
    // Check that the width and height are equal to eachother
    // If height=width, we can resize the square and use it for the request.
    if(imgData.width == imgData.height) {
		var fileInfo = {
		  title: name + '.' + imgData.identification.toLowerCase(),
		  mimeType: 'image/'+ imgData.identification.toLowerCase(),
		};
		
		docfile = Drive.Files.insert(fileInfo, imgBlob);
		var continueFolderParse = true;
		var fold;
		var folders = DriveApp.getFolders();
		while (folders.hasNext() && continueFolderParse) {
		  fold = folders.next();
		  Logger.log(fold.getName());
		  if (fold == 'ABCBot') {
			folder = fold;
			continueFolderParse = false;
		  }
		}
		logo = DriveApp.getFileById(docfile.id);
		folder.addFile(logo);
		DriveApp.getRootFolder().removeFile(logo);
		
		if (imgData.identification != "PNG" ) {
			imgBlob = logo.getAs('image/png');
			imgData = ImgApp.getSize(imgBlob);
			
			fileInfo = {
			  title: name + '.' + imgData.identification.toLowerCase(),
			  mimeType: 'image/'+ imgData.identification.toLowerCase(),
			};
			
			docfile = Drive.Files.insert(fileInfo, imgBlob);
			var converted = DriveApp.getFileById(docfile.id);
			folder.addFile(converted);
			DriveApp.getRootFolder().removeFile(converted);
			folder.removeFile(logo);
			logo = converted;
		}
		
		var resizedImg = ImgApp.doResize(docfile.id, 32);
		imgBlob = resizedImg.blob.getBytes();
		folder.removeFile(logo);
			
      try { 
        var resp = Github.Repository.createFile(branch, imgFilePath, imgBlob, "Adding image for listing.");
      } catch(e) {
		  // if file exists need to get it's sha and update instead
        var git_file_obj = Github.Repository.getContents({ref: branch}, imgFilePath);
        Github.Repository.updateFile(branch, imgFilePath, imgBlob, git_file_obj.sha, "Updating image for listing.");
      }
      addedImage = true;
    }
  } catch(e) {
    Logger.log(e);
  }
  
  return addedImage;
}

kenman345 avatar Nov 16 '18 13:11 kenman345

I'm really sorry for my poor English skill. I cannot understand about your reply. Can I ask you about the issue of my library?

tanaikech avatar Nov 16 '18 21:11 tanaikech

I want a method where I provide a fileId and a width, and an image type and it returns that resized image in that filetype.

The .getAs([string]) method should be the thing you need to use to accomplish this, building on top of what you already have with your library.

kenman345 avatar Nov 16 '18 21:11 kenman345

In your situation, I think that the resized image can be converted. Are there some issues for this flow?

tanaikech avatar Nov 16 '18 22:11 tanaikech

Issue? No I have it working well, I just wish it was in your library as it could be utilized a lot easier that way and many others might like it too

On Fri, Nov 16, 2018 at 5:05 PM Kanshi TANAIKE [email protected] wrote:

In your situation, I think that the resized image can be converted. Are there some issued for this flow?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/tanaikech/ImgApp/issues/4#issuecomment-439543477, or mute the thread https://github.com/notifications/unsubscribe-auth/AKeM13GgZsI1ubCH9_B6AhU6O_DiULg3ks5uvzaOgaJpZM4Yhwte .

kenman345 avatar Nov 16 '18 22:11 kenman345

You want to propose to include the mimeType conversion to the library. Is my understanding correct? I had thought that there are some bugs in the library. If my understanding is correct, your proposal can be achieved.

tanaikech avatar Nov 16 '18 22:11 tanaikech

It’s working well for me

On Fri, Nov 16, 2018 at 5:12 PM Kanshi TANAIKE [email protected] wrote:

You want to propose to include the mimeType conversion to the library. Is my understanding correct? I had thought that there are some bugs in the library. If my understanding is correct, your proposal can be achieved.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/tanaikech/ImgApp/issues/4#issuecomment-439545189, or mute the thread https://github.com/notifications/unsubscribe-auth/AKeM1wdNUJs-K89ZeWP_48ZRZ26uqbCOks5uvzhfgaJpZM4Yhwte .

kenman345 avatar Nov 16 '18 22:11 kenman345

Can you give me a time to do?

tanaikech avatar Nov 16 '18 22:11 tanaikech

No rush, I have my solution working but having less steps definitely would be nice.

Being able to send in the blob instead of a fileID would also skip steps too, but that is for some other time

On Fri, Nov 16, 2018 at 5:16 PM Kanshi TANAIKE [email protected] wrote:

Can you give me a time to do?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/tanaikech/ImgApp/issues/4#issuecomment-439546068, or mute the thread https://github.com/notifications/unsubscribe-auth/AKeM10kaTRbvlAX5LYANaKwyqPtXpYwYks5uvzlPgaJpZM4Yhwte .

kenman345 avatar Nov 16 '18 22:11 kenman345