Question

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!

Was it helpful?

Solution 2

As stated in the question comments, 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>';
}
?>

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top