Dynamic Image for User ID
How do I create a system where I provide userID
as a variable GET and output an image that can be returned?
Eg. I am specifying the ID ashttp://ccvg.net/man/findstatus.php?id=2
I have images already created and I am currently delivering 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 use a tag <img>
to take it to another page.
source to share
Create a new page that you will use to upload to the image file and allow GET
. Also, make sure you set the content type image/png
. Make sure that you have also installed GD Library
<?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);
source to share
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">';
}
}
source to share
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'] . '" />';
}
}
source to share