Code _php base of PHP batch generation thumbnail

Source: Internet
Author: User
Disadvantages: Long width of the picture will be stretched deformation, can not be intelligent cutting, need intelligent cutting, please study for yourself.
<?php
$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-> <a href= ' {$file} ' > $file </a>". 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. = "-> 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 File
Imagejpeg ($to, $config [' prefix ']. $file);
$op. = "Done-> created thumb: <a href= ' {$config [' prefix ']}{$file} ' >{$config [' prefix ']}{$file}</a> ']. ENDL;
$done + +;
}
}
}
Closedir ($handle);
$op. = "Fin-> {$done} file (s) written." ENDL;
echo "<pre>";
Echo $op;
echo "</pre>";
Exit
?>

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.