PHP's perfect batch generation thumbnail code

Source: Internet
Author: User
Tags explode md5 mkdir rand save file

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<TITLE> Generate thumbnails </TITLE>
</HEAD>
<BODY>
<?php
$c = Array (' dir ' => './images ', ' newdir ' => './thumb ', ' W ' => ', ' h ' => ' 200 ');
Process only files that contain small in the file name, and if all files are processed, set to '. '
$only = '. ';

Set this row to 0 if you do not want to overwrite a picture that already exists;
$over = 1;

Whether to process GIF, 0 is not processed
$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 (' directory does not exist! ');
$config = "<?php return Array (' dir ' => '). $dir." ', ' newdir ' => '. $newdir. "', ' W ' => '". $w. "', ' h ' => '. $h." > ";
SaveFile ($c, $config);
while ($a = $d->read ()) {
$type = substr (STRRCHR ($a, "."), 1);
if ($a!== '. ' && $a!== ' ... ') {
if (Is_dir ($dir. '/'. $a)) {
$zdir []= $a;
ElseIf ($type = = ' jpg ' | | | $type = = ' gif ' | | $type = = ' bmp ' | | | $type = = ' png ') {
if (strstr ($a, $only)) {
$file []= $dir. '/' $a;
}
}
}
}
$rand = rand (100,999);
$img = new Image ($dir, $newdir);
$img->over = $over;
$img->regif= $regif;
echo "<div style= ' width:800px ' >";
foreach ($file as $key) {
$SRC = $img->reimg ($key, $w, $h);
echo "<div style= ' float:left;border: #666666 1px dotted; Width: $w px; Height: $h px; padding:5px; margin:5px; ' ></div> ";
}
echo "</div>";
Die ("<br><br><label> <input name=" Go "type=" button "id=" Go "onclick=" location= ' index.php ' "" Value= "Www.111cn.net continues to process the new picture" ></label><br><br> ");
}
?>
<div style= "Clear:both; width:250px; height:110px; border: #006633 1px dashed; padding:5px; font-size:12px; Color: #009933 ">
<form name= "Form1" method= "Post" action= "Do=start" >
<label> Source directory:
<input name= "dir" type= "text" id= "dir" value= "<?= $c [' dir ']?> ' >
</label>
<br><br>
<label> Save to:
<input name= "Newdir" type= "text" id= "Newdir" value= "<?= $c [' Newdir ']?> '] >
</label>
<br><br>
<label> width &nbsp; High:
<input name= "W" type= "text" id= "W" value= "<?= $c [' W ']?>" size= "7" maxlength= "3" >
X
<input name= "H" type= "text" id= "H" value= "<?= $c [' H ']?>" size= "7" maxlength= "3" >
</label>
<br>
<br>
<label>
<input type= "Submit" name= "search" id= "search" value= "generation" >
</label>
</form>

</div>
&copy;2008 <a href= "Http://www.111cn.net" target=_blank>wbsifan</a>
</BODY>
</HTML>
?
<?php
Create a table of contents (directory, [mode])
function Mkdirs ($l 1, $l 2 = 0777) {
if (!is_dir ($l 1)) {
If the directory does not exist, recursion is established
Mkdirs (DirName ($l 1), $l 2);
Return @mkdir ($l 1, $l 2);
}
return true;
}

Save file (file, [content])
function SaveFile ($l 1, $l 2 = ') {
if (function_exists (file_put_contents)) {
File_put_contents ($l 1, $l 2);
} else{
$fp = @fopen ($l 1, ' WB ');
@fwrite ($FP, $l 2);
Fclose ($FP);
}
}

Get file suffix (file)
function Getfix ($l 1) {
Return End (Explode ('. ', $l 1));
}

is the allowable type (current, allowed)
function Checkfix ($l 1, $l 2) {
if (!is_array ($l 2)) {
$l 2 = Explode (', ', Str_replace (', ', $l 2));
}
Return In_array ($l 1, $l 2)? 1:0;
}

Class image{
Source Address
var $src;
New diagram path (after localization)
var $newsrc;

Allowed diagram types
var $allowtype = array ('. jpg ', '. gif ', '. png ', '. jpeg ');
Whether the thumbnail gif, 0 does not handle
var $regif = 0;
Whether to keep the source file (1 is reserved, 0 is MD5)
var $keep = 0;
If you can overwrite a picture that already exists, 0 is not covered
var $over = 0;

Picture Source Directory
var $dir;
Processed Table of Contents
var $newdir;


function __construct ($l 1=null, $l 2=null) {
$this->dir = $l 1? $l 1: "./images/temp";
$this->newdir = $l 2? $l 2: "./images/s";
}

function image ($l 1=null, $l 2=null) {
$this->__construct ($l 1, $l 2);
}

/**
* The file path after processing
* Save the source picture, MD5 file name to the new directory
*
* @ param rename ()
* The filename after the @ return process
*/
function ReName ($SRC) {
MD5 filename (for example: 3293okoe.gif)
$l 1 = substr (MD5 ($SRC), 10,10). STRRCHR ($src, ".");
File name after processing
$l 1 = $this->w. ' _ ' $this->h. ' _ '. $l 1;
Returns the address after processing
return $this->newdir. ' /'. $l 1;
}


/**
* Generate thumbnails
*
* @ param Mini (picture address, width, height, quality)
* If you want to keep the original file name, set $o->keep = 1;
* The image address after the @ return processing
*/
function Mini ($SRC, $w, $h, $q =80) {
$this->src = $src;
$this->w = $w;
$this->h = $h;

Whether to work with GIF images
if (STRRCHR ($SRC, ".") = = ". gif" && $this->regif = = 0) {
return $this->src;
}

Whether to keep the original file name, default does not hold
if (! $this->keep) {
Address of the renamed file
$NEWSRC = $this->rename ($SRC);
} else {
Keep it formerly known
$SRC = str_replace (' \ ', '/', $SRC);
$NEWSRC = $this-&GT;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);
}
Get Picture Properties
$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;
}
Working with thumbnails
$nim = Imagecreatetruecolor ($w, $h);
$k 1 = round ($h/$w, 2);
$k 2 = round ($oh/$ow, 2);
if ($k 1< $k 2) {
$oow = $ow;
$ooh = Round ($ow * $k 1);
$SW = 0;
$sh = ($oh-$ooh)/2;
}
else {
$oow = $oh/$k 1;
$ooh = $oh;
$SW = ($ow-$oow)/2;
$sh = 0;
}
Generate pictures
if (function_exists (imagecopyresampled)) {
Imagecopyresampled ($nim, $im, 0,0, $SW, $sh, $w, $h, $oow, $ooh);
}
else {
Imagecopyresized ($nim, $im, 0,0, $SW, $sh, $w, $h, $oow, $ooh);
}
Whether the processed directory exists
if (!is_dir ($this->newdir)) {
@mkdir ($this->newdir);
}
Save picture
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;
}
Returns the path after processing
return $NEWSRC;
}

/**
* Save the network picture
*
* @ param getimg (source map)
* This address (the MD5 file name of the local directory) after it is saved
*/
function getimg ($l 1) {
$l 2 = $this->dir. substr (MD5 ($l 1), 10,10). STRRCHR ($l 1, ".");
File exists, return address directly
if (File_exists ($l 2)) {
echo "Exits ...";
return $l 2;
}

Start getting the file and return to the new path
$img = file_get_contents ($l 1);
if ($img) {
if (!is_dir ($this->dir)) {
@mkdir ($this->dir);
}
SaveFile ($l 2, $img);
echo "File_get";
return $l 2;
}
}

/**
* Convert thumbnail www.111cn.net (filename and structure unchanged)
*
* @ param Mini (source address, width, height, quality)
* Address generated by @ return
*/

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.