Php code for batch scaling images [ini parameter control]

Source: Internet
Author: User
Php has a function to process images. php can also scale images with high requirements. First, use an INI file to set the size to be scaled. if the value is 0 in width or height, the image is zoomed in or out. if the value is 0, the original size is used, if none of them are 0, pull them to the specified size.

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.

An example of the INI file I set is as follows:

The code is as follows:


/*
; 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
*/
?>


The following is the php code for zooming images. the variable classes is an array. you can select the settings specified in any number of INI files:

The code is as follows:


$ 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 );
}
?>

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.