Jump to content

<IMG src=.php> not working, need help


MrLarkins

Recommended Posts

OK, i've used it before, but for some reason it's not working.

 

I have 2 pages

 

PAGE 1  (bfbc2.php)

<?php

$url = 'http://api.bfbcs.com/api/pc';
$value = $_GET['playername'];

$postdata = "players=$value&fields=basic";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$data = curl_exec($ch);
curl_close($ch);

$data = json_decode($data,true);
$player = $data['players'];
$stuff = $player['0'];
$rank = $stuff['rank'];
echo ('<img src="http://www.oskgaming.com/images/bfbc2ranks/'.$rank.'.jpg" />');
?> 

 

PAGE 2 (test.html)

 

<img src="http://www.oskgaming.com/images/bfbc2.php?playername=Lark.s">

 

if I type http://www.oskgaming.com/images/bfbc2.php?playername=Lark.s  I get the image requested

 

if  type http://www.oskgaming.com/test.html I get a broken image.

 

Any ideas?  Did I make a mistake somewhere?

Link to comment
https://forums.phpfreaks.com/topic/213629-not-working-need-help/
Share on other sites

How?

 

the <IMG> points to the PHP.

 

the PHP accesses a database, pulls a value and displays jpg named by that pulled value.

 

How can I point the <IMG> at the jpg when I don't know which jpg to use?  There are 50 jpg's to choose from!

 

Yes, your img src isn't pointing to the file, its pointing to a URL.  You need to have it point directly to a .jpg/.gif/.png file.

 

but it IS point to the php file, and the php file outputs the img...so why is a broken image being displayed?

 

i must add that the PHP file runs flawlessly on its own...but when i try <img src=.php>, it shows a broken image

That won't work. If you point the src attribute to a php script you must output the image source, not the pathname. Your browser went to that script to load an image and all it got was a string url of an image, not the actual image data.

 

ok, your making more sense...so I need to change my php script

instead of having

echo ('<img src="http://www.oskgaming.com/images/bfbc2ranks/'.rank.'.jpg" />');

what should it be?  do i need to load all 50 image choices into an array?  do I need the header(Content: image/jpeg); in my script?

You can try instead of echoing the URL. Depending on your server, imagecreatefromjpeg may not allow you to get an image resource from a url.

//get image path like in above script

header('Content-Type: image/jpeg');
$im = imagecreatefromjpeg($img_url);
imagejpeg($im);
imagedestroy($im);

that did it...great catch...here's my final code

<?php

$url = 'http://api.bfbcs.com/api/pc';
$value = $_GET['playername'];

$postdata = "players=$value&fields=basic";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$data = curl_exec($ch);
curl_close($ch);

$data = json_decode($data,true);
$player = $data['players'];
$stuff = $player['0'];
$rank = $stuff['rank'];
$img_url = 'http://www.oskgaming.com/images/bfbc2ranks/'.$rank.'.jpg';
header('Content-Type: image/jpeg');
$im = imagecreatefromjpeg($img_url);
imagejpeg($im);
imagedestroy($im);

?> 

and the actual <img> tag resides on another page

 

much thanks!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.