xXREDXIIIXx Posted June 1, 2014 Share Posted June 1, 2014 (edited) OK you fantastic PHPfreakers I'm not sure how to describe this so I hope you will understand.. I have a predicament where I set the header info to: header('Content-Type: text/html; charset=utf-8'); but later on I have a part of my site that requests an image from somewhere else using getImage() here I need to set header("Content-type: image/jpeg"); in order for the image to show correctly. but this conflicts with the previous page of headers already sent. so I was looking for a way around this OR maybe can I load them in after the page has loaded? I have tried using Jquery to post with ajax but I don't seem to get any response the alert does not trigger for any conversation with an image. I have tested all of this and it works as long as I don't put any OUTPUT before triggering the getImage() <script type='text/javascript'> $(document).ready(function(){ function loadImages(){ $.ajax({ type: "POST", url: "/admin/getImage.php", data: {token: '<?php echo $token; ?>', chatId: '<?php echo $chat; ?>', uId: '<?php echo $uId; ?>'}, success: function(data){ alert(data); $('#img').html(data); } }); return false; } }); </script> here is part of the messages page that would show the image. (just imagine there was more content etc..) <?php header('Content-Type: text/html; charset=utf-8'); ## OTHER GENERIC STUFF $parser = new Parser($v1, $v2); $convo = $parser->getConvo($token, $id); foreach($convo['messageGroups'] as $con) { $chat = $con['messageGroupId']; $uId = $con['latestMessage']['messageUid']; $key = $con['latestMessage']['messageKind']; if($key == '3'){ $image = $parser->getImage($token, $chat, $uId); echo $image; } } ?> here is the function for getImages() public function getImage($token, $chat, $uId){ $this->initCURL(); $chatter = str_replace("{{chat}}", $chat, $this->url['image']); $chatter = str_replace("{{uId}}", $uId, $chatter); $headerData = $this->defaultGETHeader($token); curl_setopt($this->curl, CURLOPT_URL, $chatter); curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headerData); $output = curl_exec($this->curl); $this->closeCURL(); header("Content-type: image/jpeg"); return $output; } Edited June 1, 2014 by xXREDXIIIXx Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted June 1, 2014 Share Posted June 1, 2014 Setting the same header multiple times does not lead to conflcts. The only reason why you're getting an error message is because you have output before the second header() call, which is not allowed. Looks like you've forgotten the CURLOPT_RETURNTRANSFER. Quote Link to comment Share on other sites More sharing options...
xXREDXIIIXx Posted June 1, 2014 Author Share Posted June 1, 2014 Setting the same header multiple times does not lead to conflcts. The only reason why you're getting an error message is because you have output before the second header() call, which is not allowed. Looks like you've forgotten the CURLOPT_RETURNTRANSFER. Exactly, but I already knew WHY I was getting the error message, I was asking if there was a way around it. Like I said it executes mid-page and possibly multiple times depending if there is more than 1 image. Not exactly sure what CURLOPT_RETURNTRANSFER. does but I added it. was working fine before without it. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted June 2, 2014 Share Posted June 2, 2014 This simply makes no sense. It seems you misunderstand how HTTP and HTML works. An HTTP response contains a single resource with exactly one type: You can either send an HTML document or one(!) image, but not HTML mixed with multiple images or something like that. If you want to use images in HTML, you need an img element with a URL pointing to the image content: <img src="https://yoursite.com/images/cute_kitten.jpg" alt="cute kitten"> The image source may very well be a PHP script which outputs the image data dynamically: <img src="/admin/getImage.php?id=1234" alt="some sensible description"> I guess that's what you want. The getImage.php script would take the image ID from the URL and output this particular image. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.