iT邦幫忙

0

php GD合成問題

<?php
header('Content-type: image/jpeg');

$im = imagecreatetruecolor(320, 240);
$np = imagecreatetruecolor(160, 120);
$np2 = imagecreatetruecolor(160, 120);
$img1 = imagecreatefromjpeg('bg/test001.jpg');
$img2 = imagecreatefrompng('bg/test002.png');
$img3 = imagecreatefrompng('bg/test003.png');

imagealphablending($np, false);
imagecopyresampled($np, $img2, 0, 0, 0, 0, 160, 120, 320, 240);
imagealphablending($np2, false);
imagecopyresampled($np2, $img3, 0, 0, 0, 0, 160, 120, 320, 240);

imagecopy($im, $img1, 0, 0, 0, 0, 320, 240);
imagecopy($im, $np, -40, -40, 0, 0, 160, 120);
imagecopy($im, $np2, -20, -20, 0, 0, 160, 120);
imagecopy($im, $np, 0, 0, 0, 0, 160, 120);
imagecopy($im, $np2, 20, 20, 0, 0, 160, 120);
imagecopy($im, $np, 40, 40, 0, 0, 160, 120);
imagecopy($im, $np2, 60, 60, 0, 0, 160, 120);
imagecopy($im, $np, 80, 80, 0, 0, 160, 120);
imagecopy($im, $np2, 100, 100, 0, 0, 160, 120);
imagecopy($im, $np, 120, 120, 0, 0, 160, 120);
imagecopy($im, $np2, 140, 140, 0, 0, 160, 120);
imagecopy($im, $np, 160, 160, 0, 0, 160, 120);

$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
imagettftext($im, 36, 0, 45, 140, $black, 'fonts/arial.ttf', 'Here I am.');
imagefttext($im, 36, 0, 44, 139, $white, 'fonts/arial.ttf', 'Here I am.');

imagejpeg($im);

imagedestroy($img1);
imagedestroy($img2);
imagedestroy($img3);
imagedestroy($im);
imagedestroy($np);
imagedestroy($np2);
?>
請問img1 ,img2...怎樣用另外一個用PHP GD 產生的圖案代替?
謝謝!

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

4
fillano
iT邦超人 1 級 ‧ 2010-10-06 09:39:10
最佳解答

你都已經用了imagecreatetruecolor()了...請你解釋一下需求好嗎?不然看不出你碰到了什麼問題。

看更多先前的回應...收起先前的回應...
leoyuen iT邦新手 5 級 ‧ 2010-10-06 15:19:04 檢舉

這個程式沒有問題,我是想用"bg/test001.jpg"改成用是由另一支程式PHP GD所產生的圖案代替,
$phpimg="GD所產生的圖案" ; 這行應該怎樣寫?
$img1 = imagecreatefromjpeg($phpimg);

謝謝!

leoyuen iT邦新手 5 級 ‧ 2010-10-07 22:38:16 檢舉

我問題是"bg/test001.jpg"改成用由另一支程式PHP GD所產生的圖案代替",
試過這樣寫:$phpimg="testimg.php" ;
$img1 = imagecreatefromjpeg($phpimg);
但不成功,應該怎樣寫?
請邦忙,
謝謝!

leoyuen iT邦新手 5 級 ‧ 2010-10-09 01:47:11 檢舉

Fillano 大師在嗎?,明白我的意思?麻煩你邦邦忙。

感謝!感謝!

fillano iT邦超人 1 級 ‧ 2010-10-10 14:24:41 檢舉

疑?我忘記了,抱歉。

簡單的做法,就是$im = imagecreatetruecolor($width, $height),然後用其他函數在$im上畫圖。最後要輸出時,可以使用imagepng($im)或是imagejpeg($im)等函數,看你需要使用怎樣的格式。

leoyuen iT邦新手 5 級 ‧ 2010-10-12 00:58:56 檢舉

還是沒有解決,假設有兩個畫圖PHP檔 Aimg.php 和 Bimg.php
Aimg.php是用合成圖像,現在要用其中一個圖案:
$img2 = imagecreatefrompng('test002.png');
test002.png 改成用Bimg.php 已經產出來的圖案。

leoyuen iT邦新手 5 級 ‧ 2010-10-12 01:06:45 檢舉

在網頁可以當一個圖像看$phpimg="Bimg.php" ; <IMG src="$phpimg" >
但在合成圖像不能這樣寫:
$phpimg="Bimg.php" ;
$img1 = imagecreatefromjpeg($phpimg);
正確要怎樣?

fillano iT邦超人 1 級 ‧ 2010-10-12 22:23:47 檢舉

你想要做的事情是做不到的。比較好的方法是把程式寫得更有彈性,這樣Aimg.php與Bimg.php才有機會協作。

例如你可以把程式拆成兩個部份:繪圖邏輯、輸出。繪圖邏輯只負責產出$im,要怎麼處理$im再交由其他程式決定。輸出部份只負責接收$im,然後根據需求直接輸出,或是存到檔案等。

做好這樣的分工,接下來可以把Bimg.php中處理繪圖的邏輯作成一個函數,你可以用參數來控制這個函數,函數會返回$im。如果你要在其他程式中使用,只要include/require Bimg.php,然後呼叫這個函數就可以了。

如果單一函數還沒有足夠的彈性,那你可能還得把函數拆的更細。看你實際應用狀況而定。

leoyuen iT邦新手 5 級 ‧ 2010-10-13 07:48:35 檢舉

Thank you !

我要發表回答

立即登入回答