powerbot15 / image-compressor Goto Github PK
View Code? Open in Web Editor NEWFrontend javascript module for resizing and compressing images
Frontend javascript module for resizing and compressing images
Im trying to integrate this into one of my GatsbyJS component. but got the SecurityError.
import React from 'react'
import Link from 'gatsby-link'
import NotFound from '../assets/images/image-not-found.png'
import Moment from 'react-moment'
const localServer = "http://localhost:1337"
var imageCompressor = new ImageCompressor;
var compressorSettings = {
toWidth : 100,
toHeight : 100,
mimeType : 'image/png',
mode : 'strict',
quality : 0.6,
grayScale : true,
sepia : true,
threshold : 127,
vReverse : true,
hReverse : true,
speed : 'low'
};
const NewsSection = ({data}) => {
function renderImage(imgSrc) {
return imageCompressor.run(imgSrc, compressorSettings, (compressedSrc) => {
console.log(imgSrc)
})
}
return (
<section className="features" id="news">
<div className="container team-container">
<div className="row">
<div className="col-lg-12 text-center">
<div className="navy-line teal-color"></div>
<h1>News</h1>
</div>
</div>
{ data.map(document => (
<div className="row blog-post">
<div className="col-sm-12 col-md-4">
<img
src={document.node.image ? renderImage(localServer + document.node.image.url) : NotFound }
alt=""
className="img-fluid"
/>
</div>
<div className="col-sm-12 col-md-8">
<h2>{document.node.headline}</h2>
<div className="news-credit">
<strong className="commens-name">{document.node.publisher}</strong>
<span style={{marginLeft: '5px'}}/>
<small className="text-muted">Publication date
<span style={{marginLeft: '5px'}}/>
<Moment parse="YYYY-MM-DD HH:mm:ss">
{document.node.publish_date}
</Moment>
</small>
</div>
<hr/>
<p>{document.node.content}</p>
<a href={document.node.post_link} target="blank">
<button className="btn btn-primary">Read More</button>
</a>
</div>
</div>
))
}
</div>
</section>
)
}
export default NewsSection
I got this error when I compressing my image
Uncaught DOMException: Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The canvas has been tainted by cross-origin data.
at ImageCompressor.grayScale (file:///D:/Project/node_modules/image-compressor/image-compressor.js:164:40)
at ImageCompressor.imageFilters (file:///D:/Project/node_modules/image-compressor/image-compressor.js:212:22)
at ImageCompressor.compressAddOn (file:///D:/Project/node_modules/image-compressor/image-compressor.js:74:18)
at ImageCompressor.compress (file:///D:/Project/node_modules/image-compressor/image-compressor.js:59:18)
at ImageCompressor.imageLoaded (file:///D:/Project/node_modules/image-compressor/image-compressor.js:34:18)
with this:
var imageCompressor = new ImageCompressor();
var compressorSettings = {
toWidth : 100,
toHeight : 100,
mimeType : 'image/jpg',
mode : 'strict',
quality : 0.6,
grayScale : true,
sepia : true,
threshold : 127,
vReverse : true,
hReverse : true,
speed : 'high'
};
window.addEventListener("load",e=>{
imageCompressor.run("test.jpg", compressorSettings, proceedCompressedImage);
})
function proceedCompressedImage (compressedSrc) {
console.log(compressedSrc)
}
what is this happend? X(
quality parameter values between 0.1 to 0.9 doesn't seem to work(for mime type : PNG). Not even in the provided Demo. I've tried rendering images with parameters 0.1 and 0.9 , both of them returned images of same size. Please help @powerbot15
I write this library in client side but it throws error also i added the script for imagecompressor
<script src="node_modules/image-compressor/image-compressor.js"></script>
Let me know if any.
I want to use this module in react. However, I am getting error Error: Source can not be loaded. I installed via npm install image-compressor
import "image-compressor";
var ImageCompressor = new window.ImageCompressor();
var compressorSettings = {
toWidth: 100,
toHeight: 100,
mimeType: "image/png",
mode: "strict",
quality: 0.6,
grayScale: true,
sepia: true,
threshold: 127,
vReverse: true,
hReverse: true,
speed: "low"
};
const proceedCompressedImage = compressedSrc => {
console.log("Compressed Src: ", compressedSrc);
};
export const compressImg = imageSrc => {
ImageCompressor.run(imageSrc, compressorSettings, proceedCompressedImage);
};
I installed npm image-compressor.
my code is
router.put('/person', function(req, res, next) {
if (req.access === false) {
res.send(401);
}
var encodedImage=""
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
var fileSize = files.attachment.size;
if(fileSize!=0){
var filePath = files.attachment.path;
var fileName = files.attachment.name;
var fileType = files.attachment.type;
encodedImage = imageBase64.local(filePath);
var imageCompressor = new ImageCompressor;
var compressorSettings = {
toWidth : 100,
toHeight : 100,
mimeType : 'image/png',
mode : 'strict',
quality : 0.6,
speed : 'low'
};
imageCompressor.run(filePath, compressorSettings, .proceedCompressedImage());
}
function proceedCompressedImage(compressedsrc){
console.log("---==-=-=-=-==-=-="+compressedsrc)
}
How do i solve this issue ?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.