最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to open a new tab with PHP - Stack Overflow

programmeradmin1浏览0评论

I'm trying to open a new tab with Php, then display an image. I found openWindow() function but it applies to Javascript. I'm trying to use only Php.

My Php code:

<?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 );
}
?>

Any help would be appreciated!

I'm trying to open a new tab with Php, then display an image. I found openWindow() function but it applies to Javascript. I'm trying to use only Php.

My Php code:

<?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 );
}
?>

Any help would be appreciated!

Share Improve this question edited Aug 2, 2013 at 1:30 Jonnny 5,03912 gold badges66 silver badges96 bronze badges asked Aug 2, 2013 at 1:24 TimeTime 232 silver badges9 bronze badges 7
  • 12 You can't do it as PHP is a server side language. You can write within PHP the JS to perform it though. – Jonnny Commented Aug 2, 2013 at 1:26
  • 2 PHP has no knowledge of, connection to, or control over the web browser. You'll have to use client-side functionality to influence the web browser's behavior. (In this case HTML and/or JavaScript.) – David Commented Aug 2, 2013 at 1:27
  • @Johny Do you have an example of this, Javascript within Php? Thanks. – Time Commented Aug 2, 2013 at 1:33
  • 1 @Time why don't you try just echo "<some javascript>"; – Hydra IO Commented Aug 2, 2013 at 1:34
  • 1 If you don't want to use Javascript then you'll have to develop a browser that will understand PHP! – Lee Taylor Commented Aug 2, 2013 at 1:34
 |  Show 2 more ments

2 Answers 2

Reset to default 5

PHP is a server-side language. This makes it impossible to open a new tab in the user's webclient without using Javascript.

Here are more information: Open a URL in a new tab (and not a new window) using JavaScript

As stated in the question ments, this is a browser / client side issue. PHP is a server side scripting language.

However, in you code, try this instead:

<?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>';
}
?>
发布评论

评论列表(0)

  1. 暂无评论