Hongmu Notes
Home Program Notes EmpireCMS – Random Article Thumbnail Generation and Article-Embedded Image Replacement
Program Notes Empire cms

EmpireCMS – Random Article Thumbnail Generation and Article-Embedded Image Replacement

EmpireCMS – Random Article Thumbnail Generation and Article-Embedded Image Replacement

I collected hundreds of thousands of articles, only to discover that none of them had thumbnails – which made the whole thing look absolutely hideous!!

So, just use your little hand to write some code that generates random thumbnails – and assign a thumbnail to every article!

code

 隐藏内容:会员可查看
<?php
$biao = "phome_";//表前缀
$fbiao = "news";//什么表

require('e/class/connect.php');        //引入数据库配置文件和公共函数文件
require('e/class/db_sql.php');        //引入数据库操作文件
$link=db_connect();                //连接MYSQL
$empire=new mysqlquery();        //声明数据库操作类

// 检查是否第一次执行脚本
if (!isset($_COOKIE['mycookie'])) {
    setcookie('mycookie', '1', time() + 86400, '/');
    $empire->query("UPDATE `{$biao}ecms_{$fbiao}` SET `titlepic` = ''");
}

$sql=$empire->query("SELECT id FROM `{$biao}ecms_{$fbiao}` WHERE `titlepic` = ' ' ORDER BY `id` ASClimit 100");
while($r=$empire->fetch($sql))        //循环获取查询记录
{
    $r2 = json_decode(file_get_contents("http://api.4s5.cn/Img/api.php"),true);
    if($r2['code']==200){
        $empire->query("UPDATE `{$biao}ecms_{$fbiao}` SET `titlepic` = '{$r2['img']}' WHERE `id` = {$r['id']}");
        echo "成功".$r['id']."》》{$r2['img']}<br>";
    }else{
        echo "失败".$r['id']."》》<br>".print_r($r2);
    }
}
echo '<meta http-equiv="refresh" content="0"/>';

db_close();                        //关闭MYSQL链接
$empire=null;                        //注消操作类变量
?>

Second requirement

Although the article includes an thumbnail, however!

The images collected in this article have防盗链设置 on other websites; as a result, they simply won't display on my website!

If we go with manual processing – that would cost hundreds of thousands – or with automatic processing – would that require the locomotive to collect data all over again? That would be too much hassle!

So, I'll give my little hands another try!

Write a piece of code that randomly replaces images within an article!

Match the image links in the article and then replace them with the specified image links!

This solves the issue where various images within an article fail to display!

 隐藏内容:会员可查看
<?php
$biao = "phome_";//表前缀
$fbiao = "news";//什么表
require('e/class/connect.php');        //引入数据库配置文件和公共函数文件
require('e/class/db_sql.php');        //引入数据库操作文件
$link=db_connect();                //连接MYSQL
$empire=new mysqlquery();        //声明数据库操作类

$id = empty($_GET['next'])?0:$_GET['next'];

$sql=$empire->query("SELECT id,stb FROM `{$biao}ecms_{$fbiao}` WHERE `id` >= {$id} ORDER BY `id` ASC limit 100");        //查询新闻表最新10条记录
while($r=$empire->fetch($sql))        //循环获取查询记录
{
    $img = json_decode(file_get_contents("http://api.4s5.cn/Img/api.php?num=50"),true);
    if($img['code']==200){
        $r2 = $empire->fetch1("SELECT id,newstext FROM `{$biao}ecms_{$fbiao}_data_{$r['stb']}` WHERE `id` = {$r['id']} ");
        $newstext = randomReplaceImageLinks(stripslashes($r2['newstext']),$img['img'],0);
        $empire->query("UPDATE `{$biao}ecms_{$fbiao}_data_{$r['stb']}` SET `newstext` = '".addslashes($newstext)."' WHERE `id` = {$r['id']}");
        echo "成功".$r['id']."》》<br>";
    }else{
        echo "失败".$r['id']."》》<br>";
    }
    $id=$r['id'];
}
echo '<meta http-equiv="refresh" content="1;url=http://'.$_SERVER['HTTP_HOST'].''.$_SERVER['SCRIPT_NAME'].'?next='.$id.'">';

db_close();                        //关闭MYSQL链接
$empire=null;                        //注消操作类变量

//type 0为替换img全部,1为只替换链接,默认替换链接
function randomReplaceImageLinks($content, $imageLinks, $type = 1) {
  // 匹配 img 标签中的 src 属性
  $pattern = '/<img.+?src=[\'"](?P<src>.+?)[\'"].*?>/i';

  // 获取所有匹配到的图片链接
  preg_match_all($pattern, $content, $matches);

  if (empty($matches[$type])) {
    return $content;
  }
  
  // 遍历所有匹配到的图片链接,用随机选择的图片链接替换
  foreach ($matches[$type] as $oldImageLink) {
    // 随机选择一个图片链接
    $randomIndex = array_rand($imageLinks);
    $newImageLink = $imageLinks[$randomIndex];
    $newImageLink = $type==0?'<img src="' . $newImageLink . '">':$newImageLink;
    $content = str_replace($oldImageLink, $newImageLink, $content);
  }

  return $content;
}

How do I use the code?

It's actually quite simple!

Create a file named 1.php in the root directory of your server, enter the code inside it, and then simply open this file in your web browser!

Don't know how to use it? Hehe! (* ̄︶ ̄*) – I'll help you for a fee!

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

Empire CMS database statement & SQL statement format

Empire CMS database statement & SQL statement format Program Notes Empire cms

Introduction to Imperial CMS extended SQL program writing, basic examples of Imperial CMS database statements & SQL statement formats: Note: The following examples are based on placing PHP files in the system root directory. Example 1: Connect to MYSQL program. (a.php)<?php require('e/class/connect.php'); //Introduce database configuration files and public function files requ…
👁 351
Empire CMS obtains the system COOKIE variable function getcvar()

Empire CMS obtains the system COOKIE variable function getcvar() Program Notes Empire cms

Get the system COOKIE variable function syntax: getcvar($var,$ecms) Description: $var: is the variable name $ecms: 0 is to set the foreground COOKIE variable, 1 is to set the background COOKIE variable. This parameter can be omitted and defaults to 0. Usage example: getcvar('mlusername'), get the user name of the front-end login member getcvar('loginu...
👁 296
Empire CMS smart label call field collection

Empire CMS smart label call field collection Program Notes Empire cms

Collect and classify all fields that support smart tag calls into Empire CMS smart tags: [e:loop={column ID/topic ID, number of items displayed, operation type, only display pictures with titles, additional SQL conditions, display sorting}] Template code content [/e:loop] Calling time: <?=date('m-d',$bqr[newstime])?> <?=dat…
👁 3749

Recommended reading

(Adaptive mobile version) Responsive HR service website PBootCMS template – Enterprise management website source code download – 0260

(Adaptive mobile version) Responsive HR service website PBootCMS template – Enterprise management website source code download – 0260 Practical Collection pbootcms Template

An adaptive, responsive PbootCMS website template designed for HR service and management platforms for mobile devices. Its professional and sophisticated design makes it ideal for HR agencies and corporate management consulting firms to showcase their services. This template helps service providers attract corporate clients online and expand their business channels. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 56
Responsive AI Chip Website Template 0128

Responsive AI Chip Website Template 0128 Practical Collection Yiyou template

An eyouCMS responsive website template designed for the artificial intelligence and AI chip industry. Its futuristic design style effectively showcases AI chip products, R&D initiatives, and real-world application scenarios. This template helps chip design companies build their brand image online and attract investment and customers. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for YouyouCMS...
👁 70
Responsive tea set sales website template 1192

Responsive tea set sales website template 1192 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for the tea and tea ware sales industry. Its classical and elegant design is perfect for showcasing tea products, tea ceremony culture, brand stories, and sales information. It helps tea brands effectively present their products online and attract tea enthusiasts. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 35
Empire CMS – Paid Video Viewing Responsive Template 031

Empire CMS – Paid Video Viewing Responsive Template 031 Practical Collection empire template

A clean, free-source code package for Empire CMS with paid video viewing options! Multiple access levels are available: free viewing for visitors, login-required viewing for members, VIP member viewing, and paid viewing – four distinct options in total – along with a Member Center! The outdated comment functionality has been removed, and any login-related display issues in the header have been fixed. Preview image included.
👁 273
(Adaptive mobile version) Responsive full-home renovation and custom home furnishings website – PBootCMS template; Download source code for a green home renovation company website – 0261

(Adaptive mobile version) Responsive full-home renovation and custom home furnishings website – PBootCMS template; Download source code for a green home renovation company website – 0261 Practical Collection pbootcms Template

This is an adaptive, responsive website template built with PbootCMS, designed for home renovation customization and home furnishings businesses. Its eco-friendly design makes it ideal for displaying case studies and products from renovation companies or whole-house customization brands. It helps home improvement businesses attract homeowners online and enhance their brand image. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related Articles...
👁 37
Marketing-oriented soy milk maker and food processor website template 0129

Marketing-oriented soy milk maker and food processor website template 0129 Practical Collection Yiyou template

This EYO CMS template is ideal for businesses specializing in soy milk makers and food processors. Its modern home appliance design style effectively showcases these products as well as the brand story. It helps small household appliance brands present their products online and attract home consumers. Template Preview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EYO CMS | EYO CMS (Eyo...
👁 55