Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Resize and convert #4

Open
kenman345 opened this issue Nov 15, 2018 · 10 comments
Open

Feature Request: Resize and convert #4

kenman345 opened this issue Nov 15, 2018 · 10 comments

Comments

@kenman345
Copy link

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.

@tanaikech
Copy link
Owner

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?

@kenman345
Copy link
Author

kenman345 commented Nov 16, 2018

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;
}

@tanaikech
Copy link
Owner

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?

@kenman345
Copy link
Author

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.

@tanaikech
Copy link
Owner

tanaikech commented Nov 16, 2018

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

@kenman345
Copy link
Author

kenman345 commented Nov 16, 2018 via email

@tanaikech
Copy link
Owner

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.

@kenman345
Copy link
Author

kenman345 commented Nov 16, 2018 via email

@tanaikech
Copy link
Owner

Can you give me a time to do?

@kenman345
Copy link
Author

kenman345 commented Nov 16, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants