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