php批量缩放图片的代码[ini参数控制]
时间:2018-12-30 分类:PHP 作者:编程之家
首先使用一个 ini文件 来设置要缩放的大小,其中为宽或高0的则为图片 放大或缩小,都为0则还是原大小,都不为0都拉抻成指定的大小。 注意:ini文件 使用PHP 解释时为注释文件 ,什么也没有输出 ,这是为了安全起见而故意为之。而;则是ini文件 的注释。 我设置的ini文件 例子如下: <div class="codetitle"><a style="CURSOR: pointer" data="17840" class="copybut" id="copybut17840" onclick="doCopy('code17840')"> 代码 如下:
<div class="codebody" id="code17840">
<?
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 /
?>
下面是编写的缩放
图片 的
PHP 代码 ,其中变量classes是
一个 数组,可以选择任意多个ini
文件 中指定的设置:
<div class="codetitle">
<a style="CURSOR: pointer" data="9554" class="copybut" id="copybut9554" onclick="doCopy('code9554')"> 代码 如下: <div class="codebody" id="code9554">
<?
PHP $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 = ge
timage size($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);
image
cop yresampled($thumb,$source,$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);
}
?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。