Home > Article > Backend Development > php generate thumbnail image_php generate thumbnail image code_PHP tutorial
php generate thumbnail image_php generate thumbnail image code This is a classic and practical PHP code for generating thumbnails. In professional terms, it is PHP code for generating thumbnails.
php tutorial to generate thumbnails_php generate thumbnail code
This is a classic and practical PHP code for generating thumbnails. In professional terms, it is PHP code for generating thumbnails.
*/
# Display graphics and connections
function showdir ($adirectory, $i)
{
global $browsedir;$start = $i;
# Change $maxcols and $maximages to change the number of rows and columns of small images displayed on each page.
$maxcols = 2;
$maximages = 6;
$maximages = $i + ($maximages - 3);# Change $imagemaxwidth and $imagemaxheight to change the width and height of the displayed thumbnail.
$imagemaxwidth = 100;
$imagemaxheight = 100;
# Calculate the ratio of height to width.$imagemaxratio = $imagemaxwidth / $imagemaxheight;
echo (img_start.$imagepath.img_width.$imageoutputwidth.img_height.$imageoutputheight.img_end);
$ndirectory = sizeof ($adirectory);
echo (table_start);
for ($i; $i<=$maximages;)
{
echo (row_start);
for ($icols=1; $icols<=$maxcols; $icols++)
{
echo (col_start);
$imagefilename = $adirectory[++$i];
If (strlen($imagefilename)>0)
{
$imagepath = $browsedir."/".$imagefilename;
$imagesize = getimagesize ($imagepath);
if ($imagesize)
$imagewidth = $imagesize[0];
$imageheight = $imagesize[1];
$imageratio = $imagewidth / $imageheight;
If ($imageratio > $imagemaxratio)
$imageoutputwidth = $imagemaxwidth;
$imageoutputheight = ceil ($imagemaxwidth/$imagewidth*$imageheight);
}
else if ($imageratio < $imagemaxratio)
$imageoutputheight = $imagemaxheight;
$imageoutputwidth = ceil ($imagemaxheight/$imageheight*$imagewidth);
} else
$imageoutputwidth = $imagemaxwidth;
$imageoutputheight = $imagemaxheight;
}
# Display graphics
echo (a_start.$imagepath.a_close);echo (line_break.$adirectory[$i]);
echo (a_end);}
echo (col_end);
}
}
echo (row_end);
}
echo (table_end);
pagemenu ($browsedir, $ndirectory, $start);
}
function pagemenu ($browsedir, $ndirectory, $pg) {echo "
page:"; $pg_num = 1;
for ($img_num = 0; $img_num <= $ndirectory;) {
if ($pg == $img_num) {
echo "# Establish links to other pages, each page displays four pictures, so every time the page number $pg_num increases by 1, $img_num increases by 4.
$pg_num = $pg_num + 1;
$img_num = $img_num + 4;}
echo "
n";}
function dirtoarray ($browsedir, $extensions)
{$nextensions = sizeof ($extensions);
#Main program
$idirectory = 0;
$directory = dir($browsedir);
while ($entry = $directory->read())
{
for ($i=1; $i<=$nextensions; $i++)
{
$compare = stristr ($entry, $extensions[$i]);
If (strlen($compare) == strlen($extensions[$i]))
$adirectory[++$idirectory] = $entry;
break;
}
}
}
$directory->close();
return $adirectory;
}
#Variable $browsedir is the location where the graphics file is placed.
$browsedir="./images";
# The extensions of graphics files that are allowed to be browsed are placed in the array and can be added by yourself.
$extensions[1] = "jpeg";
$extensions[2] = "jpg";
define ("line_break", "
$extensions[3] = "gif";
$extensions[4] = "png";
showdir (dirtoarray ($browsedir, $extensions), $start);
");define ("table_start", "
n");
n");
define ("table_end", "
define ("row_start", "n"); n");
define ("row_end", "
define ("col_start", "n "); n");
define ("col_end", "n
define ("img_start", "");
define ("img_width", " width=");
define ("img_height", " height=");
define ("a_start", '');
define ("a_end", "");
?>