Zen Cart图片处理质量设置及PHP批量调整文件夹内图片大小

网上流传的Zen Cart程序基本都存在一个问题:产品图片处理默认质量是最高的100,导致图片体积过大,空间不够用,页面加载过久。不知道是不是建站公司故意这样做,迫使客户增大虚拟主机空间。同一张图片由PHP处理生成相同尺寸的新图片,如果质量设置为100 新图片大小可能是100K,而质量设置为85的话,清晰度变化不大,大小可能只有20K。一般来说质量设置为80就可以了,再高一点建议不要超过85。

如何修改Zen Cart图片处理质量

首先登录后台 My Store > Images 中将几个Quality值设定为80

然后将下面这三个文件中列出的代码部分的quality设置为80

  • /includes/extra_configures/bmz_image_handler_conf.php
  • /includes/classes/bmz_image_handler.class.php
  • /admin/includes/modules/new_product_preview.php

(bmz_image_handler_conf.php和bmz_image_handler.class.php是涉及zen_image生成的在bmz_cache文件夹的不同尺寸的缓存图片 不同于zen_image_OLD

bmz_image_handler_conf.php

 $ihConf['default']['quality']   = 100; // about Line 19: 	

bmz_image_handler.class.php

 
		function resize_imageIM($file_ext, $dest_name, $bg, $quality = 100) {// about Line 332
		function resize_imageGD($file_ext, $dest_name, $bg, $quality = 100) {// about Line 450
		function save_imageGD($file_ext, $image, $dest_name, $quality = 100) {// about Line 636
		

new_product_preview.php

		imagejpeg ( $im_s, $destination_name_medium, 100 ); //  about Line 94   
		@imagejpeg ( $im_s, $destination_name_large, 100 ); //  about Line 137
		imagejpeg ( $im_s, $destination_name_small, 100 ); //  about Line 167
		

压缩Zen Cart老图片

照上面做,之后新添加的产品图片就不会出现体积过大的问题,但之前已经存在的体积过大的图片怎么瘦身呢?bmz_cache文件夹里的图片好说 直接删除了 程序会按需重新生成,但其它产品图片呢?之前是傻傻地把images文件夹打包下来用RIOT批量处理后再上传替换原来的图片,虽然是批量处理,但也只能一个一个文件夹地处理和上传…

Zen Cart产品图片有三个尺寸Small 85*85, Large 282*282, Very large 500*500,分别存放在images目录下的 s l v三个文件夹,每个文件夹又按月份存档产品图片,所以用上面的办法处理有些岁月的网站的图片,几万张图片,不容易…

后来发现也可以通过PHP在线批量处理,在网上找了份代码,不过这个代码只能处理指定目录内图片,不能遍历处理其子目录图片,自己想修改成可以遍历处理指定目录内所有图片及其子目录内所有图片,但实现不了。我只能稍稍修改了下,可以在线设置 源文件夹,目标文件夹,图片宽、高、质量。如果批量处理图片如果过多可能报错:Internal Server Error。这个小程序有点危险,自己知道地址就行了,我也不上演示地址了,看看截图吧,
resize-compress-zencart-images

代码如下(也可在百度网盘下载)。

<?php
/***
@JungleHi 20141210 
http://littlebar.tk/2014/12/10/
Source: http://www.sanwebe.com/2012/07/resize-multiple-images-in-a-folder-using-php
***/

ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);

//Maximize script execution time
ini_set('max_execution_time', 0);

//Function that resizes image.
function resizeImage($SrcImage,$DestImage, $MaxWidth,$MaxHeight,$Quality)
{
    list($iWidth,$iHeight,$type)    = getimagesize($SrcImage);
    $ImageScale             = min($MaxWidth/$iWidth, $MaxHeight/$iHeight);
    $NewWidth               = ceil($ImageScale*$iWidth);
    $NewHeight              = ceil($ImageScale*$iHeight);
    $NewCanves              = imagecreatetruecolor($NewWidth, $NewHeight);

    switch(strtolower(image_type_to_mime_type($type)))
    {
        case 'image/jpeg':
        case 'image/png':
        case 'image/gif':
            $NewImage = imagecreatefromjpeg($SrcImage);
            break;
        default:
            return false;
    }

    // Resize Image
    if(imagecopyresampled($NewCanves, $NewImage,0, 0, 0, 0, $NewWidth, $NewHeight, $iWidth, $iHeight))
    {
        // copy file
        if(imagejpeg($NewCanves,$DestImage,$Quality))
        {
            imagedestroy($NewCanves);
            return true;
        }
    }
}


if( isset($_REQUEST&#91;'ImagesDirectory'&#93;) ){
$ImagesDirectory    = $_REQUEST&#91;'ImagesDirectory'&#93;; //源文件夹 以/结尾 (要处理的图片所在的文件夹,Zen Cart产品图片存放在images目录下的 s l v三个文件夹,每个文件夹又按月份存档)
$DestImagesDirectory    = $_REQUEST&#91;'DestImagesDirectory'&#93;; //目标文件夹 以/结尾 (如果与源文件夹路径一致即可替换源图片)
$NewImageWidth      = $_REQUEST&#91;'NewImageWidth'&#93;; //新图片宽 Zen Cart默认大中小三种图片尺寸分别为small 85*85, large 282*282, very large 500*500
$NewImageHeight     = $_REQUEST&#91;'NewImageHeight'&#93;; // 新图片高
$Quality        = $_REQUEST&#91;'Quality'&#93;; //图片质量

/***
//Initial settings, Just specify Source and Destination Image folder.
$ImagesDirectory    = '/var/www/vhosts/domain.com/wwwroot/images/v/201401/'; //Source Image Directory End with Slash
$DestImagesDirectory    = '/var/www/vhosts/domain.com/wwwroot/images/v/201401R/'; //Destination Image Directory End with Slash
$NewImageWidth      = 500; //New Width of Image
$NewImageHeight     = 500; // New Height of Image
$Quality        = 80; //Image Quality
***/


if (!file_exists($DestImagesDirectory)) {
    mkdir($DestImagesDirectory, 0777, true); //如果目标文件夹不存在则创建之
	echo "$DestImagesDirectory does not exist, however we've created that folder for you<br />";
}


//some hints
echo '<b>Settings</b><br />ImagesDirectory: ';
echo $ImagesDirectory;
echo '<br />DestImagesDirectory: ';
echo $DestImagesDirectory;
echo '<br />NewImageWidth: ';
echo $NewImageWidth ;
echo '<br />NewImageHeight: ';
echo $NewImageHeight;
echo '<br />Quality: ';
echo $Quality;
echo '<hr />';
echo 'handling...';
echo '<hr />';

//Open Source Image directory, loop through each Image and resize it.
if($dir = opendir($ImagesDirectory)){
    while(($file = readdir($dir))!== false){

        $imagePath = $ImagesDirectory.$file;
        $destPath = $DestImagesDirectory.$file;
        $checkValidImage = @getimagesize($imagePath);

        if(file_exists($imagePath) && $checkValidImage) //Continue only if 2 given parameters are true
        {
            //Image looks valid, resize.
            if(resizeImage($imagePath,$destPath,$NewImageWidth,$NewImageHeight,$Quality))
            {
                echo $file.' resize Success!<br />';
                /*
                Now Image is resized, may be save information in database?
                */

            }else{
                echo $file.' resize Failed!<br />';
            }
        }
    }
    closedir($dir);
}

//display the form
echo
"<hr /><b>Resize Multiple Images in a Folder using PHP</b> JungleHi 20141209<hr>

<form method=post action='resize-multiple-images.php'>
ImagesDirectory:<input type='text' name='ImagesDirectory' value='$ImagesDirectory' style='width:1000px'></input>
<br>
DestImagesDirectory:<input name='DestImagesDirectory' type='text' value='$DestImagesDirectory' style='width:1000px'></input>
<br>
NewImageWidth:<input name='NewImageWidth' type='text' value='$NewImageWidth' style='width:100px'></input>
<br>
NewImageHeight:<input name='NewImageHeight' type='text' value='$NewImageHeight' style='width:100px'></input>
<br>
Quality:<input name='Quality' type='text' value='$Quality' style='width:100px'></input>
<br>
<input type='submit' value='Compress'></input>

</form>
";

}
else{
echo
"<b>Resize Multiple Images in a Folder using PHP</b> JungleHi 20141209<hr>

<form method=post action='resize-compress-zencart-images.php'>
ImagesDirectory:<input type='text' name='ImagesDirectory' value='/var/www/vhosts/domain.com/wwwroot/images/' style='width:1000px'></input>
<br>
DestImagesDirectory:<input name='DestImagesDirectory' type='text' value='/var/www/vhosts/domain.com/wwwroot/images/' style='width:1000px'></input>
<br>
NewImageWidth:<input name='NewImageWidth' type='text' value='500' style='width:100px'></input>
<br>
NewImageHeight:<input name='NewImageHeight' type='text' value='500' style='width:100px'></input>
<br>
Quality:<input name='Quality' type='text' value='80' style='width:100px'></input>
<br>
<input type='submit' value='Compress'></input>

</form>
";
}
?>

修改默认大中小图片尺寸

最后面再补充点东西关于Zen Cart产品图片上传时候自动生成的三个尺寸Small 85*85, Large 282*282, Very large 500*500,我个人觉得大图500*500还是有点小,800*800比较合适,可以修改/admin/includes/languages/english.php下面几行中数值为你想要的就行

  define('PRODUCT_MEDIUM_SMALL_WIDTH','85');
  define('PRODUCT_MEDIUM_SMALL_HEIGHT','85');
  define('PRODUCT_MEDIUM_IMAGE_WIDTH','282');
  define('PRODUCT_MEDIUM_IMAGE_HEIGHT','282');
  define('PRODUCT_MEDIUM_LARGE_WIDTH','500');
  define('PRODUCT_MEDIUM_LARGE_HEIGHT','500');

不会有同学问为可以直接用上面那个压缩ZEN CART老图片的小程序处理吧?这三个尺寸是在原图的基础上生成的,用那个小程序把500*500扩大到800*800,可是可以,但不清晰啊~

Add a Comment

Your email address will not be published.