ecshop百度编辑器远程下载无后缀的图片,并且加水印
生活随笔
收集整理的這篇文章主要介紹了
ecshop百度编辑器远程下载无后缀的图片,并且加水印
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<?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 startdefine('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 endarray_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
總結
以上是生活随笔為你收集整理的ecshop百度编辑器远程下载无后缀的图片,并且加水印的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mysql5.X重点难点速记
- 下一篇: 5.34. PECL FAQ