嘟嘟嘟嘟的图像

时间:2013-02-15 21:58:28

标签: php image image-manipulation

所以我希望能够混淆图像以扭曲原始图像。我的意思是这个。加载图像,遍历图像并获取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

如果你能提供帮助,我们将不胜感激。

感谢。

1 个答案:

答案 0 :(得分: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);
?>

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