A php batch-generated thumbnail code

Source: Internet
Author: User

<? Php tutorial
$ Oimg = "test.jpg"; // original image name
$ Classes = array ('translation', 'autoheight', 'autowidth', 'Stretch '); // give classes for the new creating images 'size which are defined in the specified ini file
$ Suffix = 'jpg '; // the new image's suffix
$ Inifile = 'image. ini. php ';

$ Size = getimagesize ($ oimg );
$ X = $ size [0]/$ size [1];
$ Name = explode ('.', $ oimg );

If (! File_exists ($ inifile) die ('ini file does not exist! ');
$ Cn = parse_ini_file ($ inifile, true); // parse the class style image size from ini file
Foreach ($ classes as $ class ){
Foreach ($ cn as $ k => $ v ){
If ($ k = $ class ){
If ($ v ['width'] & $ v ['height']) {
$ Thumbwidth = $ v ['width'];
$ Thumbheight = $ v ['height'];
} Elseif ($ v ['width']) {
$ Thumbwidth = $ v ['width'];
$ Thumbheight = round ($ thumbwidth/$ x );
} Elseif ($ v ['height']) {
$ Thumbheight = $ v ['height'];
$ Thumbwidth = round ($ thumbheight * $ x );
} Else {
$ Thumbwidth = $ size [0];
$ Thumbheight = $ size [1];
}
Break;
}
}
If (! Isset ($ thumbheight )&&! Isset ($ thumbwidth) die ('ini file settings error! ');

$ Nimg = $ name [0]. '_'. $ class. '.'. $ suffix; // new image file name
$ Source = imagecreatefromjpeg ($ oimg );
$ Thumb = imagecreatetruecolor ($ thumbwidth, $ thumbheight );
Imagecopyresampled ($ thumb, $ source, 0, 0, 0, $ thumbwidth, $ thumbheight, $ size [0], $ size [1]);

If ($ suffix = 'jpg ') $ method = 'imagejpeg ';
Else $ method = 'image'. $ suffix;
$ Method ($ thumb, $ nimg );
Imagedestroy ($ thumb); // release the image source
Imagedestroy ($ source );
}
?>

Php code for Image Scaling. The variable classes is an array. You can select the settings specified in any number of INI files:

This is an ini configuration file. The above code will read the image location in this file, which can be multiple.

<? Php /*
; Translate the image format using the original image size
[Translation]
Width = 0
Height = 0

; Stretch the image to the specified size
[Stretch]
Width = 800
Height = 600

; Zoom the image to the specified width with height auto size
[Autoheight]
Width = 740
Height = 0

; Zoom the image to the specified height with width auto size
[Autowidth]
Width = 0
Height = 380
*/?>

Note: When the INI file is interpreted as a php file, there is no output, which is intended for security reasons. The annotation of the INI file.

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.