3

How can I make a system where I provide a userID as a GET variable and output an image that can be returned?

Eg. I give ID as http://ccvg.net/man/findstatus.php?id=2

I have some images already created, and I currently deliver the images using

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        if($row["Status"] == 0){
            echo '<img src="img/currently-available.png" alt="Currently Available" height="30" width="127">';
        }
        if($row["Status"] == 1){
            echo '<img src="img/short-leave.png" alt="Short Leave" height="30" width="89">';
        }
        if($row["Status"] == 2){
            echo '<img src="img/long-leave.png" alt="Long Leave" height="30" width="89">';
        }
        if($row["Status"] == 3){
            echo '<img src="img/semi-permanent-leave.png" alt="Semi-Permanent Leave" height="30" width="160">';
        }
    }
} else {
    echo "Status not found";
}

I want to be able to use a <img> tag to bring it up on another page.

AkshayP
  • 2,141
  • 2
  • 18
  • 27
phonetic
  • 85
  • 1
  • 10
  • At first: use /> at the end of image tags. Second: Use switch($row['Status']) instead of if statements. Third: What do you mean exactly? – Tyr Dec 31 '14 at 12:20
  • most likely related http://stackoverflow.com/questions/1851849/output-an-image-in-php – Kevin Dec 31 '14 at 12:25
  • 1
    @Tyr When using HTML 5 (and this is standard today) you don't close tags like IMG so using `/>` at end of IMG tag is not recommended. – Volvox Dec 31 '14 at 12:30

3 Answers3

3

Create a new page that you'll use to load in your image file, and allow for a GET. Also, make sure you set the content type to image/png. Make sure you also have the GD Library installed

<?php
Header("Content-Type: image/png");  
//Fetch data using $_GET 
//...
$image = file_get_contents("img/undefined.png"); //new image
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        switch($row["Status"]) {
          case 0 :
             $image = file_get_contents("img/currently-available.png");
          break;
          case 1 :
             $image = file_get_contents("img/short-leave.png");
          break;
          case 2 :
             $image = file_get_contents("img/long-leave.png");
          break;
          case 3 :
             $image = file_get_contents("img/semi-permanent-leave.png");
          break;
          default :
             $image = file_get_contents("img/undefined.png"); //new image
          break;
        }
} 

$im = imagecreatefromstring($image);
imagepng($im);
imagedestroy($im);
ʰᵈˑ
  • 11,279
  • 3
  • 26
  • 49
0

Use the following code:

if($_GET['id'] != '') { 
 $id = $_GET['id'];
 if($id == 0){
            echo '<img src="img/currently-available.png" alt="Currently Available" height="30" width="127">';
        }
 if($id == 1){
            echo '<img src="img/short-leave.png" alt="Short Leave" height="30" width="89">';
        }
 if($id == 2){
            echo '<img src="img/long-leave.png" alt="Long Leave" height="30" width="89">';
        }
 if($id == 3){
            echo '<img src="img/semi-permanent-leave.png" alt="Semi-Permanent Leave" height="30" width="160">';
        }
}
John Snow
  • 61
  • 6
0

imagestatus.php

<?php
$array= array(
    'Currently Available',
    'Short Leave',
    'Long Leave',
    'Semi-Permanent Leave',
);

if(isset($_GET['Status']) && isset($array[$_GET['Status']])){
  dynamic($array[$_GET['Status']]);
}else{
   dynamic("Status not found");
}

function dynamic($text){    
    // Create a strlen($text)+5*30 image
    $im = imagecreate(strlen($text)+5, 30);

    // White background and blue text
    $bg = imagecolorallocate($im, 255, 255, 255);
    $textcolor = imagecolorallocate($im, 0, 0, 255);

    // Write the string at the top left
    imagestring($im, 5, 0, 0, $text, $textcolor);

    // Output the image
    header('Content-type: image/png');

    imagepng($im);
    imagedestroy($im);
}
?>

Example

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
       echo '<img src="imagestatus.php?Status=' . $row['Status'] . '" />';
   }
}
Buse Gönen
  • 238
  • 1
  • 5
  • Although nice, OP would ideally (and I'm assuming) want to use the images they already have and not use text - for user experience sake. (Would also sanitize the `$_GET['text']`) – ʰᵈˑ Dec 31 '14 at 12:34
  • You can keep clean or sanitize the $_GET['text']. I give an example. – Buse Gönen Dec 31 '14 at 12:37