Create image from url: multiple problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Thew
    New Member
    • Aug 2010
    • 69

    #1

    Create image from url: multiple problems

    Code:
    <?php
    if(isset($_GET['img']) && is_numeric($_GET['img'])){ 
    $img = $_GET['img']; 
    
    $imgarray = array ( 
    '1' => 'http://www.path/to/image1.png', 
    '2' => 'http://www.path/to/image2.png', 
    '3' => 'http://www.path/to/image3.png'
    );
    
    $src = $imgarray[$img];
    header('Content-type: image/png');
    echo file_get_contents($src);
    }
    else
    {
        header('Content-type: image/png');
    echo 'Image could not be loaded';
    }
    ?>
    Hello again bytes! Im having multiple problems. 1: When the $_GET['img'] is set and its numeric, the image will be displayed right, but i want to add text in the upper-right corner of the image... How can i do that? I've looked through multiple GD tutorials and examples but i can't find my answer. 2: When $_GET['img'] isn't set i want to display the text: Image could not be loaded. How cna i do that? Because this doesn't seem to work...
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    in both cases you need to apply imagecreatefrom string() or a combination of this with other GD functions. mind that you have to return an image, not text.

    Comment

    • johny10151981
      Top Contributor
      • Jan 2010
      • 1059

      #3
      think simple:

      you dont need to print the content of the image by yourslef.

      you can do this:
      Code:
      <?php
      if(isset($_GET['img']) && is_numeric($_GET['img'])){ 
      $img = $_GET['img']; 
       
      $imgarray = array ( 
      '1' => 'http://www.path/to/image1.png', 
      '2' => 'http://www.path/to/image2.png', 
      '3' => 'http://www.path/to/image3.png'
      );
       
      $src = $imgarray[$img];
      echo "<img src='".$src."' alt='".$src."'>";
      }
      else
      {
       echo 'Image could not be loaded';
      }
      ?>
      if a specific file is missing and you want to print some text then you must not set the Content-type. just print the missing message.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        although that code has to be put in the HTML page …

        Comment

        • Thew
          New Member
          • Aug 2010
          • 69

          #5
          I searched on PHP.net and after some time i found this:

          Code:
          $text = base64_decode('Image could not be loaded!');
          	
          	$im = imagecreatefromstring($text);
          	header('Content-Type: image/png');
          	imagepng($im);
          	imagedestroy($im);
          But that doesn't seem to work at me? And the text in the upper-right corner? How can i do that?
          EDIT: I searched through the GD Functions and imagestring() worked. But now i still want to place my text in the upper-right corner when $_GET['img'] is set...

          Comment

          Working...