Php's perfect batch generation of thumbnails code-PHP source code

Source: Internet
Author: User
Ec (2); & lt ;! DOCTYPEHTMLPUBLIC & quot;-W3CDTDHTML4.0TransitionalEN & quot; & gt; & lt; HTML & gt; & nbsp; & lt; HEAD & gt; & nbsp; & lt; metahttp-equiv & quot; Content-Type & quot; content & quot; texthtml; c script ec (2); script





Generate a thumbnail


$ C = array ('dir' => '. /images ', 'newdir' => '. /thumb', 'w' => '123', 'H' => '123 ');
// Only process files with small in the file name. If you process all files, set it '.'
$ Only = '.';

// If you do not want to overwrite an existing image, set this row to 0;
$ Over = 1;

// Whether to process GIF images. 0 indicates not to process
$ Regif = 1;

If ($ _ GET ['do '] = 'start '){
$ File = array ();
$ Dir = $ _ POST ['dir'];
$ Newdir = $ _ POST ['newdir'];
$ W = intval ($ _ POST ['W']);
$ H = intval ($ _ POST ['H']);
$ D = @ dir ($ dir) or die ('the directory does not exist! ');
$ Config =" '". $ Dir. "', 'newdir' => '". $ newdir. "', 'w' => '". $ w. "', 'H' => '". $ h. "');?> ";
Savefile ($ c, $ config );
While ($ a = $ d-> read ()){
$ Type = substr (strrchr ($ a, "."), 1 );
If ($! = '.' & $! = '..'){
If (is_dir ($ dir. '/'. $ )){
$ Zdir [] = $;
} Elseif ($ type = 'jpg '| $ type = 'gif' | $ type = 'bmp' | $ type = 'png '){
If (strstr ($ a, $ only )){
$ File [] = $ dir. '/'. $;
}
}
}
}
$ Rand = rand (100,999 );
$ Img = new image ($ dir, $ newdir );
$ Img-> over = $ over;
$ Img-> regif = $ regif;
Echo"

";
Foreach ($ file as $ key ){
$ Src = $ img-> reImg ($ key, $ w, $ h );
Echo"

";
}
Echo"

";
Die ("



");
}
?>





©2008 wbsifan


// Create a directory (directory, [mode])
Function mkdirs ($ l1, $ l2 = 0777 ){
If (! Is_dir ($ l1 )){
// If the directory does not exist, recursively create
Mkdirs (dirname ($ l1), $ l2 );
Return @ mkdir ($ l1, $ l2 );
}
Return true;
}

// Save the file (file, [content])
Function savefile ($ l1, $ l2 = ''){
If (function_exists (file_put_contents )){
File_put_contents ($ l1, $ l2 );
} Else {
$ Fp = @ fopen ($ l1, 'wb ');
@ Fwrite ($ fp, $ l2 );
Fclose ($ fp );
}
}

// Obtain the file suffix (file)
Function getfix ($ l1 ){
Return end (explode ('.', $ l1 ));
}

// Whether the data type is allowed (current, allowed)
Function checkfix ($ l1, $ l2 ){
If (! Is_array ($ l2 )){
$ L2 = explode (',', str_replace ('','', $ l2 ));
}
Return in_array ($ l1, $ l2 )? 1: 0;
}

Class image {
// Source Address
Var $ src;
// New image path (after localization)
Var $ newsrc;

// Allowed graph types
Var $ allowtype = array('.jpg ', '.gif', '.png', '.jpeg ');
// Whether to scale down GIF, 0 is not processed
Var $ regif = 0;
// Whether the source file is retained (1 is retained, 0 is MD5)
Var $ keep = 0;
// Whether the existing image can be overwritten. If the value is 0, the existing image cannot be overwritten.
Var $ over = 0;

// Image source directory
Var $ dir;
// Processed directory
Var $ newdir;


Function _ construct ($ l1 = null, $ l2 = null ){
$ This-> dir = $ l1? $ L1: "./images/temp ";
$ This-> newdir = $ l2? $ L2: "./images/s ";
}

Function image ($ l1 = null, $ l2 = null ){
$ This->__ construct ($ l1, $ l2 );
}

/**
* Path of the processed file
* Save the source image and MD5 file name to a new directory.
*
* @ Param rename ()
* @ Return indicates the processed file name.
*/
Function reName ($ src ){
// File name after MD5 (for example, 3293okoe.gif)
$ L1 = substr (md5 ($ src), 10, 10). strrchr ($ src ,".");
// Processed file name
$ L1 = $ this-> w. '_'. $ this-> h. '_'. $ l1;
// Return the address after processing
Return $ this-> newdir. '/'. $ l1;
}


/**
* Generating thumbnails
*
* @ Param Mini (image address, width, height, quality)
* To retain the original file name, set $ o-> keep = 1;
* @ Return: The image address after processing
*/
Function Mini ($ src, $ w, $ h, $ q = 80 ){
$ This-> src = $ src;
$ This-> w = $ w;
$ This-> h = $ h;

// Whether to process GIF images
If (strrchr ($ src, ".") = ". gif" & $ this-> regif = 0 ){
Return $ this-> src;
}

// Whether the original file name is retained. It is not retained by default.
If (! $ This-> keep ){
// The renamed file address
$ Newsrc = $ this-> reName ($ src );
} Else {
// Keep original name
$ Src = str_replace ('\', '/', $ src );
$ Newsrc = $ this-> newdir. strrchr ($ src ,"/");
}

// If it already exists, return the address directly.
If (file_exists ($ newsrc) & $ this-> over = 0 ){
Return $ newsrc;
}

// If it is a network file, save it first
If (strstr ($ src, "http ://")&&! Strstr ($ src, $ _ SERVER ['HTTP _ host']) {
$ Src = $ this-> getimg ($ src );
}
// Obtain image attributes
$ Arr = getimagesize ($ src );
$ Ow = $ arr [0];
$ Oh = $ arr [1];
$ Ot = $ arr [2];
Switch ($ ot ){
Case 1:
$ Im = imagecreatefromgif ($ src );
Break;
Case 2:
$ Im = imagecreatefromjpeg ($ src );
Break;
Case 3:
$ Im = imagecreatefrompng ($ src );
Break;
Default:
Return 0;
}
// Process the thumbnail
$ Nim = imagecreatetruecolor ($ w, $ h );
$ K1 = round ($ h/$ w, 2 );
$ K2 = round ($ oh/$ ow, 2 );
If ($ k1 <$ k2 ){
$ Oow = $ ow;
$ Oh = round ($ ow * $ k1 );
$ Sw = 0;
$ Sh = ($ oh-$ oh)/2;
}
Else {
$ Oow = $ oh/$ k1;
$ Oh = $ oh;
$ Sw = ($ ow-$ oow)/2;
$ Sh = 0;
}
// Generate an image
If (function_exists (imagecopyresampled )){
Imagecopyresampled ($ nim, $ im, 0,0, $ sw, $ sh, $ w, $ h, $ oow, $ OH );
}
Else {
Imagecopyresized ($ nim, $ im, 0,0, $ sw, $ sh, $ w, $ h, $ oow, $ OH );
}
// Whether the processed directory exists
If (! Is_dir ($ this-> newdir )){
@ Mkdir ($ this-> newdir );
}
// Save the image
Switch ($ ot ){
Case 1:
$ Rs = imagegif ($ nim, $ newsrc );
Break;
Case 2:
$ Rs = imagejpeg ($ nim, $ newsrc, $ q );
Break;
Case 3:
$ Rs = imagepng ($ nim, $ newsrc );
Break;
Default:
Return 0;
}
// Return the processed path
Return $ newsrc;
}

/**
* Save the network image
*
* @ Param getImg (source image)
* @ Return the saved address (MD5 file name in the local directory)
*/
Function getimg ($ l1 ){
$ L2 = $ this-> dir. '/'. substr (md5 ($ l1), 10, 10). strrchr ($ l1 ,".");
// The file exists and the address is returned directly.
If (file_exists ($ l2 )){
// Echo "exits ...";
Return $ l2;
}

// Get the file and return the new path
$ Img = file_get_contents ($ l1 );
If ($ img ){
If (! Is_dir ($ this-> dir )){
@ Mkdir ($ this-> dir );
}
Savefile ($ l2, $ img );
// Echo "file_get ..";
Return $ l2;
}
}

/**
* Convert the thumbnail www.111cn.net (the file name and structure remain unchanged)
*
* @ Param mini (Source Address, width, height, quality)
* @ Return: The generated address.
*/

Function reImg ($ src, $ w, $ h, $ q = 80 ){
$ This-> keep = 1;
Return $ this-> Mini ($ src, $ w, $ h, $ q );
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.