2013-02-15 143 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); 
?> 

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