PHP用文本制作透明的png

时间:2015-08-28 18:24:20

标签: php image png

我正在运行此代码,该代码会创建一个640x1136的透明png,并在左上角广告一个带有黑色文本的白色框。

运行代码时,会创建png,但会将其作为输出

返回
?PNG

IHDR?z??
        IDATx???1 ?Om
??>g???IEND?B`?

有人碰巧知道这是什么吗?

与此同时,有人会想到更短的方式吗?

$im = imagecreatetruecolor(640, 1136);
imagesavealpha($im, true);
$color = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $color);
imagepng($im);
imagesavealpha($im, true); // important to keep the png's transparency 
$text_color = imagecolorallocate($im, 0, 0, 0);
$width = 640; // the width of the image
$height = 1136; // the heighst of the image
$font_size = 20; // font size
$box_color = imagecolorallocate($im, 255, 255, 255);
// Set the offset x and y for the text position
$offset_x = 0;
$offset_y = 20;
$dims = imagettfbbox($font_size, 0, $font, $text);
$text_width = $dims[4] - $dims[6] + $offset_x;
$text_height = $dims[3] - $dims[5] + $offset_y;
// Add text background
imagefilledrectangle($im, 0, 0, $text_width, $text_height, $box_color);
// Add text
imagettftext($im, $font_size, 0, $offset_x, $offset_y, $text_color, $font,$text);
imagepng($im, $img, 0);

1 个答案:

答案 0 :(得分:1)

您应该使用您的数据发送正确的header('Content-Type: image/png');

php <yourscript>.php > filename.png

或者,如果您是从控制台尝试,并且希望将输出定向到PNG文件,则应将其作为imagepng($im);运行,或将imagepng($im, 'filename.png');更改为library(shiny) ui <- shinyUI(fluidPage( sidebarLayout( sidebarPanel( tabsetPanel( tabPanel(title = "Panel 1", br(), h4("some content"), checkboxInput(inputId = "B1", label = "Button 1", value = FALSE) ), tabPanel(title = "Panel 2", br(), h4("some content"), checkboxInput(inputId = "B2", label = "Button 2", value = FALSE) ) ) ), mainPanel( conditionalPanel(condition = "input.B1", h4("some content"), h1("Button1 is active") ), conditionalPanel(condition = "input.B2", h4("another content"), h1("Button2 is active") ) ) ) )) server <- shinyServer(function(input,output, session) { observe({ # if the first button is checked - uncheck the second button if (input$B1) updateCheckboxInput(session, inputId = "B2", value = FALSE) }) observe({ # if the second button is checked - uncheck the first button if (input$B2) updateCheckboxInput(session, inputId = "B1", value = FALSE) }) }) shinyApp(ui = ui, server = server) 您的脚本当前正在输出PNG到输出,您会看到原始PNG数据。

相关问题