1

所以我希望能够将图像弄乱以扭曲原始图像。我的意思是这个。加载图像,遍历图像并获取 32x32 块并将每个单独的块存储在数组中。然后将它们重新组合成一张新图片,其中的块以随机顺序排列。

这是我目前必须从原始图像中获取和存储块然后创建重新组装图像的代码(注意这还没有随机化部分)。但由于某种原因,它不能正确输出。

<?php

$name = "pic.jpg";

$src = imagecreatefromjpeg($name);
list($width, $height, $type, $attr) = getimagesize($name);

$x_size = floor($width/32);
$y_size = floor($height/32);

$mixed = array();

$new_image = imagecreatetruecolor(32,32);

$x = 0;
$y = 0;

for($y = 0; $y < $height; $y+= 32) {
    for($x = 0; $x < $width; $x+=32) {
        imagecopy($new_image, $src, 0, 0, $x, $y, 32, 32); 
        array_push($mixed, $new_image);
    }
}

$final_image = imagecreatetruecolor($width, $height);

$i = 0;
$x1 = 0;
$y1 = 0;

for($i = 0; $i < sizeof($mixed); $i++) {

    $x1++;

    if($x1 >= $x_size) {
        $x1 = 0;
        $y1++;
    }

    imagecopymerge($final_image, $mixed[$i], $x1, $y1, 0,0,32,32,100); 
}

header('Content-Type: image/jpeg');
imagejpeg($final_image);

?>

原图:

http://puu.sh/236XS

输出:

http://puu.sh/236YO

如果您能提供帮助,将不胜感激。

谢谢。

4

1 回答 1

0

我使用以下代码解决了我自己的问题:

<?php
include("test.php");
//global variables
$name = "pic.jpg";
$size = 64;
$x = 0;
$y = 0;

$spots = array("0,0", "0,1", "0,2", "0,3",
               "1,0", "1,1", "1,2", "1,3",
               "2,0", "2,1", "2,2", "2,3",
               "3,0", "3,1", "3,2", "3,3");

//open image from file (Original image)
$src = imagecreatefromjpeg($name);
//load image details
list($width, $height, $type, $attr) = getimagesize($name);

//calculate amount of tiles on x/y axis.
$x_size = floor($width/$size);
$y_size = floor($height/$size);

$new_image = imagecreatetruecolor($size,$size);
$final_image = imagecreatetruecolor($width, $height);
$used = array();

for($y = 0; $y < $height; $y+= $size) {
    for($x = 0; $x < $width; $x+= $size) {

            //generate random x/y coordinates
            redo:
            $spot = rand(0, sizeof($spots)-1);

            if(!in_array($spot, $used)) {
                $coords = explode(",", $spots[$spot]);

                //grab 32x32 square from original image
                imagecopy($new_image, $src, 0, 0, $x, $y, $size, $size); 
                //place 32x32 square into new image at randomly generated coordinates
                imagecopy($final_image, $new_image, $coords[0]*$size, $coords[1]*$size, 0,0,$size,$size); 
                array_push($used, $spot);

            } else {
                goto redo;
            }

    }
}

//display final image

header('Content-Type: image/jpeg');
imagejpeg($final_image);

print_r($used);
?>

可能不是最有效的代码,但它可以工作:)

于 2013-02-16T02:56:48.240 回答