2012-05-28 59 views
0

我使用html2canvas從此代碼生成屏幕抓圖jpg。不過,我不能針對一個particaular div,所以我抓住整個屏幕。來自base64_decode的PHP裁剪圖像

$canvasImg = $_POST['img']; 
$data = base64_decode($canvasImg); 
$File = "z.jpg"; 
$Handle = fopen($File, 'w'); 
fwrite($Handle, $data); 
fclose($Handle); 


問題:我怎麼能裁剪圖像?

這是我attampt

$canvasImg = $_POST['img']; 
$image = base64_decode($canvasImg); 
$dest_image = 'z.jpg'; 
$img = imagecreatetruecolor('200','150'); 
$org_img = imagecreatefromstring($image); 
$ims = getimagesize($image); 
imagecopy($img,$org_img, 0, 0, 20, 20, 200, 150); 
imagejpeg($img,$dest_image,90); 
imagedestroy($img); 



但即時得到一個錯誤
Warning: getimagesize(�PNG ) [<a href='function.getimagesize'>function.getimagesize</a>]: failed to open stream: Invalid argument

+0

您正在尋找imagecreatefromstring()http://php.net/manual/en/function.imagecreatefromstring.php –

+0

你是不是傳遞一個圖像到imagecreatefrompng(),有一定的誤差與圖像變量 –

+0

重編輯我的問題,實現'imagecreatefromstring()' –

回答

0

getimagesize($image);的第一個參數必須是圖像的文件名。

+1

如果你想在飛行中這樣做,那麼'imagesx($ image)'和'imagesy($ image)'是你需要的,以返回x和y的大小值適當。 – Death

1

你想imagecreatefromstring();,不imagecreatefrompng();。這將變成一個PHP圖像對象,然後你可以輸出爲JPEG使用imagejpeg();

1

您的問題強調,您正在傳遞的PNG緩衝區的功能。因此,爲什麼你得到一個PNG的幻數(89 50 4e 47 0d 0a 1a 0a --- OR --- 0x89「PNG」CR LF 0x1A LF)。您需要將文件保存到臨時位置,並將位置傳遞給getimagesize等。因此,他們爲什麼會抱怨流。

您可以使用imagecreatefromstring(...)這將採取緩衝區並輸出資源的句柄。