How to use the tinify.fromBuffer function in tinify

To help you get started, we’ve selected a few tinify examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Croc-ye / tinyhere / libs / compress.js View on Github external
fs.readFile(name, function(err, sourceData) {
        if (err) throw err;
        tinify.fromBuffer(sourceData).toBuffer(function(err, resultData) {
          if (err) throw err;
          filePath = path.join(filePath, name);
          const writerStream = fs.createWriteStream(filePath);
          // 标记文件末尾
          writerStream.write(resultData,'binary');
          writerStream.end();
      
          // 处理流事件 --> data, end, and error
          writerStream.on('finish', function() {
            failsList.push(null);
            record(name, true, failsList.length, fullLen);
            if (failsList.length === fullLen) {
                finishcb(failsList, filePath);
            }
          });
github semiromid / compress-images / test / node_modules / compress-images / index.js View on Github external
fs.readFile(input, function(err, sourceData) {
              if (err){
                throw err;
              } 
              tinifypng.fromBuffer(sourceData).toBuffer(function(err, resultData) {
                if (err){
                  throw err;
                }
                fs.writeFile(output, resultData, function(err) {
                    if(err) {
                        return console.log(err);
                    }
                    //- - - - - - - - - - - - -  - - - - - - - - - - -
                    //Block statistic
                    //- - - - - - - - - - - - -  - - - - - - - - - - -
                    //Узнаем размер файла после сжатия
                    size_output = getFilesizeInBytes(output);
                        
                    //Находим на сколько процентов удалось сжать файл
                    percent = size_output / size_in;
                    percent = percent * 100;
github semiromid / compress-images / index.js View on Github external
fs.readFile(input, function(err, sourceData) {
                        if(err === null){ 
                              if (err){
                                throw err;
                              } 
                              tinifypng.fromBuffer(sourceData).toBuffer(function(err, resultData) {
                                if (err){
                                  throw err;
                                }
                                fs.writeFile(output, resultData, function(err) {
                                    if(err) {
                                        return console.log(err);
                                    }
                                    //- - - - - - - - - - - - -  - - - - - - - - - - -
                                    //Block statistic
                                    //- - - - - - - - - - - - -  - - - - - - - - - - -
                                    //Узнаем размер файла после сжатия
                                    size_output = getFilesizeInBytes(output);
                                        
                                    //Находим на сколько процентов удалось сжать файл
                                    percent = size_output / size_in;
                                    percent = percent * 100;
github cklwblove / vue-cli3-template / build / tinypng.js View on Github external
fs.readFile(filePathAll, (err, sourceData) => {
          if (err) {
            console.log(chalk.red(`${file} 压缩失败`));
            reject(err);
          } else {
            let fileSize = fs.statSync(filePathAll).size;

            tinify.fromBuffer(sourceData).toBuffer((err, resultData) => {
              if (err) {
                console.log(chalk.red(`${file} 压缩失败`));
                reject(err);
              }
              //将压缩后的文件保存覆盖
              fs.writeFile(filePathAll, resultData, (err) => {
                let compressFileSize = fs.statSync(filePathAll).size;
                console.log(
                  chalk.green(
                    `${file} ${(fileSize / 1024).toFixed(2)}kb 压缩成功 ${(
                      compressFileSize / 1024
                    ).toFixed(2)}kb ${(
                      ((compressFileSize - fileSize) * 100) /
                      fileSize
                    ).toFixed(1)}%`
                  )
github odrick / free-tex-packer-core / FilesProcessor.js View on Github external
static tinifyImage(buffer, options, callback) {
        if(!options.tinify) {
            callback(buffer);
            return;
        }

        tinify.key = options.tinifyKey;

        tinify.fromBuffer(buffer).toBuffer(function(err, result) {
            if (err) throw err;
            callback(result);
        });
    }
}
github odrick / free-tex-packer / electron / main.js View on Github external
ipcMain.on('tinify', (e, data) => {
    tinify.key = data.key;
    tinify.fromBuffer(Buffer.from(data.imageData, 'base64')).toBuffer((err, res) => {
        if (err) {
            e.sender.send('tinify-complete', {
                success: false,
                uid: data.uid,
                error: err.message
            });
            return;
        }
        
        e.sender.send('tinify-complete', {
            success: true,
            uid: data.uid,
            data: res.toString('base64')
        });
    });
});
github muwoo / tinypng-upload / src / renderer / assets / js / operate.js View on Github external
return new Promise((resolve, reject) => {
      let sourceData = fs.readFileSync(path)
      tinify.fromBuffer(sourceData).toBuffer((err, resultData) => {
        if (err instanceof tinify.AccountError) {
          reject(err)
          return new Notification('compress filed', {
            body: 'Verify your API key and account limit.'
          })
        } else if (err instanceof tinify.ServerError) {
          reject(err)
          return new Notification('compress filed', {
            body: 'Temporary issue with the Tinify API.'
          })
        } else if (err instanceof tinify.ConnectionError) {
          reject(err)
          return new Notification('compress filed', {
            body: 'A network connection error occurred.'
          })
        } else {
github skyujilong / tinypng-webpack-plugin / src / compression.js View on Github external
(function tinifyCompress() {
            tinify.key = __keys[0];
            tinify.fromBuffer(fileInfo.data).toBuffer(function(err, resultData) {
                if (err instanceof tinify.AccountError && __keys.length > 1) {
                    //更换秘钥
                    __keys.splice(0, 1);
                    tinifyCompress();
                } else if (err instanceof tinify.ServerError && __serverErrorRetry -- > 0) {
                    tinifyCompress();
                } else if(err){
                    reject(err);
                } else {
                    resolve({
                        dir: fileInfo.dir,
                        compressionData: resultData
                    });
                }
            });
        })();
github skyujilong / tinypng-webpack-plugin / src / uploader.js View on Github external
let compressImg = yield new Promise((resolve, reject) => {
                    tinify.fromBuffer(fileInfo.source.source()).toBuffer((err, resultData) => {
                        if (err) {
                            reject(err);
                        } else {
                            resolve(resultData);
                        }
                    })
                });
                //压缩图片成功

tinify

Node.js client for the Tinify API. Tinify compresses your images intelligently. Read more at https://tinify.com.

MIT
Latest version published 2 years ago

Package Health Score

50 / 100
Full package analysis