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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用
    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害