2013-02-15 66 views
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

如果你能帮助它wouldbe不胜感激。

谢谢。

+2

很抱歉,但照片使它很难读你的问题。 .. – Tchoupi 2013-02-15 22:00:26

+0

你是什么意思? – 2013-02-15 22:01:31

+2

更新OP来减轻骨头:) – 2013-02-15 22:05:08

回答

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); 
?> 

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