2016-02-19 48 views
2

已經搜索了一段時間,似乎無法完成此操作。我(試圖)學習php-gd,但由於某種原因,即使使用隨時可用的代碼片斷,我在文件運行時得到的所有內容都是一個小的,可能是20x20px的框。通過phpinfo()查看;和GD運行良好,我似乎無法找到任何錯誤的代碼,任何想法?PHP GD - 空格

<?php 
$redimg = imagecreatetruecolor(100, 100); 
$image = imagecreatefrompng('overlay.png'); 

// sets background to red 
$red = imagecolorallocate($redimg, 255, 0, 0); 
imagefill($redimg, 0, 0, $red); 

imagecopymerge($image, $redimg, 0, 0, 0, 0, 100, 100, 75); 
header('Content-type: image/png'); 
imagepng($image); 
imagedestroy($image); 
imagedestroy($redimg); 

?> 
+0

您的預期結果是什麼? – timclutton

回答

1

您會看到空白方塊,因爲存在由於Content-Type標頭而被抑制的錯誤。嘗試刪除標題以查看錯誤是什麼,或查看錯誤日誌。

另外,如果你正在學習PHP/GD我建議考慮進去一看我寫了一個庫,有效地封裝了許多的GD功能融入了更多人性化的API:SimpleImage

這個庫可以保存你很多時間,代碼和眼淚。

0

試試這個代碼,它應該正常工作

<?php 
$redimg = imagecreatetruecolor(100, 100); 
$red = imagecolorallocate($redimg, 255, 0, 0); 
imagefill($redimg, 0, 0, $red); 

$image = imagecreatefrompng('./cover.png'); 
imagesavealpha($image,true); 

imagecopyresampled($redimg, $image, 0, 0, 0, 0, 100, 100, 
imagesx($image), imagesy($image)); 

/** your code for writing to the response might not work in localhost 
because apache directly write entaire bytes to the response and most 
browsers do not like that. but it should surly work in the remote host **/ 
ob_start(); 
imagepng($redimg); 
$base64 = base64_encode(ob_get_clean()); 
$url = "data:image/png;base64,$base64"; 
echo "<body style='background: green' ><img src='$url' /></body>"; 

imagedestroy($image); 
imagedestroy($redimg); 

?> 

但如果你是高達更復雜的圖像處理任務,那麼你應該專注於一個良好的GD包裝,這使得事情變得簡單了你,一個我建議是ImageArtist由我寫的。

這裏是ImageArtist中的相同代碼。

$img = new Overlay(100,100,new Color(255,0,0)); 
$img2 = new Image("./cover.png"); 
$img2->resize(100,100); 

$img = $img->merge($img2,0,0); 
$img->dump();