About converting PHP batch image formats-This article converts to webp. the formats of other processes are the same,
Recently, we have to generate all the images in the project in webp format. Sort out the process. (You can check whether the image is saved locally or the image is linked to a database)
First of all, it must be batch processing. If a php file cannot be processed so much, the memory will burst. I suggest calling the php file cyclically with ajax.
Paste the ajax code below. Just check it out with me. The old man will skip it.
<! DOCTYPE html>
The following is php processing files. sqlsrc. php mainly processes the image paths stored in the database,
Below I will pull the image of the database locally and generate the webp format/
You can run this script directly in the directory where the project is located,
Note that the addresses in sqlsrc. php must be spelled out by yourself ~
The sqlsrc. php file is as follows:
1 <? Php 2 ini_set ('gd.jpeg _ ignore_warning ', 1); // ignore 3 set_time_limit (0); // 0 indicates no time limit 4 $ dsn = 'mysql: host = 192.168.1.1; dbname = yourdbname '; 5 $ user = 'root'; 6 $ password = ''; 7 $ status = 1; // you need it, this parameter is 8 9 $ data =$ _ POST; 10 11 try {12 $ SQL = 'select thumbnail_pic, small_pic, big_pic from sdb_goods '; 13 $ SQL. = "limit {$ data ['limit']} offset {$ data ['offset']}"; // offset paging query/14 $ dbh = new PDO ($ dsn, $ user, $ password); 15 $ dbh -> SetAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION); 16 $ stmt = $ dbh-> prepare ($ SQL); 17 $ stmt-> bindParam (': status ', $ status); // example of parameter binding. For details about parameter binding, see here. Others ignore this line of 18 $ stmt-> execute (); 19 20 $ flag = false; // set an identifier here. If the while function is not used, the flag is still false21 while ($ row = $ stmt-> fetch (PDO :: FETCH_ASSOC) {22 $ flag = true; // if there is any queried content, the flag will true23 $ count = $ data ['Count']; 24 25 $ ptn = "/http. *? Fs_storage/"; 26 foreach ($ row as $ k => $ v) {27 28 preg_match_all ($ ptn, $ v, $ res ); 29 if (empty ($ res [0]) 30 {31 continue; 32} 33 foreach ($ res as $ kk => & $ vv) // This layer is not required for traversal, who can optimize the post. 34 {35 $ vv [0] = rtrim ($ vv [0], 'fs _ store '); // picture remote path 36 $ vv [0] = rtrim ($ vv [0], '|'); // picture remote path 37 $ ptn01 = "/http. *? \ |/"; 38 $ src = preg_replace ($ ptn01,'', $ vv [0]); // remote path Standard Edition 39 40 if (empty ($ src )) 41 {42 continue; 43} 44 45 // here, the address of the image in the table is obtained, and the address of the local location is organized. 46 $ ptn02 = "/. * com/"; 47 $ local = preg_replace ($ ptn02 ,'. ', $ src); // local path 48 $ ptn03 = "/: 88/"; 49 $ local = preg_replace ($ ptn03, '', $ local ); // local path + file name 50 $ path = pathinfo ($ local, PATHINFO_DIRNAME); 51 if (empty ($ path) 52 {53 continue; 54} 55 if (! Is_dir ($ path) 56 {57 mkdir ($ path, 777, true); // 58} 59 60 61 // the following line will report a warning, forget what it is, 62 @ $ img = file_get_contents ($ src); 63 if (! $ Img) 64 {65 continue; 66} 67 file_put_contents ($ local, $ img ); 68 // download the image to the local device based on the database address // 69 70 71 turnType ($ local); 72 // call the function in this line of code to generate a webp image with the same name in the folder; 73 // Save the address to the database 74 // splice it into your online url image address and save it to the database 75 // but it is not necessary and the name is the same, only the suffix is different 76} 77} 78} 79 echo $ flag; 80 $ flag = false; // This line is not required, but in order to pay homage to my teacher, allow me to take the place here/81} catch (PDOException $ e) {82 echo 'SQL Query :'. $ SQL. '</br>'; 83 echo 'Connection failed :'. $ e-> getMessa Ge (); 84} 85?>
Sqlsrc
For the code in the while part of the above file, it is mainly the regular splicing address. This part can be left blank, and everyone's business is different, directly look at other parts (gd library functions and the final called functions)
I will query 50 images and traverse them once. A webp is generated for one image, which is very inefficient ///////////
The pictest. php file is as follows (called in the previous file ,)
Generate webp for a single image
1 <? Php 2/* 3 ** webp format conversion function. The 4 ** parameter adds the file name to the specific image path, for example, D: \ workspaces \ upload \ images \ 2017 \ demo.jpeg 6 */7 function turnType ($ file) 8 {9 if (is_file ($ file )) 10 {11 // get the file suffix 12 $ ext = pathinfo ($ file, PATHINFO_EXTENSION );; 13 14 // convert jpg or png to webp15 if ($ ext = 'jpeg '| $ ext = 'jpg' | $ ext = 'png ') based on the suffix ') 16 {17 // generate a new file name 18 $ newpic = rtrim ($ file, $ ext ). 'webp '; 19 20 if ($ ext = 'jpg') 21 {22 $ ext = 'jpeg '; 23 } 24 25 $ funName = 'imagecreatefrom '. $ ext; // The Name Of The concatenation function imagecreatefromjpeg or imagecreatefrompng26 27 $ hImg = $ funName ($ file); // open the image resource, 28 29 imagewebp ($ hImg, $ newpic ); // use this image resource to create a webp image. The path $ tdir30 31 imagedestroy ($ hImg) exists. // destroy the canvas resource 32} 33} 34} 35 36?>
Webp generated by a single file
Put the above three codes in a folder/change the second file sqlsrc. php concatenates (or deletes) The part of the address, and the image is directly stored locally... refer to the following code,
How to limit the number of queries per time is not taken into consideration, who has suggested attaching/
Here, we can directly traverse the entire directory and generate code in webp format in batches. For more information about small data volumes, see /;
<? Php $ dir = '.. /images '; // set the directory here to traverse the entire directory, and then generate a webp format image. The number of images is too large to exceed the memory. For more information, see imgtype ($ dir ); // call the 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);} // User-Defined function --- get extension name; function get_extension ($ file) {return pathinf O ($ file, PATHINFO_EXTENSION);} // traverses the directory, recursively calls, concatenates a new file name, and then calls the format conversion function imgtype ($ dir) {$ dir = rtrim ($ dir ,'/'). '/'; $ hd = opendir ($ dir); // while ($ hf = readdir ($ hd) // {if ($ hf = '. '| $ hf = '.. ') {continue;} if (is_file ($ dir. $ hf) {// gets the UDF with the file suffix $ ext = get_extension ($ dir. $ hf ); // convert the jpg file to webp if ($ ext = 'jpeg '| $ ext = 'jpg' | $ ext = 'png '| $ ext = = 'png ') {// generate a new file name $ new = rtrim ($ dir. $ hf, $ Ext); $ new. = 'webp '; jpgturn ($ dir. $ hf, $ new, $ ext) ;}} if (is_dir ($ dir. $ hf) // recursive call {imgtype ($ dir. $ hf) ;}}?>
Pictest. php file