PHP Batch generated thumbnail code _php tutorial

Source: Internet
Author: User
Disadvantage: The long and wide picture will be stretched deformation, can not be intelligent cutting, the need for intelligent cutting, 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-and couldn ' t create sour image pointer." ENDL;
Continue
}

if (file_exists ($config [' prefix ']. $file) | | substr ($file, 0, strlen ($config [' prefix '])) = = = $config [' prefix ']) {
$op. = "Note--and this file has already got a thumbnail." ENDL;
Continue
}
$to = Imagecreatetruecolor ($config [' t_width '], $config [' t_height ']);
if (! $to) {
$op. = "Fail-and 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-and couldn ' t create thumbnail. PHP fail. ". ENDL;
Continue
}

Save File
Imagejpeg ($to, $config [' prefix ']. $file);
$op. = "Done-created thumb: {$config [' prefix ']}{$file}". ENDL;
$done + +;
}
}
}
Closedir ($handle);
$op. = "Fin-and {$done} file (s) written". ENDL;
echo "
";  
Echo $op;
echo "
";
Exit
?>

http://www.bkjia.com/PHPjc/319200.html www.bkjia.com true http://www.bkjia.com/PHPjc/319200.html techarticle disadvantage: Long and wide picture will be stretched deformation, can not be intelligent cutting, the need for intelligent cutting, please do your own research. PHP $config =array (); $config [' Path ']=./'; $config [' T_width ']=120 ...

  • 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.