php批量縮放圖片的代碼[ini參數(shù)控制]
更新時(shí)間:2011年02月11日 02:26:19 作者:
php有專(zhuān)門(mén)處理圖片的函數(shù),對(duì)于一些要求較高的圖片縮放,php也能做到。
首先使用一個(gè)ini文件來(lái)設(shè)置要縮放的大小,其中為寬或高0的則為圖片放大或縮小,都為0則還是原大小,都不為0都拉抻成指定的大小。
注意:ini文件使用php解釋時(shí)為注釋文件,什么也沒(méi)有輸出,這是為了安全起見(jiàn)而故意為之。而;則是ini文件的注釋。
我設(shè)置的ini文件例子如下:
<?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
*/
?>
下面是編寫(xiě)的縮放圖片的php代碼,其中變量classes是一個(gè)數(shù)組,可以選擇任意多個(gè)ini文件中指定的設(shè)置:
<?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 = 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);
}
?>
注意:ini文件使用php解釋時(shí)為注釋文件,什么也沒(méi)有輸出,這是為了安全起見(jiàn)而故意為之。而;則是ini文件的注釋。
我設(shè)置的ini文件例子如下:
復(fù)制代碼 代碼如下:
<?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
*/
?>
下面是編寫(xiě)的縮放圖片的php代碼,其中變量classes是一個(gè)數(shù)組,可以選擇任意多個(gè)ini文件中指定的設(shè)置:
復(fù)制代碼 代碼如下:
<?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 = 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);
}
?>
相關(guān)文章
PHP最常用的ini函數(shù)分析 針對(duì)PHP.ini配置文件
php的配置函數(shù)就是幾個(gè)ini_*的函數(shù),主要是針對(duì)配置文件的操作,其實(shí)就四個(gè)函數(shù):ini_get、ini_set、ini_get_all、ini_restore。個(gè)人感覺(jué)最有用的就是ini_set和ini_get。2010-04-04PHP中使用foreach和引用導(dǎo)致程序BUG的問(wèn)題介紹
PHP 引用有些類(lèi)似 C 語(yǔ)言指針, 但一些重要的特性和 C 語(yǔ)言指針不一樣, 如果不注意, 會(huì)導(dǎo)致程序 BUG. foreach 操作的是數(shù)組或?qū)ο蟮目截? 但 PHP5, 可以使用引用操作對(duì)象元素本身2012-09-09PHP實(shí)現(xiàn)無(wú)限極分類(lèi)生成分類(lèi)樹(shù)的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)無(wú)限極分類(lèi)生成分類(lèi)樹(shù)的方法,結(jié)合實(shí)例形式簡(jiǎn)單分析了無(wú)限極分類(lèi)的原理與實(shí)現(xiàn)方法,涉及PHP數(shù)組遍歷與判斷相關(guān)操作技巧,需要的朋友可以參考下2017-09-09php判斷當(dāng)前操作系統(tǒng)類(lèi)型
這篇文章主要介紹了php判斷當(dāng)前操作系統(tǒng)類(lèi)型,分為兩種情況一種是服務(wù)器端,一種是客戶(hù)端,感興趣的小伙伴們可以參考一下2015-10-10php使用Swoole與WebSocket實(shí)現(xiàn)彈幕效果的示例代碼
在本文中,我們將深入探討如何使用Swoole與WebSocket結(jié)合,實(shí)現(xiàn)彈幕效果,并著重強(qiáng)調(diào)需要注意的關(guān)鍵地方,以確保我們的彈幕系統(tǒng)能夠高效、穩(wěn)定地運(yùn)行,感興趣的朋友可以參考下2024-02-02