0
             <?php
                ob_start();
                session_start();
                require_once('name.php');
                   if(!isset($_GET['e']))
                   {
                    header('HTTP/1.1 204 No Content');
                    exit();
                    }
                    if(empty($_SESSION['id']))
                    {
                    header('Location: login.php');
                        exit();
                    }
                    $id=$_GET['e'];

                    //database login information removed

                    $statement2=$db->prepare('select * from event2 where id=:id and userid=:uid');
                    $statement2->execute(array('id' => $id,
                               'uid' => $_SESSION['id']));
                    if($statement2->rowCount()==0)
                    {
                header('HTTP/1.1 204 No Content');
                exit();
                    }
                    $row=$statement2->fetch();
                    $image=$row['image'];
                    $realimage=imagecreatefromstring($image);


                header('Content-type: image/jpeg');
               $realimage;
                ob_end_flush();
                ?>

I'm receiving an "Image 'image link' cannot be displayed because it contains errors." I've tried changing the content header position around to various places and cannot get it to work. I've downloaded the image straight from the database and it does reproduce properly. When opening the page in chrome, I'm presented with a tiny little green stock picture.

Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80

2 Answers2

1

What about outputting the image?

imagejpeg($realimage);

You're missing an imagejpeg before the $realimage;. (imagejpeg or the other appropriate image* functions for outputting.)


By the way, why don't you output directly $image (via echo) instead of first parsing the image data as image?

bwoebi
  • 23,637
  • 5
  • 58
  • 79
  • 2
    Given that the `header` above it gives `image/jpeg`, maybe `imagejpeg` would be a better suggestion, though :) – Ry- Apr 20 '13 at 18:30
  • I tried the imagejpeg($realimage) and also echo($image) and neither of those worked. The reason that I am not outputting directly is because it is saved as text within the database (a requirement of the project) so the function imagecreatefromstring is required to do that, right? – user2302779 Apr 20 '13 at 21:05
  • Oh, and then once I pasted it my code still came out ugly, so I decided to trust you guys not to judge the awful indentation on here (although I should have known better) – user2302779 Apr 20 '13 at 21:09
  • @user2302779 No, it should work without imagecreatefromstring. Perhaps temporarily remove the header to see if there are any PHP errors. – bwoebi Apr 20 '13 at 21:10
  • I just see a ton of garble (hopefully image data) – user2302779 Apr 20 '13 at 21:14
  • Then everything is right. ... Btw. are you sure that you're using the right header? – bwoebi Apr 20 '13 at 21:15
  • It is another one of the requirements, so I suppose that it is the right one. I converted the image to a jpeg in another file using imagejpeg($data['file']['file']) and then added it to the db using file_get_contents($data['file']['file']) – user2302779 Apr 20 '13 at 21:23
  • @user2302779 When you're describing this I'm not sure anymore where the error is... when reading or when writing to the database... – bwoebi Apr 20 '13 at 21:26
  • Well I believe the writing part is correct, because I downloaded the text straight back from the database, added a .jpeg extension to it, and displayed perfectly on my computer, so I am fairly confident my error is in this file, I just thought I would give you that additional information unless there was a conflict that I was overlooking. – user2302779 Apr 20 '13 at 21:29
  • mh, no... I don't see any error when you output the image directly. (`echo $image;`) – bwoebi Apr 20 '13 at 21:31
  • So just to be clear, you took out imagecreatefromstring, left the header, put echo($image); directly under the end flush,and received no error? im sitting here running this thing in chrome and still receiving the same stock photo – user2302779 Apr 20 '13 at 21:39
  • @user2302779 try to put your image into a file, upload it, and replace the echo with a `readfile('path/to/image.jpg');`. Then you see if the error is in the image reading code or somewhere else. – bwoebi Apr 20 '13 at 21:48
  • Then I'd ike to know if there is no BOM at the beginning, whitespaces at the beginning and the end, text outputted by something else etc.? – bwoebi Apr 20 '13 at 21:57
  • I can confirm that there is no whitespace at the beginning/end, text outputted by anything else (that I know of,this is the entirety of the file.) I am working on this on my tablet and do not know of a way of finding the BOM settings on here, but I will attempt to remote log in to my desktop from my tablet and will let you know ASAP. – user2302779 Apr 20 '13 at 22:02
  • because with this error description, I think that your problem is that not only the image bytes are output. – bwoebi Apr 20 '13 at 22:08
  • I just changed the encoding from ansi to utf-8 without BOM and I'm still receiving the same error... – user2302779 Apr 20 '13 at 22:15
  • Then I fear that I'll be unable to help you without access to your file. (The raw file, no pastebin etc.) – bwoebi Apr 20 '13 at 22:26
  • Will you be online later tonight (around 11:30, Eastern Time) – user2302779 Apr 20 '13 at 22:30
  • Then it'll be 5:30 in my timezone... A bit too late in the night/morning ;-) – bwoebi Apr 20 '13 at 22:35
  • Ah, in what way would you like for me to get this file to you? – user2302779 Apr 20 '13 at 22:47
1

You have spaces before the <?php opening tag. Remove them.

Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
  • 2
    sure that the spaces aren't produced after putting the code in here? – bwoebi Apr 20 '13 at 18:28
  • @bwoebi: I don't know, ask the OP. ;) – Marcel Korpel Apr 20 '13 at 18:33
  • They are, this is my first time putting code on stack overflow and I was having an awful time trying to get my code to be compliant with the website's question creation wizard. It ended up with me pounding a bunch of tabs within notepad++ and calling it a day. – user2302779 Apr 20 '13 at 21:07