批量替换图片url链接/批量替换图片的Url地址
1、批量替换链接:
PHP Code复制内容到剪贴板
- // 以下这段为批量替换图片的url链接
- $query = new \yii\db\Query();
- $query->from('yoyocmf_article_data');
- foreach($query->batch() as $articles){
- foreach($articles as $article){
- if(!$article){
- echo "没有了";
- continue;
- }
- $_module = Base::findOne($article['id']);
- $_module->id = $article['id'];
- $content = $article['content'];
- // 替换图片链接
- $content = str_replace("http://soft.ccc:8001/", "http://demo20.liqinwl.com/", $content);
- $_module->content = $content;
- $_module->save();
- echo $article['id'] . " => 更新完成";
- echo "<br/>";
- }
- }
2、批量替换内容中的图片,并抓取远程图片地址 入库:
PHP Code复制内容到剪贴板
- $query = new \yii\db\Query();
- $query->from('yoyocmf_article_data');//->limit(10)->offset(472);
- foreach($query->batch() as $articles){
- if(!$articles){
- echo "没有了";
- continue;
- }
- foreach($articles as $article){
- if(!$article){
- echo "没有了";
- continue;
- }
- $_module = Base::findOne($article['id']);
- $_module->id = $article['id'];
- $content = $article['content'];
- $content = preg_replace_callback('/<img[\s\S]*?src="([\s\S]*?)"[\s\S]*?>/', function ($matches) {
- if (!emptyempty($matches[1])) {
- $imgDir = date('Ymd');
- $res = Attachment::saveImgFromUrl($matches[1],$imgDir);
- $img = Yii::$app->storage->getUrl($res[0]->path);
- return Html::img($img);
- }
- }, $content);
- $_module->content = $content;
- $_module->save();
- echo $article['id'] . " => 更新完成";
- echo "<br/>";
- }
- }
PHP Code复制内容到剪贴板
- /**
- * PHP将网页上的图片攫取到本地存储
- * @param $imgUrl *图片url地址
- * @param string $saveDir 本地存储路径 默认存储在当前路径
- * @param null $fileName 图片存储到本地的文件名
- * @return mix
- */
- public static function saveImgFromUrl($imgUrl, $saveDir = './', $fileName = null)
- {
- if (emptyempty($imgUrl)) {
- return false;
- }
- $hash = md5($imgUrl);
- $attachment = static::findByHash($hash);
- if ($attachment) {
- return [$attachment, null];
- }
- $path = $saveDir;
- $saveDir = Yii::getAlias('@storagePath/upload/') . $saveDir . "/";
- //获取图片信息大小
- $imgSize = getImageSize($imgUrl);
- //p($imgSize);
- if (!in_array($imgSize['mime'], array('image/jpg', 'image/gif', 'image/png', 'image/jpeg'), true)) {
- return false;
- }
- //获取后缀名
- $_mime = explode('/', $imgSize['mime']);
- $_ext = '.' . end($_mime);
- if (emptyempty($fileName)) { //生成唯一的文件名
- $fileName = uniqid(time(), true) . $_ext;
- }
- //开始攫取
- ob_start();
- readfile($imgUrl);
- $imgInfo = ob_get_contents();
- ob_end_clean();
- if (!file_exists($saveDir)) {
- mkdir($saveDir, 0777, true);
- }
- $fp = fopen($saveDir . $fileName, 'a');
- $imgLen = strlen($imgInfo); //计算图片源码大小
- $_inx = 1024; //每次写入1k
- $_time = ceil($imgLen / $_inx);
- for ($i = 0; $i < $_time; $i++) {
- fwrite($fp, substr($imgInfo, $i * $_inx, $_inx));
- }
- fclose($fp);
- $filePath = ($path ? ($path . '/') : '') . $fileName;
- $attachment = new static();
- $attachment->path = $filePath;
- $attachment->name = $fileName;
- $attachment->extension = $_ext;
- $attachment->type = $imgSize['mime'];
- $attachment->size = $imgLen;
- $attachment->hash = $hash;
- $attachment->save();
- return [$attachment, null];;
- }