Solving the problem of php batch image format conversion
最近要把项目中的图片全部生成webp格式, 过程整理一下, (直接存在本地,或者图片链接存在数据库都可以看看)
首先,肯定是批量处理, 一个php处理不了这么多, 会爆内存的, 个人建议用ajax循环调用php文件
下面贴ajax代码, 小白和我看看就行, 老人跳过
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>更换图片</title> <script type="text/javascript" src="./jquery-1.8.3.min.js"></script> </head> <body> <button id="but01">点我就帮你转图片</button> </body> <script type="text/javascript"> var limit = 50; //每次50条 var offset = 0; //偏移量,每次自增50 function toPhp(){ $.ajax({ url: "./sqlsrc.php", //后台处理程序 type: 'POST', data: {limit: limit, offset: offset}, success:function(data) { if(data) { offset = offset + 50; toPhp(); //根据后台返回的内容,继续调用自己 }else { alert('这里是limit---' + limit); alert('这里是data---' + data); alert('完成'); } }, error:function() { alert('失败'); }, dataType:'text' }); } $("#but01").click(toPhp); // 点击按钮执行程序</script> </html>
下面是php处理文件, sqlsrc.php主要是处理那种图片路径存在数据库里的,
以下本人是把数据库的图片拉到本地同时生成webp格式/
你可以直接在项目所在的目录运行此脚本,
注意 sqlsrc.php中的地址要自己拼~
sqlsrc.php文件如下
<?php ini_set ('gd.jpeg_ignore_warning', 1); //忽略 set_time_limit(0);//0表示不限时 $dsn='mysql:host=192.168.1.1;dbname=yourdbname'; $user='root'; $password=''; $status=1; //看个人需要了,这个参数 $data = $_POST; try { $sql='select thumbnail_pic,small_pic,big_pic from sdb_goods'; $sql .= " limit {$data['limit']} offset {$data['offset']}"; // 偏移分页查询/ $dbh=new PDO($dsn,$user,$password); $dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); $stmt=$dbh->prepare($sql); $stmt->bindParam(':status',$status); //绑定参数的样例, 喜欢参数绑定的看这里,其他人无视这行 $stmt->execute(); $flag = false; //这里设置一个标识,如果没走while函数, 那么flag依然为false while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) { $flag = true; //只要有查询到的内容, flag就会true $count = $data['count']; $ptn = "/http.*?fs_storage/"; foreach ($row as $k=>$v){ preg_match_all($ptn,$v,$res); if(empty($res[0])) { continue; } foreach ($res as $kk=>&$vv) //这层遍历不需要, 谁可以优化下贴上来. { $vv[0] = rtrim($vv[0],'fs_storage');//图片远程路径 $vv[0] = rtrim($vv[0],'|');//图片远程路径 $ptn01 = "/http.*?\|/"; $src = preg_replace($ptn01,'',$vv[0]); //远程路径标准版 if(empty($src)) { continue; } //这里获取到表中图片的地址, 整理成本地的地址 $ptn02 = "/.*com/"; $local = preg_replace($ptn02,'.',$src); //本地路径 $ptn03 = "/:88/"; $local = preg_replace($ptn03,'',$local); //本地路径+文件名 $path = pathinfo($local,PATHINFO_DIRNAME); if(empty($path)) { continue; } if(!is_dir($path)) { mkdir($path,777,true); // } //下面这行会报个warning, 忘了是啥, 直接抑制掉了 @$img = file_get_contents($src); if (!$img) { continue; } file_put_contents($local,$img); //根据数据库的地址下载图片到本地// turnType($local); //此行代码调用函数, 在文件夹中生成一个同名的webp图片; //接着应该把地址存到数据库中 //拼接成你线上的url图片地址,然后存到数据库就行了 //但是没必要,名字都是一样的,只是后缀不一样 } } } echo $flag; $flag = false; //这行也是不需要的, 但是为了祭奠我的老师, 容许我放在这里占个位/ } catch (PDOException $e) { echo 'SQL Query:'.$sql.'</br>'; echo 'Connection failed:'.$e->getMessage(); } ?> sqlsrc
关于上面这个文件,while中的代码, 主要是正则 拼接地址,这部分可以不看, 每个人的业务都不一样, 所以看了也白看, 直接看其他部分(gd库函数和最后的调用函数)
我这里是1次查50张图片, 然后遍历, 一张图片一张图片生成webp,效率非常不高///////////
pictest.php文件如下,(上个文件中有调用, )
单个图片生成webp
<?php /* ** webp格式转换函数, ** 参数为具体的图片路径加上文件名, ** 例:D:\workspaces\upload\images\2017\demo.jpeg */ function turnType($file) { if(is_file($file)) { //获取文件后缀名 $ext = pathinfo($file,PATHINFO_EXTENSION);; //根据后缀名把jpg或者png转成webp if($ext == 'jpeg' || $ext == 'jpg' || $ext == 'png') { //生成新的文件名 $newpic = rtrim($file,$ext).'webp'; if($ext == 'jpg' ) { $ext = 'jpeg'; } $funName = 'imagecreatefrom'.$ext; //拼接函数名imagecreatefromjpeg 还是 imagecreatefrompng $hImg = $funName($file); //打开这个图片资源, imagewebp($hImg,$newpic); //用这个图片资源创建一个webp图片, 存在路径$tdir imagedestroy($hImg); //销毁画布资源 } } } ?> 单个文件生成webp
单个文件生成webp
以上三个代码放在一个文件夹中/ 主要改第二个文件sqlsrc.php中拼接地址的部分(或者删除) ,图片直接在本地的...可以参考下面的代码,
怎么限制每次查询的数量没考虑, 谁有建议可以贴上来/
这里还有直接遍历整个目录, 然后批量生成webp格式的代码. 数据量小的可以参考下/;
<?php $dir = '../images'; //这里设置目录, 会遍历整个目录,然后生成一个webp格式的图片/ 数量太大会超内存,有需要的可以参考imgtype($dir);//这里调用函数 //格式转换函数function jpgturn ($sdir,$tdir,$ext) { if($ext == 'jpeg' || $ext == 'jpg') { $hImg = imagecreatefromjpeg($sdir); } if($ext == 'png' || $ext == 'PNG') { $hImg = imagecreatefrompng($sdir); } imagewebp($hImg,$tdir); imagedestroy($hImg); }// 自定义函数---获取后缀名;function get_extension($file) { return pathinfo($file,PATHINFO_EXTENSION); }// 遍历目录, 递归调用, 拼接新文件名, 然后调用格式转换函数function imgtype ($dir) { $dir = rtrim($dir,'/').'/'; $hd = opendir($dir); // while($hf=readdir($hd)) // { if($hf=='.'||$hf=='..') { continue; } if(is_file($dir.$hf)) { //获取文件后缀名的自定义函数 $ext = get_extension($dir.$hf); //根据后缀名把jpg转成webp if($ext == 'jpeg' || $ext == 'jpg' || $ext == 'png' || $ext == 'PNG') { //生成新的文件名 $new = rtrim($dir.$hf,$ext); $new .= 'webp'; jpgturn($dir.$hf,$new,$ext); } }if(is_dir($dir.$hf))//递归调用 { imgtype($dir.$hf); } } }?>
The above is the detailed content of Solving the problem of php batch image format conversion. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
