大规模处理图片两边的黑边

刘德胜 129次浏览

摘要:<?php functionwhcke($filename,$img){ $mime=$img['mime']; $w=$img[0];//宽; $h=$img[1];//高; $wl=$wr=$hs=$hx=0; if($mime=="image/pn...

<?php 
function whcke($filename,$img){
    $mime = $img['mime'];
    $w    = $img[0];//宽;
    $h    = $img[1];//高;
    $wl = $wr =  $hs = $hx = 0;
    if($mime=="image/png"){
       $im = @imagecreatefrompng($filename);
    }else{
       $im = @imagecreatefromjpeg($filename); 
    }
    //左宽
    for($i=0;$i<$w;$i++){
        $rgb = ImageColorAt($im, $i, 0);
        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = $rgb & 0xFF;
        $rgba = $r.$g.$b;
        if($rgba>0){
            $wl = $i+12;
            break;
        }
    }
    //右宽
    for($i=$w-1;$i>1;$i--){
        $rgb = ImageColorAt($im, $i, 1);
        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = $rgb & 0xFF;
        $rgba = $r.$g.$b;
        if($rgba>0){
            $wr = $i-12;
            break;
        }
    }
    //上
    for($i=0;$i<$h;$i++){
        $rgb = ImageColorAt($im, $w/2, $i);
        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = $rgb & 0xFF;
        $rgba = $r.$g.$b;
        if($rgba>0){
            $hs = $i+10;
            break;
        }
    }
    //上
    for($i=$h-1;$i>1;$i--){
        $rgb = ImageColorAt($im, $w/2, $i);
        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = $rgb & 0xFF;
        $rgba = $r.$g.$b;
        if($rgba>0){
            $hx = $i-10;
            break;
        }
    }
    return array($wl,$wr,$hs,$hx,$im);
}
//图片去两边黑色边的程序
set_time_limit(0);
function funimga($f){
    $filename = "new/".$f.'.jpg';
    $img_info = getimagesize($filename);
    $w        = $img_info[0];//原宽
    // $n_w      = $w-($w/100)*32;//新宽
    // $xw       = ($w/100)*16;
    $n_w      = $w-($w/100)*18;//新宽
    $xw       = ($w/100)*9;    
    //创建图片
    $dst_im = imagecreatetruecolor($n_w, $img_info[1]);
    //源图像
    if($img_info['mime']=="image/png"){
        $src_im = @imagecreatefrompng($filename);
    }else{
       $src_im = @imagecreatefromjpeg($filename); 
    }
    //拷贝源图像左上角起始 150px 150px
    imagecopy( $dst_im, $src_im, -$xw, 0, 0, 0, $img_info[0],$img_info[1]);
    //输出拷贝后图像
    imagejpeg($dst_im,"new/$f.jpg");
    imagedestroy($dst_im);
    imagedestroy($src_im);
}
function funimg($f){
    $filename = "old/".$f.'.jpg';
    $img_info = getimagesize($filename);
    $wh = whcke($filename,$img_info);
    //创建图片
    $dst_im = imagecreatetruecolor($wh[1]-$wh[0], $wh[3]-$wh[2]);
     imagecolorallocate($dst_im, 255, 255, 255);
    //拷贝源图像左上角起始 150px 150px
    imagecopy( $dst_im, $wh[4], 0, 0, $wh[0], $wh[2], $wh[1],$wh[3]);
    //输出拷贝后图像
    imagejpeg($dst_im,"new/$f.jpg");
    imagedestroy($dst_im);
    imagedestroy($wh[4]);
}
function read_all_dir ( $dir ){ 
    $result = array();
    $handle = opendir($dir);//读资源
    if ($handle){
        while (($file = readdir($handle)) !== false ){
            if ($file != '.' && $file != '..'){
                $cur_path = $dir . DIRECTORY_SEPARATOR . $file;
                if (is_dir($cur_path )){//判断是否为目录,递归读取文件
                    $result['dir'][$cur_path] = read_all_dir($cur_path );
                }else{
                    $result['file'][] = $cur_path;
                }
            }
        }
        closedir($handle);
    }
    return $result;
}
 
$file =read_all_dir('E:\liudeshengaa\zf\old');
foreach($file['file'] as $v){
    $s = 'E:\liudeshengaa\zf\old\\';
    $f = str_replace("$s", "", $v) ;
    $f = str_replace(".jpg", "", $f) ;
    echo funimg($f);
}
$file =read_all_dir('E:\liudeshengaa\zf\new');
foreach($file['file'] as $v){
    $s = 'E:\liudeshengaa\zf\new\\';
    $f = str_replace("$s", "", $v) ;
    $f = str_replace(".jpg", "", $f) ;
    echo funimga($f);
}
echo "ok";