2012-04-23 38 views
0

好的,在previous question of mine中,我發現我正在使用的瀏覽器由於某種原因不支持toDataURL()。我試圖使用像todataur-png-js這樣的解決方法,但無濟於事,因爲它也失敗了。我遇到了這樣的可能性,我可以讓PHP根據畫布中的像素數據構建圖像並將其保存爲圖像。但是,我對畫布沒有太多的經驗,也不知道如何做到這一點。 任何想法?在正確的方向微調也可以。 =)從像素陣列創建圖像服務器端

+0

todataur-png-js似乎是一個很好的方向。你嘗試了什麼?它是如何失敗的? – Cal 2012-04-23 17:03:58

+0

@Cal有一個頁面(這裏是[http://dest.at/tdu/draw.html])供您測試,但瀏覽器不會將背景設置爲包含畫布數據的圖像。它沒有做任何事情。 =/ – Viewtiful 2012-04-23 17:08:02

+0

它適用於我,使用最新的Chrome。你使用的是什麼瀏覽器?它給了什麼錯誤? – Cal 2012-04-23 17:09:13

回答

1

如果你已經有了一個像素陣列,您可以使用的GD庫的imagecreatetruecolor()imagesetpixel()功能做到這一點:

// Assume $pixelArray is a 2-dimensional array of colors for the pixels 

// Grab the dimensions of the pixel array 
$width = count($pixelArray, 0); 
$height = count($pixelArray); 

// Create the image resource 
$img = imagecreatetruecolor($width, $height); 

// Set each pixel to its corresponding color stored in $pixelArray 
for ($y = 0; $y < $height; ++$y) { 
    for ($x = 0; $x < $width; ++$x) { 
     imagesetpixel($img, $x, $y, $pixelArray[$y][$x]); 
    } 
} 

// Dump the image to the browser 
header('Content-Type: image/png'); 
imagepng($img); 

// Clean up after ourselves 
imagedestroy($img); 

如果你想保存圖像到磁盤,而不是轉儲到的瀏覽器,簡單地忽略調用header()和傳遞路徑imagepng()作爲第二個參數:

imagepng($img, '/some/path/to/your/desired/image.png'); 
+0

嗯,帶「imagesetpixe()」的行會拋出錯誤「無法使用字符串偏移量作爲數組」。 – Viewtiful 2012-04-26 17:21:38

+0

@Viewtiful:'$ pixelArray'必須是你的像素數組(你在問題標題中提到你有像素數組)。 – FtDRbwLXw6 2012-04-26 18:03:41

+0

我做過了嗎?無論如何,我只需將getImageData()。數據的值發送到服務器?再次,我幾乎沒有使用油畫的經驗。抱歉。 = / – Viewtiful 2012-04-30 17:45:28

1

目前還不清楚,如果你能夠在像素數據來獲得在的Netfront眉畫布SER。

從您的示例頁面,更改按鈕處理程序代碼:

var cv=document.getElementsByTagName('canvas')[0];document.documentElement.style.background='url('+cv.toDataURL()+')'; 

要這樣:

var cv=document.getElementsByTagName('canvas')[0];alert(cv.toDataURL()); 

是否顯示在彈出的alert()任何像素數據?

如果是這樣,那麼您似乎不能在此瀏覽器的背景圖片中使用數據網址。如果沒有,則看起來您的畫布不支持引擎蓋下的getImageData()