Home > Backend Development > PHP Tutorial > Generate thumbnails PHP code to batch generate thumbnails

Generate thumbnails PHP code to batch generate thumbnails

WBOY
Release: 2016-07-29 08:38:23
Original
959 people have browsed it

Disadvantages: Pictures with different lengths and widths will be stretched and deformed, and cannot be intelligently cropped. If you need intelligent cropping, please do your own research.
$config = array();
$config['path'] = "./";
$config['t_width'] = 120;
$config['t_height'] = 98;
$config['ignore'] = array("",".","..");
$config['prefix'] = "thumb_";
$done = 0;
define("IMAGE_JPG", 2 );
define("ENDL", "n");
if($handle = opendir($config['path'])) {
while(false !== ($file = readdir($handle))) {
if(!array_search($file,$config['ignore'])) {
list($im_width, $im_height, $type) = getimagesize($file);
if($type != IMAGE_JPG) {
continue;
}
$op .= "found -> $file" . ENDL;
$im = @imagecreatefromjpeg($file);
if (!$im) {
$op .= "fail -> couldn't create sour image pointer." . ENDL;
continue;
}
if(file_exists($config['prefix'] . $file) | | substr($file, 0, strlen($config['prefix'])) == $config['prefix']) {
$op .= "note -> this file has already got a thumbnail." . ENDL;
continue;
}
$to = imagecreatetruecolor($config['t_width'],$config['t_height']);
if(!$to) {
$op .= "fail -> couldn' t create dest image pointer." . ENDL;
continue;
}
if(!imagecopyresampled($to, $im, 0, 0, 0, 0, $config['t_width'], $config['t_height'] , $im_width, $im_height)) {
$op .= "fail -> couldn't create thumbnail. php fail." . ENDL;
continue;
}
//Save the file
imagejpeg($to, $config ['prefix'] . $file);
$op .= "done -> created thumb: {$config[' prefix']}{$file}" . ENDL;
$done++;
}
}
}
closedir($handle);
$op .= "fin -> {$done} file( s) written" . ENDL;
echo "

"; <br>echo $op; <br>echo "
";
exit;
?>

The above has introduced the code for generating thumbnails in batches with PHP, including the content of generating thumbnails. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template