php 图片本地化

<?php
/**
 * Created by PhpStorm.
 * User: root
 * Date: 2019/12/17
 * Time: 13:52
 */

class download_image{
    public function downloadImages($url,$frequency="")
    {
        $header = array("Connection: Keep-Alive", "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Pragma: no-cache", "Accept-Language: zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3", "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:29.0) Gecko/20100101 Firefox/29.0");

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);

        //curl_setopt($ch, CURLOPT_HEADER, $v);

        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

        curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');

        $content = curl_exec($ch);

        $curlinfo = curl_getinfo($ch);
        //print_r($curlinfo);

        //关闭连接

        curl_close($ch);

        if ($curlinfo['http_code'] == 200) {

            if ($curlinfo['content_type'] == 'image/jpeg') {

                $exf = '.jpg';

            } else if ($curlinfo['content_type'] == 'image/png') {

                $exf = '.png';

            } else if ($curlinfo['content_type'] == 'image/gif') {

                $exf = '.gif';

            }

            //存放图片的路径及图片名称  *****这里注意 你的文件夹是否有创建文件的权限 chomd -R 777 mywenjian

            $array = explode("/",$curlinfo['url']);

            $images_name = array_slice($array,-1,1);
            $path = "Update/";
            $path .= date('Ymd',time())."/";

            if(!file_exists($path))//文件夹不存在,先生成文件夹
            {
                mkdir($path);
            }
            $images_name = md5(time().$images_name[0].$frequency).$exf;

            $filepath = $path.$images_name;

            $content = file_get_contents($url);
            //下载图片
            file_put_contents($filepath, $content);

            //$res = file_put_contents($filename, $content);//同样这里就可以改为$res = file_put_contents($filepath, $content);

            return $filepath;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

执念、很无奈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值