我已經參考了PHP手冊來嘗試和反轉圖像的顏色,但我無法讓圖像以我需要的方式顯示。用imagepng()內聯顯示圖像
本質上,在一個WordPress循環中,我需要拍攝一張圖像,將其反轉,然後將div的背景圖像設置爲該反轉圖像。這是我的代碼到目前爲止:
<?
if (have_rows("slideshow")):
while (have_rows("slideshow")):
the_row();
$icon = get_sub_field("icon");
$image = get_sub_field("image");
?>
<button data-slide="<? echo $image["url"] ?>">
<div class="icon" style="background-image:url('<? echo $icon["url"] ?>');">
<?
function negate($im) {
if (function_exists("imagefilter")) {
return imagefilter($im, IMG_FILTER_NEGATE);
}
for ($x = 0; $x < imagesx($im); ++$x) {
for ($y = 0; $y < imagesy($im); ++$y) {
$index = imagecolorat($im, $x, $y);
$rgb = imagecolorsforindex($index);
$color = imagecolorallocate($im, 255 - $rgb["red"], 255 - $rgb["green"], 255 - $rgb["blue"]);
imagesetpixel($im, $x, $y, $color);
}
}
return(true);
}
$im = imagecreatefrompng($icon["url"]);
if ($im && negate($im)) {
echo "Image successfully converted to negative colors.";
imagepng($im);
imagedestroy($im);
}
?>
<!--<span style="background-image:url('img/icon-circle-white.png');"></span>-->
</div><!--/.icon-->
<div class="caption">
<h2><? the_sub_field("title"); ?></h2>
<? the_sub_field("caption"); ?>
</div><!--/.caption-->
</button>
<?
endwhile;
endif;
?>
這個工程,但它吐出了一堆奇怪的字符,而不是圖像。在我看來,問題是imagepng()
需要header("Content-type: image/png");
,但我不能這樣做,因爲這是在一個WordPress循環內,而不是一個單獨的文件。
我的想法是外部圖像反轉的東西,並運行對,我在環(例如指定每一個形象,單獨的PHP:<img src="/invert.php?url=<? $icon['url'] ?>" />
不幸的是,我不知道該怎麼做
是這樣的。 ?可能
您可以使用'ob_get_contents'緩存,並將圖像base64編碼的內容嵌入到'data:image/png; base64,___'中。 – Deadooshka
有趣,我會給它一個鏡頭。你能指點我一個教程嗎?我對PHP不太好。 – JacobTheDev
您是通過worldpress上傳器上傳圖片,還是隻是將它們拉到服務器上的URL或其他目錄中,以便將它們嵌入到您的文章中?如果你沒有智能地設置背景顏色(你似乎沒有,因爲你有circle-white.png)和背景爲什麼不是我們CSS3的反轉(100%)過濾器,因爲倒置的圖像是一樣的負。除非您需要稍後保存圖像。 – Jem