Unable to display image provided by API

A

Anonymous

Guest
Hi all...
First thing to say; I'm learnt PHP more than ten years ago, and have barely used it since. Instead, I've been working as a paramedic, so nothing IT related at all! and currently off work with the COVID so my brain isn't working well; please be kind!

I'm trying to write an app for a local search and rescue (SAR) team. At the moment, I'm trying to give them the ability to query their HR system from the app, via a RESTful API, and allow them to choose a skillset, say "Rapid Water Rescue", and have it show them who is qualified in that area.

I'm pleased to say that I've managed to do that, but I'm struggling to get it to display the SAR team members photo alongside it. I think I'm getting the same problem as https://www.sitepoint.com/community/t/php-generated-images-show-up-as-gibberish-in-browser/4591, but can't figure out from the replies how to fix it.

I understood:
1) That the img src should point to a seperate PHP file, instead of say a function and
2) The header needed to be set = header('Content-Type: image/jpeg');

I've included both in my code, but can't get either to work.

If I try to do it via a function on the same page, I get this (Printed from the $d4h variable at the end of the function):
Code:
����JFIF��>CREATOR: gd-jpeg v1.0 (using IJG JPEG v80), default quality ��C    $.' ",#(7),01444'9=82<.342��C  2!!22222222222222222222222222222222222222222222222222���w"�� ���}!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������

This is the image data isn't it?

So how do I get that to display as an image?
If I set the header('Content-Type: image/jpeg'), I just get 'header already set', and it doesn't print the image anyway (even if I remove the original header).

Thanks so much for looking.

Following is the Skillsets.php
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<html lang="en">
<head>	
	<link rel="stylesheet" type="text/css" href="styles.css" />
  <title>Team Qualifications</title>
</head>
	<body>
		<?php			
			////////////////////////////// Dependant Functions /////////////////////////////////////////////////////////
		function CheckStatus($ID) //Used to check status of team member and print if active
		{		
	
			//API URL	
			$url = 'https://api.d4h.org/v2/team/members/' . $ID;

			//Collect
			$d4h = \Httpful\Request::get($url)
				->expectsJson()
				->addHeader('Authorization', 'Bearer ****AUTH_TOKEN****')
				->send();				
		
			//Convert JSON Object to PHP array
			$obj2 = json_decode($d4h);		

			if($obj2->statusCode == 200)
			{	
				//Try printing image via function
				GetImage($ID);
	
				//Try printing image via seperate PHP file
				$ImgSource = '"GetImage.php?ID=' . $ID;
				echo ' <div align="center"><img src="' . $ImgSource . '" width="200" height="200"></div>';

				//Print team member name
				echo '<p>' . ucwords(strtolower($obj2->data->name)) . '</p><br><br><br>';
			}
		}

		function GetImage($ID)
		{
			//header('Content-Type: image/jpeg');
	
			//API URL	
			$url = 'https://api.d4h.org/v2/team/members/' . $ID . '/image?size=thumbnail';
	
			//Collect
			$d4h = \Httpful\Request::get($url)
				->addHeader('Authorization', 'Bearer ****AUTH_TOKEN****')
				->send();
	
			$obj2 = json_decode($d4h);	
	
			//This creates the text dump
			echo $d4h;
			
			//imagecreatefromjpeg($d4h);
	
		}
			///////////////////////////////////// Main Content /////////////////////////////////////////////////
		
			include('httpful.phar'); 
			////////////////////////////////////// Collect Course List & Present Form //////////////////////////////////////	
		
			//API URL			
			$url = 'https://api.d4h.org/v2/team/qualifications?sort=title:asc';
			// ' . $_GET['courseID'] . '/qualified-members?sort=name:asc&state=qualified';
		
			//echo $url . '<br>';				
			//Collect
			$d4h = \Httpful\Request::get($url)
                    ->expectsJson()
					->addHeader('Authorization', 'Bearer ****AUTH_TOKEN****')
                    ->send();				
		
			//Convert JSON Object to PHP array
			$obj2 = json_decode($d4h);	
		
			//Find how many data entries exist in the data element of the PHP array
			foreach ($obj2 as $key => $value) {
				if($key == 'data'){$repeat=count($value);}
			}
		?>
		<div align ="center">
		<form method="POST" action='skillsets.php'> 
		<div class="select_join"><select name="courseID">
		<option value = "">---Select Qualification---</option>
			<?php
				for($a=0;$a<$repeat;$a++)
			{				
					//Print list of courses to select from
					echo "<option value='" . $obj2->data[$a]->id . "'><h2>" . $obj2->data[$a]->title . "</h2></option>";
			}
			?>			
			<br><div class="container">
			<div class="center">
			<input type="submit" width=400px>	
			</div>
			</div>
		</form>
			</div><br>			
		<?php 

				
			////////////////////////////////////// List Team Members //////////////////////////////////////
			$coursename = $_POST['courseName']; 
			$course = $_POST['courseID'];

			//Collect course name 
			//API URL			
			$url = 'https://api.d4h.org/v2/team/qualifications/' . $course;
		
			//echo $url . '<br>';				
			//Collect
			$d4h = \Httpful\Request::get($url)
                    ->expectsJson()
					->addHeader('Authorization', 'Bearer ****AUTH_TOKEN****')
                    ->send();				
		
			//Convert JSON Object to PHP array
			$obj2 = json_decode($d4h);	
			
			//Assign coursename
			$coursename =  $obj2->data->title;	
			
			//Print title
			if($coursename != NULL){echo '<h2>Team Members Qualified in ' . $coursename .  '</h2><br>';}
			
			
			
			//Print list of team members 
					
			//API URL			
			$url = 'https://api.d4h.org/v2/team/qualifications/' . $course . '/qualified-members?sort=name:asc&state=qualified';
		
			//Collect
			$d4h = \Httpful\Request::get($url)
                    ->expectsJson()
					->addHeader('Authorization', 'Bearer ****AUTH_TOKEN****')
                    ->send();				
		
			//Convert JSON Object to PHP array
			$obj2 = json_decode($d4h);	
			
			//Find how many data entries exist in the dats element of the PHP array
			foreach ($obj2 as $key => $value) {
				if($key == 'data'){$repeat=count($value);}
			}
		
			//Print required info
			for($a=0;$a<$repeat;$a++)
			{				
				//Check if user is active, and get photo (if possible)
				CheckStatus($obj2->data[$a]->id);
			}				
			?></p>
	</body>
</html>


Following is GetImage.php
Code:
<?php header('Content-Type: image/jpeg');

	$ID = $_GET['ID'];
	
	//API URL	
	$url = 'https://api.d4h.org/v2/team/members/' . $ID . '/image?size=thumbnail';
	
	//Collect
	$d4h = \Httpful\Request::get($url)
		->addHeader('Authorization', 'Bearer ****AUTH_TOKEN****')
		->send();

	imagecreate($d4h)	

?>
 
This thread was posted one and half of year ago, the account of OP is not active, why did you asked a question when the OP is not able to answer?
 
Back
Top