# Resizing a JPG file (e.g. filename.jpg -> resized.jpg) curl -X POST -s --form "op=fixedWidth&fixedWidth=200&input=@filename.jpg;type=image/jpg" https://www.toptal.com/developers/img-resize/resize > resized.jpg
// core
var fs = require('fs');
// from npm
var superagent = require('superagent');
// open the output file
var outStream = fs.createWriteStream('resized.jpg');
// do the request
var req = superagent
.post('https://www.toptal.com/developers/img-resize/resize')
.attach('input', 'filename.jpg')
;
// save the returned file
req.end(function(res) {
res.pipe(outStream);
});
Many thanks toArjan Haverkamp for this example.
function ImgResize($JPGfile, &$error = '')
{
$ch = curl_init('https://www.toptal.com/developers/img-resize/resize');
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('input' => '@'.$JPGfile));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jpg = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($status !== 200) {
$error = 'https://www.toptal.com/developers/img-resize/ request failed: HTTP code ' . $status;
return false;
}
$curl_error = curl_error($ch);
if (!empty($curl_error)) {
$error = 'https://www.toptal.com/developers/img-resize/ request failed: CURL error ' . $curl_error;
return false;
}
curl_close($ch);
return $jpg;
}
$result = ImgResize('input.jpg', $error);
if (false === $result) { die("{$error}\n"); }
file_put_contents('crushed.jpg', $result);