<? Php // Use PHP to generate batch image thumbnails Function mkdirs ($ dirname, $ mode = 0777) // Create a directory (directory, [mode]) { If (! Is_dir ($ dirname )) { Mkdirs ($ dirname, $ mode); // If the directory does not exist, recursively create Return mkdir ($ dirname, $ mode ); } Return true; } Function savefile ($ filename, $ content = '') // Save the file (file, [content]) { If (function_exists (file_put_contents )) { File_put_contents ($ filename, $ content ); } Else { $ Fp = fopen ($ filename, "wb "); Fwrite ($ fp, $ content ); Fclose ($ fp ); } } Function getsuffix ($ filename) // get the filename suffix { Return end (explode (".", $ filename )); } Function checksuffix ($ filename, $ arr) // whether the parameter is of the allowed type (currently, allowed) { If (! Is_array ($ arr )) { $ Arr = explode (",", str_replace ("", "", $ arr )); } Return in_array ($ filename, $ arr )? 1: 0; } Class image { Var $ src; // Source Address Var $ newsrc; // path of the New Graph (after localization) Var $ allowtype = array (". gif", ". jpg", ". png", ". jpeg"); // allowed image types Var $ regif = 0; // whether to scale down the GIF. If it is 0, it is not processed. Var $ keep = 0; // whether to retain the source file (1 is reserved, 0 is MD5) Var $ over = 0; // whether the existing image can be overwritten. If the value is 0, the existing image cannot be overwritten. Var $ dir; // image source directory Var $ newdir; // processed directory Function _ construct ($ olddir = null, $ newdir = null) { $ This-> dir = $ olddir? $ Olddir: "./images/temp "; $ This-> newdir = $ newdir? $ Newdir: "./images/s "; } Function reNames ($ src) { $ Md5file = substr (md5 ($ src), 10, 10). strrchr ($ src ,"."); // After the MD5 file name (for example, 3293okoe.gif) $ Md5file = $ this-> w. "_". $ this-> h. "_". $ md5file; // Processed file name Return $ this-> newdir. "/". $ md5file; // Save the source image and MD5 file name to the new directory. } Function Mini ($ src, $ w, $ h, $ q = 80) // Generate a thumbnail Mini (image address, width, height, quality) { $ This-> src = $ src; $ This-> w = $ w; $ This-> h = $ h; If (strrchr ($ src, ".") = ". gif" & $ this-> regif = 0) // Whether to process GIF images { Return $ this-> src; } If ($ this-> keep = 0) // whether to retain the source file. By default, the source file is not retained. { $ Newsrc = $ this-> reNames ($ src); // renamed file address } Else // keep original name { $ Src = str_replace ("\", "/", $ src ); $ Newsrc = $ this-> newdir. strrchr ($ src ,"/"); } If (file_exists ($ newsrc) & $ this-> over = 0) // If it already exists, return the address directly. { Return $ newsrc; } If (strstr ($ src, "http ://")&&! Strstr ($ src, $ _ SERVER ['HTTP _ host']) // If it is a network file, save it first { $ Src = $ this-> getimg ($ src ); } $ Arr = getimagesize ($ src); // obtain image attributes $ Width = $ arr [0]; $ Height = $ arr [1]; $ Type = $ arr [2]; Switch ($ type) { Case 1: // 1 = GIF, $ Im = imagecreatefromgif ($ src ); Break; Case 2: // 2 = JPG $ Im = imagecreatefromjpeg ($ src ); Break; Case 3: // 3 = PNG $ Im = imagecreatefrompng ($ src ); Break; Default: Return 0; } // Process the thumbnail $ Nim = imagecreatetruecolor ($ w, $ h ); $ K1 = round ($ h/$ w, 2 ); $ K2 = round ($ height/$ width, 2 ); If ($ k1 <$ k2) { $ Width_a = $ width; $ Height_a = round ($ width * $ k1 ); $ Sw = 0; $ Sh = ($ height-$ height_a)/2; } Else { $ Width_a = $ height/$ k1; $ Height_a = $ height; $ Sw = ($ width-$ width_a)/2; $ Sh = 0; } // Generate an image If (function_exists (imagecopyresampled )) { Imagecopyresampled ($ nim, $ im, 0, $ sw, $ sh, $ w, $ h, $ width_a, $ height_a ); } Else { Imagecopyresized ($ nim, $ im, 0, $ sw, $ sh, $ w, $ h, $ width_a, $ height_a ); } If (! Is_dir ($ this-> newdir )) { Mkdir ($ this-> newdir ); } Switch ($ type) // Save the image { 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 $ newsrc; // return the processed path } Function getimg ($ filename) { $ Md5file = $ this-> dir. "/". substr (md5 ($ filename), 10, 10). strrchr ($ filename ,"."); If (file_exists ($ md5file )) { Return $ md5file; } // Get the file and return the new path $ Img = file_get_contents ($ filename ); If ($ img) { If (! Is_dir ($ this-> dir )) { Mkdir ($ this-> dir ); } Savefile ($ md5file, $ img ); Return $ md5file; } } Function reImg ($ src, $ w, $ h, $ q) // Convert the thumbnail (the file name and structure remain unchanged) { $ This-> keep = 1; Return $ this-> Mini ($ src, $ w, $ h, $ q ); // Address generated by return } } $ Image = new image (); Echo $ image-> reImg ("images/zht.jpg", 75, 80 ); Echo "<br/> "; Echo $ image-> reImg ("images/m89w.jpg", 80 ); Echo "<br/> "; Echo $ image-> getimg ("./images/s/zht.jpg "); ?> |