PHP图片平均分割

/**
     * 图片平均分割
     * $h = 23, $w = 8
     * 保存:web/upload_pic/Cut
     */
    public function actionCutPng()
    {
        $filename = dirname(dirname(dirname(dirname(__FILE__)))).'\web\upload_pic\test114.png';
        list($width, $height) = getimagesize($filename);
        $h = 23;
        $w = 8;
        $newwidth = floor($width / $w);
        $newheight = floor($height / $h);
        $lastH = $height % $h;
        $lastW = $width % $w;

        $source = imagecreatefrompng($filename);
        for( $i=0 ; $i< $h; $i++ ){
            for ($j = 0 ; $j < $w ; $j++){
                $h_p = $newheight*$i;
                $w_p = $newwidth*$j;
                if(($j + 1) == $w ){
                    $newwidth += $lastW;
                }
                if((  $i + 1 ) == $h ){
                    $newheight += $lastH;
                }
                $thumb = ImageCreateTrueColor($newwidth, $newheight);
                imagecopyresized( $thumb, $source, 0, 0, $w_p, $h_p, $newwidth,  $newheight, floor($width / $w),floor($height / $h));

                imagejpeg( $thumb , "./upload_pic/Cut/{$i}-{$j}.jpg" ,100);
            }
        }
    }

 

posted @ 2018-07-16 18:09  LiuLiwei  阅读(189)  评论(0编辑  收藏  举报