Initialization failure PHP Bulk scale image code [INI parameter control]

Source: Internet
Author: User
Tags scale image
First use an INI file to set the size to be scaled, where the width or height of 0 is the picture zoomed in or out, all 0 or the original size, not 0 are stretched to the specified size.
Note: The INI file is interpreted as a comment file using PHP, and nothing is output, which is intended for security reasons. Instead, it is a comment for the INI file.
The INI file I set up is as follows:

Copy the Code code 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
*/
?>


Here is the PHP code written to scale the picture, where the variable classes is an array, you can select any of the settings specified in the INI file:

Copy the Code code as follows:


$oimg = "Test.jpg";//original image Name
$classes = Array (' translation ', ' autoheight ', ' autowidth ', ' Stretch ');//give classes for the new creating images ' size Which is 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,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);
}
?>

The above describes the initialization failure PHP bulk scale image code [INI parameter control], including the initialization failure aspects of the content, I hope that the PHP tutorial interested in the help of friends.

  • Related Article

    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.