-2

我正在尝试使用 Php 打开一个新选项卡,然后显示图像。我找到了 openWindow() 函数,但它适用于 Javascript。我试图只使用 Php

我的PHP代码:

<?php
function showImage() {
  $my_img = imagecreate( 200, 80 );
  $background = imagecolorallocate( $my_img, 0, 0, 255 );
  $text_colour = imagecolorallocate( $my_img, 255, 255, 0 );
  $line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
  imagestring( $my_img, 4, 30, 25, "Quote", $text_colour );
  imagesetthickness ( $my_img, 5 );
  imageline( $my_img, 30, 45, 165, 45, $line_colour );

  header( "Content-type: image/png" );
  imagepng( $my_img, "quote.png" );
  imagecolordeallocate( $line_color );
  imagecolordeallocate( $text_color );
  imagecolordeallocate( $background );
}
?>

任何帮助,将不胜感激!

4

2 回答 2

5

PHP 是一种服务器端语言。这使得在不使用 Javascript 的情况下无法在用户的 web 客户端中打开新选项卡。

以下是更多信息:使用 JavaScript 在新选项卡(而不是新窗口)中打开 URL

于 2013-08-02T01:27:29.037 回答
3

如问题评论中所述,这是浏览器/客户端问题。PHP 是一种服务器端脚本语言。

但是,在您的代码中,请尝试以下操作:

<?php
function showImage() {
    $my_img = imagecreate( 200, 80 );
    $background = imagecolorallocate( $my_img, 0, 0, 255 );
    $text_colour = imagecolorallocate( $my_img, 255, 255, 0 );
    $line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
    imagestring( $my_img, 4, 30, 25, "Quote", $text_colour );
    imagesetthickness ( $my_img, 5 );
    imageline( $my_img, 30, 45, 165, 45, $line_colour );

    //header( "Content-type: image/png" );
    imagepng( $my_img, "quote.png" );
    imagecolordeallocate( $line_color );
    imagecolordeallocate( $text_color );
    imagecolordeallocate( $background );

    // You should call this too.
    imagedestroy($my_img);
    echo '<a href="quote.png" target="_blank">View the image</a>';
    // Or, you could try:
    // echo '<html><body><script>window.open('quote.png','_blank')</script></body></html>';
}
?>
于 2013-08-02T01:33:19.893 回答