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
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.