[英]Image rotation in Angular4
I am trying to rotate am image before uploading to my server. 我正在尝试旋转图像,然后再上传到服务器。 The steps I took so far are: 到目前为止,我采取的步骤是:
But I am not able to convert it back into image format. 但是我无法将其转换回图像格式。 The final image file (after rotation) is not able to upload because it is a blob. 最终图像文件(旋转后)无法上传,因为它是斑点。
Could you please tell me how to convert my rotated Base64 string to image file so that I can create a blob and upload it . 您能告诉我如何将旋转的Base64字符串转换为图像文件,以便创建一个Blob并上传它。
I guess my rotation function is not proper either as I tried to convert it and took the converted base64 string to view on https://codebeautify.org/base64-to-image-converter and it gave me a blank file (black image file). 我想我的旋转功能不合适,因为我尝试对其进行转换,并使用转换后的base64字符串在https://codebeautify.org/base64-to-image-converter上查看,它给了我一个空白文件(黑色图像文件) )。
The origin of this problem is when user clicks a image on iOS or Android and uploads it the image appears sideways. 此问题的根源是当用户在iOS或Android上单击图像并将其上传时,图像出现在侧面。 To fix this I am trying to rotate the image according to its EXIF orientation. 为了解决这个问题,我试图根据图像的EXIF方向旋转图像。
function detectFiles(event) {
this.getOrientation(event.target.files[0], function (orientation) {
this.imageOrientation = orientation;
}.bind(this));
console.log('the original image data', event.target.files);
this.selectedFiles = event.target.files;
// SAS : Converting the selected image to Base64 for rotation.
const reader = new FileReader();
reader.onloadend = (e) => {
this.base64Data = reader.result;
const rotatedData = this.rotateBase64Image(this.base64Data);
// SAS: Calling the data uri to blob
const selFile = this.dataURItoBlob(rotatedData);
this.uploadFiles(selFile);
}
reader.readAsDataURL(event.target.files[0]);
}
// SAS: Rotate the image.
function rotateBase64Image(base64ImageSrc) {
const canvas = document.createElement('canvas');
const img = new Image();
img.onload = function () {
canvas.width = img.width;
canvas.height = img.height;
console.log('image height and width', canvas.width , canvas.height);
}
img.src = base64ImageSrc;
const context = canvas.getContext('2d');
context.translate((img.width), (img.height));
// context.rotate(180 * (Math.PI / 180));
context.rotate(90);
context.drawImage(img, 0, 0);
console.log(canvas);
console.log('the rotated image', canvas.toDataURL());
return canvas.toDataURL();
}
// SAS: Data URI to Blob
function dataURItoBlob(dataURI) {
// convert base64 to raw binary data held in a string
// doesn't handle URLEncoded DataURIs
const byteString = atob(dataURI.split(',')[1]);
// separate out the mime component
const mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
// write the bytes of the string to an ArrayBuffer
const ab = new ArrayBuffer(byteString.length);
// create a view into the buffer
const ia = new Uint8Array(ab);
// set the bytes of the buffer to the correct values
for (let i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
// write the ArrayBuffer to a blob, and you're done
const blob = new Blob([ab], {type: mimeString});
console.log('the value of the blob', blob);
return blob;
}
And After this I am trying to upload the data using the below function which accepts the image file. 然后,我尝试使用下面接受图像文件的功能上传数据。
uploadFiles(selFile) {
// SAS: Setting the 'image uploaded flag' to be retrieved in quick post to prevent duplicate placeholders.
// const file = this.selectedFiles.item(0);
const file = selFile;
this.currentUpload = new Upload(file);
// This is the API call to upload the file.
this.storageApiService.createBlob(file).subscribe((response) => {
console.log(response);
}, (error) => {
console.log(error);
});
}
}
You could take care of the exif rotation for them so they don't have to rotate the image 您可以为他们处理exif旋转,这样他们就不必旋转图像
I made a library to never have to use the filereader called screw-filereader with a extra bonus of creating image from blobs very easily by just calling blob.image().then(success, fail)
and it will take care of the images exif rotation for you. 我创建了一个库,从不需要使用名为screw-filereader的文件读取器 ,只需通过调用blob.image().then(success, fail)
就可以很容易地从blob创建图像,这将额外照顾图像exif为您旋转。
// unrelevant code (just to get a rotated image with exif metadata) var exifImage = 'https://cors-anywhere.herokuapp.com/http://duckz.serveblog.net/risorse/foto/francia/P1831_21-02-10.jpg' fetch(exifImage) .then(res => res.blob()) .then(blob => { // just to show the problem you have with your image const objectURL = URL.createObjectURL(blob) const img = new Image() img.src = objectURL document.body.appendChild(img) // what screw-filereader can do (auto rotate based on exif data) // will resolve at img.onload event with a new img element // will also cast a error if blob isn't a image (img.onerror) // this is what you need... blob.image().then(img => { // convert image back to blob var canvas = document.createElement('canvas') canvas.width = img.width canvas.height = img.height const ctx = canvas.getContext('2d') ctx.drawImage(img, 0, 0) canvas.toBlob(uploadFiles) // just to show the rotated image document.body.appendChild(img) }) }) function uploadFiles(blob) { // upload blob }
img { width: 100px }
<script src="https://cdn.jsdelivr.net/npm/screw-filereader@1.4.3/index.min.js"></script>
Bonus tip, use canvas.toBlob instead of using dataURItoBlob 温馨提示,请使用canvas.toBlob而不是dataURItoBlob
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.