dongliechuich10319 2014-04-08 09:25
浏览 29

如何将图像文件保存在目录中,而不是从微小的png api提供的链接下载

I have implemented a PHP code from tiny png API in which i want to compress my PNG file. But as i an executing this code it is providing me the link to download those file. Instead i want all those images file to be saved automatically to my specified directory.

$key = "tiny png api key";
$input = "images2.png";
$output = "images2.png";

$request = curl_init();
curl_setopt_array($request, array(
CURLOPT_URL => "https://api.tinypng.com/shrink",
CURLOPT_USERPWD => "api:" . $key,
CURLOPT_POSTFIELDS => file_get_contents($input),
CURLOPT_BINARYTRANSFER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
/* Uncomment below if you have trouble validating our SSL certificate.
 Download cacert.pem from: http://curl.haxx.se/ca/cacert.pem */
// CURLOPT_CAINFO => __DIR__ . "/cacert.pem",
 CURLOPT_SSL_VERIFYPEER => true
 ));

$response = curl_exec($request);
echo $response."<BR>";
if (curl_getinfo($request, CURLINFO_HTTP_CODE) === 201) {
/* Compression was successful, retrieve output from Location header. */
$headers = substr($response, 0, curl_getinfo($request, CURLINFO_HEADER_SIZE));
foreach (explode("
", $headers) as $header) {
if (substr($header, 0, 10) === "Location: ") {
   $request = curl_init();
   curl_setopt_array($request, array(
   CURLOPT_URL => substr($header, 10),
   CURLOPT_RETURNTRANSFER => true,
   /* Uncomment below if you have trouble validating our SSL certificate. */
   // CURLOPT_CAINFO => __DIR__ . "/cacert.pem",
    CURLOPT_SSL_VERIFYPEER => true
  ));
  file_put_contents($output, curl_exec($request));
 }
 }
 } else {
 print(curl_error($request));
 /* Something went wrong! */
 print("Compression failed");
 }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 shape_predictor_68_face_landmarks.dat
    • ¥15 slam rangenet++配置
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料
    • ¥15 使用R语言marginaleffects包进行边际效应图绘制
    • ¥20 usb设备兼容性问题