图片的分割与组合

类别:编程语言 点击:0 评论:0 推荐:
我这里只考虑jpg,gif,png格式.

<?php
/**
  * @author iwind [email protected]
  */   
class Image
{
    var $ext;
    var $i_ext;
    var $width;
    var $height;
    var $create_func;
    var $out_func;

    function image_load($files)
    {
        $file = (is_array($files))?$files[0]:$files;
        $info = getimagesize($file);
        switch ($info[2])
        {
            case "1":
                $this->ext = "gif";
                $this->i_ext = "gif";
                break;
            case "2":
                $this->ext = "jpg";
                $this->i_ext = "jpeg";
                break;
            case "3":
                $this->ext = "png";
                $this->i_ext = "png";
                break;
            default:
                $this->ext = "jpg";
                $this->i_ext = "jpeg";
        }
        $this->create_func = "imagecreatefrom" . $this->i_ext;
        $this->out_func = "image" . $this->i_ext;
        $this->width = $info[0];
        $this->height = $info[1];
    }

    function image_split($photo, $x_num=1, $y_num=1)
    {
        $this->image_load($photo);
        eval("\$im=" . $this->create_func . "(\$photo);");
        $x_width = $this->width/$x_num;
        $y_height = $this->height/$y_num;
        $im1 = imagecreate($x_width, $y_height);
        $files = array();
        for ($i=0; $i<$x_num; $i++)
        {
             for ($j=0; $j<$y_num; $j++)
            {
                $x = $i * $x_width;
                $y = $j * $y_height;
                imagecopyresampled($im1, $im, 0, 0, $x, $y, $x_width, $y_height, $x_width, $y_height);
                $new_file = "{$i}_{$j}.{$this->ext}";
                $files[] = $new_file;
                eval("{$this->out_func}(\$im1, \$new_file);");
            }
        }
        imagedestroy($im1);
        return $files;
    }

    function image_join($files, $new_file)
    {
        if (!empty($files))
        {
            $this->image_load($files);
            $i_arr = array();
            $j_arr = array();
            foreach ($files as $v)
            {
                list($v1) = explode(".", $v);
                list($i_arr[], $j_arr[]) = explode("_", $v1);
            }
            $i_arr = array_unique($i_arr);
            $j_arr = array_unique($j_arr);
            $x_num = count($i_arr);
            $y_num = count($j_arr);
            $im1 = imagecreate($x_num * $this->width, $y_num * $this->height);
            foreach ($i_arr as $i)
            {
                foreach ($j_arr as $j)
                {
                    $x = $i * $this->width;
                    $y = $j * $this->height;
                    eval("\$im = {$this->create_func}(" . "\"{$i}_{$j}.{$this->ext}\");");
                    imagecopyresampled($im1, $im, $x, $y, 0, 0, $this->width, $this->height, $this->width, $this->height);
                }
            }
            eval("{$this->out_func}(\$im1, \$new_file);");
        }
    }
}

//创建一个实例
$img = new image();

//将 20041099024319.jpg 分成 2 * 3部分,即宽两部分,高三部分
//$files 是文件名数组
$files = $img->image_split("20041099024319.jpg", 2 , 3);

//将分成的小图重新组起来,名称为 new.jpg
$img->image_join($files, "new.jpg");

//
echo "success......";
?>

本文地址:http://com.8s8s.com/it/it26047.htm