如何从图像资源创建图像

时间:2012-10-01 05:38:43

标签: php image-processing

我的代码错了..

我有一张图片。我把它分成几块。现在我将每个分割图像的资源存储在数组中。 问题:如何从这些资源创建图像。数组元素的数据类型是资源。

<?php
include "game_db.php";
  $img="6eplzcnl.png";
  echo"<img src='$img'>";

    $img=imagecreatefrompng($img);


     $width = imagesx($img);
    $height = imagesy($img);

    $tile_width =50;//$_POST['width'];
    $tile_height =50;// $_POST['height'];

    //find no. of tiles in columns and rows
     $tiles_x = (int) $width/ $tile_width;
    $tiles_y = (int) $height / $tile_height;
    header("Content type: image/png");
    //cut big image into tiles
    for ($i=0, $k=0;$i<$tiles_x;$i++)
        for ($j=0;$j<$tiles_y;$j++, $k++)
        {

            $tile[$k] = imagecreatetruecolor($tile_width, $tile_height);
            imagecopyresized($tile[$k], $img, 0, 0, $tile_width* $i, $tile_height * $j, $tile_width, $tile_height, $tile_width, $tile_height);
        }

    //find only unique tiles
    for ($i=0;$i<$k;$i++)
    {
        if (sizeof($res)==0)
        {    
          $res[0] = imagecreatetruecolor($tile_width, $tile_height);
            imagecopy ($res[0],$tile[$i],0,0,0,0,$tile_width, $tile_height);
        }
        else
        {
            //check if tile matches any in the res array
            for ($j=0;$j<sizeof($res);$j++)
            {
                if (tile_match($tile[$i], $res[$j])==true) break;

            }
            //if reached end of array then add the nex tile
            if ($j==sizeof($res))
            { $res[$j] = imagecreatetruecolor($tile_width, $tile_height);
                imagecopy ($res[$j],$tile[$i],0,0,0,0,$tile_width, $tile_height);
            }
        }
    }

    //save all res tiles into a large image
    $finalimg = imagecreate (sizeof($res) * $tile_width, $tile_height);
    for ($i=0;$i<sizeof($res);$i++)
    {   echo "<br>";
    echo  gettype($res[$i]);

    //$res[$i] = base64_decode($res[$i]);
     //$im = imagecreatefromstring($res[$i]);
    //if ($im !== false) {
   // header('Content-Type: image/png');
   //imagepng($res[$i]);
    //imagedestroy($res[$i]);
     //}



        //imagecopy ($finalimg, $res[$i], $i * $tile_width, 0, 0, 0, $tile_width, $tile_height);
    }


    // set name of file


    function tile_match($a, $b)
    {

        $awid = imagesx ($a);
        $ahei = imagesy ($a);

        $bwid = imagesx ($b);
        $bhei = imagesy ($b);

        if (($awid!=$bwid)||($ahei!=$bhei)) return false;

        for ( $i=0;$i<$ahei;$i++)
        {

        for($j=0;$j<$awid;$j++)
        {
             $rgb1 = imagecolorat($a,$i,$j);
                 $rgb2 = imagecolorat($b,$i,$j); 
                 if($rgb1==$rgb2){}
                 else 
                 {return false; }



        }

        }

        return true;
    }


?>

1 个答案:

答案 0 :(得分:0)