Recently in the development of a local Internet application project, in order to increase the user experience, you need to display the search results on the left side of a website thumbnail effect, in the internet has been Baidu Google after a morning, found that most of the implementation of a small amount is still possible, if a large number of always in the middle of a lot of problems, Finally did not find a very satisfactory program, simply do it yourself.
(Figure I)
Here are the steps and code to create a thumbnail using PHP in conjunction with IECAPT in a Windows environment:
First, prepare
Download the latest version of IECAPT
Official address: http://iecapt.sourceforge.net/
In the Linux environment, you can consider using Html2image to implement
Download Address: http://www.guangmingsoft.net/htmlsnapshot/html2image.i386.tar.gz
Other implementations also have cutycapt, and, as long as the Windows environment, there is IE browser (recommended to use IE7), the majority of the machine should not be a problem.
Second, create a data table (this step is not necessary, according to the actual situation to choose)
Because there is a lot of data to batch, it is necessary to build a data table to hold the URL address of the website you want, as shown below (MySQL database table):
CREATE TABLE IF not EXISTS ' T_url ' ( ' id ' int (one) unsigned not NULL auto_increment, ' url ' varchar (+) ' Not null,
' pictype ' tinyint (1) unsigned NOT null COMMENT ' 1. Non-proportional thumbnail 2 proportional thumbnail ' flag ' tinyint (1) NOT null DEFAULT ' 1 ' COMMENT ' 0. Disable 1. Use PRIMARY KEY (' id ')) engine=myisam DEFAULT charset=gbk comment= ' url link table ' auto_increment=1;
Iii. Creating a batch file
1. First unpack the downloaded IECAPT package, and then put the Iecapt.exe in the folder you want to generate (for example, img_tmp).
For ease of understanding, before looking at the following code, create a Test.bat file, right-click Edit, write a sentence if not exist ay360cn.jpg (Iecapt.exe--url=http://www.ay360.cn/--out= ay360cn.jpg) Save, double-click Run Test.bat to see if there is a file named Ay360cn.jpg in this directory, if you see the description of success, this sentence is the core statement.
2. Import the required URL link into the URL link table T_url and execute the following PHP code:
1
0) {//each 30 is a batch file $title = "title capt". $i. ". Bat\r\n "; $str = $title. $str; $file _bat = fopen ("Img_tmp/capt". $i. ". Bat "," w "); if (fwrite ($file _bat, $str)) {echo "batch file Capt". $i. " Build succeeded
", PNs $str =" "," "," "," "," "," $i "= $i +1;41},?>
Operation Result:
(Figure II)
Iv. Execution of batch files
Can be executed through the PHP program batch file, but there are many problems in the operation, there is a manual direct batch open the above just created batch file, considering the bandwidth and CPU, not more than 20, the speed of about 3-5 seconds/effect three:
(Figure III)
V. Create a thumbnail image
The file that generated the thumbnail is create_image_img.php, which contains the main class file that generated the thumbnails is image.class.php, and the code for the two files is as follows:
ceate_image_img.php Code:
1
Generating thumbnails:". $row [' id ']." ". $row [' url ']."
"; $url = $row [' url ']; $url _MD5 = MD5 ($url), $pictype = $row [' Pictype ']; $limit _time = 1; Create a large map created $limit _time days, $thumbnails _folder = ' img_tmp/'; Save the directory of the temporary large map, must be/end at $thumbnails _folder2 = ' img/'; To save a small map of the directory, must be/end of the $output _format = ' jpg '; $cached _filename = $thumbnails _folder. $url _md5. ".". $output _format;26 $to _filename = $thumbnails _folder2. $url _md5. '. ' $output _format; if ((File_exists ($cached _filename) | | filemtime ($filename) + $limit _time*86400 > Time ()) & &!file_exists ($to _filename)) {+ if (filesize ($cached _filename) > 1024) {//bytes, cannot be blank picture 33 Create thumbnails include ("image.class.php"); $img = new Zubrag_image; PNS//Get parameters38 $img->image_type = 2; 1 = GIF, 2 = JPG, 3 = PNG39 $img->quality = 80;40 $img->max_w = 90;41 $img->max_h = 67;42 $img->iscapt = ($pictype = = 1)? True:false; A Boolean is used here, and the database is not 1. Non-proportional thumbnail 2. A proportional thumbnail of the IF ($img->generatethumbfile ($cached _filename, $to _fi Lename) {echo ")successfully created thumbnails:". $row [' id ']." ". $row [' url ']; }else{echo "failed to create thumbnail:". $row [' id ']." ". $row [' URL '];48} (+}) $sql =" SELECT * from T_url ID > ". $_get[' Id ']. "and flag = 1 ORDER BY ID ASC limit 1"; $query = mysql_query ($sql); $row = Mysql_fetch_array ($query); echo "
prepare to generate thumbnails:". $row [' id ']." ". $row [' url ']."
"; ($row [' id ']) {}else{61 $_get[' id '] = "" ";" "The"
image.class.php Code:
1
Image_type = = 1) &&!function_exists (' imagegif ')) $this->image_type = 3; Switch ($this->image_type) {1:16//if ($this->save_to_file) {$res = Imageg IF ($im, $filename);//}19//else {//Header ("Content-type:image/gif");//$res = Imagegif ($im);//}23 break;24 case 2:25 $res = imagejpeg ($im, $filename, $this Quality); break;27 case 3:28 $res = Imagepng ($im, $filename); BREAK;30} return $res; Imagecreatefromtype ($type, $filename) {$im = null;36 switch ($type) {PNS case 1:38 $im = imagecreatefromgif ($filename); break;40 Case 2:41 $im = Imagecreatefromjpeg ($filename), break;43 case 3:44 $im = Imagecreatefr Ompng ($filename); break;46}47 return $im; 48}49 50 51 function Generatethumbfile ($from _name, $to _name) {List of $orig _x, $orig _y, $orig _img_type, $img _sizes) = Getima Gesize ($from _name); /*if ($this->cut_x > 0) $orig _x = min ($this->cut_x, $orig _x), if ($this->cut_y > 0) $orig _y = Min ($this->cut_y, $orig _y) */55 if ($this->iscapt && (($orig _y/$orig _x) > (90/67))) {//Yes, and too high $orig _y = $orig _x* (67/90); }58 $this->image_type = ($this->image_type! =-1? $this->image_type: $orig _img_type); if ($orig _img_type < 1 or $orig _img_type > 3) die ("Image type not supported"); if ($this->image_type = = 1) {$ni = Imagecreate ($this->max_w, $this->max_h); 65}66 else {$ni = Imagecreatetruecolor ($this->max_w, $this->max_h); $white = Imagecolorall Ocate ($ni, 255, 255, 255); Imagefilledrectangle ($ni, 0, 0, $this->max_w, $this->max_h, $WHITE); $im = $this->imagecreatefromtype ($orig _img_type, $from _name); Imagepalettecopy ($ni, $im); Imagecopyresampled ($ni, $im, 0, 0, 0, 0, $this->max_w, $this-> ; Max_h, $orig _x, $orig _y); if ($this->saveimage ($ni, $to _name)) {Bayi return true;82}else{83 return false;84}85} ?>
Vi. Summary
At this point the entire implementation of the page and create thumbnails of the steps to the end, where the batch file section in order to improve efficiency in a manual way, bulk open batch file, in addition, the linked database section can also be implemented with the encapsulated database operation class, the code will be more concise.