ecshop百度编辑器远程下载无后缀的图片,并且加水印

作者:Abner3721 时间:2023-08-14 17:31:41 

<?php

    /**

     * Created by JetBrains PhpStorm.

     * User: taoqili

     * Date: 11-12-28

     * Time: 上午9:54

     * To change this template use File | Settings | File Templates.

     */

    header("Content-Type: text/html; charset=utf-8");

    error_reporting(E_ERROR|E_WARNING);

    //远程抓取图片配置

    $config = array(

        "savePath" => "../../../bdimages/upload1/" ,            //保存路径

        "allowFiles" => array( ".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp" ) , //文件允许格式

        "maxSize" => 3000                    //文件大小限制,单位KB

    );

    $uri = htmlspecialchars( $_POST[ 'upfile' ] );

    $uri = str_replace( "&" , "&" , $uri );

    getRemoteImage( $uri,$config );


    /**

     * 远程抓取

     * @param $uri

     * @param $config

     */

    function getRemoteImage( $uri,$config)

    {

        $tempimgUrl= '';

        //忽略抓取时间限制

        set_time_limit( 0 ); 

        //ue_separate_ue  ue用于传递数据分割符号

        $imgUrls = explode( "ue_separate_ue" , $uri );

        $tmpNames = array();

        foreach ( $imgUrls as $imgUrl ) {  

            //http开头验证

            if(strpos($imgUrl,"http")!==0){

                array_push( $tmpNames , "error" );

                continue;

            }

            //获取请求头

            $heads = get_headers( $imgUrl );

            //死链检测

            if ( !( stristr( $heads[ 0 ] , "200" ) && stristr( $heads[ 0 ] , "OK" ) ) ) {

                array_push( $tmpNames , "error" );

                continue;

            }

             

            

            //格式验证(扩展名验证和Content-Type验证)

            $ext = strtolower( strrchr( $imgUrl , '.' ) );

            //当文件后缀不能存,并且是图片类型的,自动添加文件后缀

            $imgAttr = get_headers( $imgUrl, true );

            $imgtype = array('image/png', 'image/jpeg', 'image/gif');

            

            if( !in_array( $ext, $config[ 'allowFiles' ] ) && in_array($imgAttr['Content-Type'],  $imgtype)){ 


                switch( $imgAttr['Content-Type'] )

                {

                    case 'image/png' :

                        $ext = '.png';

                        break;

                    case 'image/jpeg' :

                        $ext = '.jpg';

                        break;

                    case 'image/gif' :

                        $ext = '.gif';

                        break;

                    default:

                        $ext = '.jpg';

                }

                

                //读取远程无后缀的图片内容

                $tempimgUrl = @file_get_contents( $imgUrl);

                $imgUrl = $imgUrl.$ext;

            }



            

            $fileType = $ext;

            if ( !in_array( $fileType , $config[ 'allowFiles' ] ) || stristr( $heads[ 'Content-Type' ] , "image" ) ) {

                array_push( $tmpNames , "error" );

                continue;

            }  

            //打开输出缓冲区并获取远程图片

            ob_start();

            $context = stream_context_create(

                array (

                    'http' => array (

                        'follow_location' => false // don't follow redirects

                    )

                )

            );

            //请确保php.ini中的fopen wrappers已经激活

            readfile( $imgUrl,false,$context);

            //当无后缀图片时,用读出来的数据

            $img = !empty($tempimgUrl) ? $tempimgUrl : ob_get_contents();

            ob_end_clean();


            //大小验证

            $uriSize = strlen( $img ); //得到图片大小

            $allowSize = 1024 * $config[ 'maxSize' ];

            if ( $uriSize > $allowSize ) {

                array_push( $tmpNames , "error" );

                continue;

            }

            //创建保存位置

            $savePath = $config[ 'savePath' ].date('Ymd', time()).'/';

            if ( !file_exists( $savePath ) ) {

                mkdir( "$savePath" , 0777 );

            }

            //写入文件

            $tmpName = $savePath . rand( 1 , 10000 ) . time() . strrchr( $imgUrl , '.' );

            try {

                $fp2 = @fopen( $tmpName , "a" );

                fwrite( $fp2 , $img );

                fclose( $fp2 );  

                

                //添加水印,载入水印需要的文件 @author Abner @time 20171221 start

                define('IN_ECS', true);

                define('INIT_NO_USERS', true);

                require_once( '../../init.php');

                require_once( '../../cls_image.php');

                $image = new cls_image(); 

                if (intval($_CFG['watermark_place']) > 0 && !empty($_CFG['watermark']))

                { 

                    $image->add_watermark( $tmpName , '', '../../'.$_CFG['watermark'], $_CFG['watermark_place'], $_CFG['watermark_alpha']);

                    

                }

                //添加水印,载入水印需要的文件 @author Abner @time 20171221 end

                 

                array_push( $tmpNames ,  $tmpName );

            } catch ( Exception $e ) {

                array_push( $tmpNames , "error" );

            }

        }

        /**

         * 返回数据格式

         * {

         *   'url'   : '新地址一ue_separate_ue新地址二ue_separate_ue新地址三',

         *   'srcUrl': '原始地址一ue_separate_ue原始地址二ue_separate_ue原始地址三',

         *   'tip'   : '状态提示'

         * }

         */

        

        echo "{'url':'" . implode( "ue_separate_ue" , $tmpNames ) . "','tip':'远程图片抓取成功!','srcUrl':'" . $uri . "'}";

    }

来源:https://www.cnblogs.com/Abner3721/p/8118686.html

标签:
0
投稿

猜你喜欢

  • Python中tkinter+MySQL实现增删改查

    2024-01-20 06:49:59
  • 一个Access数据库数据传递的实例方法

    2008-11-28 16:24:00
  • oracle 字符串转成行

    2009-06-19 17:38:00
  • Array.prototype.slice

    2010-05-07 12:43:00
  • pandas创建DataFrame对象失败的解决方法

    2022-06-23 17:12:54
  • Python用requests库爬取返回为空的解决办法

    2021-10-30 04:54:29
  • python爬虫基础之urllib的使用

    2022-02-10 19:01:18
  • vue2项目中封装echarts地图的优雅方法

    2024-05-13 09:44:55
  • pycharm sciview的图片另存为操作

    2022-09-26 08:03:07
  • Python爬虫基础讲解之scrapy框架

    2023-11-16 00:07:31
  • python 插入日期数据到Oracle实例

    2022-09-29 23:51:59
  • Python脚本实现下载合并SAE日志

    2023-04-13 06:41:41
  • MySQL Cluster集群的初级部署教程

    2024-01-22 01:50:16
  • MySQL找出未提交事务的SQL实例浅析

    2024-01-23 04:08:25
  • Django框架实现在线考试系统的示例代码

    2021-05-24 23:07:00
  • 教你利用python如何读取txt中的数据

    2023-04-03 14:52:36
  • Python时间和字符串转换操作实例分析

    2023-04-15 22:58:08
  • MySQL配置文件my.cnf中文版

    2011-09-30 11:06:15
  • python正则表达式去除两个特殊字符间的内容方法

    2023-08-24 16:22:10
  • 解决Alexnet训练模型在每个epoch中准确率和loss都会一升一降问题

    2022-12-06 16:17:37
  • asp之家 网络编程 m.aspxhome.com