1

I want to return an image over an URL like http://placehold.it/500x500. I have my URL http://example.inc/assets/image/35345, which calls an action on controller. The controller get some data (name, id, etc.) from database and also a binary string of the image content.

On the frontend site, i have my img tag, where i want to call the url in my src attribute.

<img src="http://example.inc/assets/image/35345">

Some more information, i use slim PHP Framework and my server is an ubuntu 13.x system (vagrant etc.). I am an typically frontend developer and dont have good skills @ PHP.

Following snippets works:

$file = fopen($name, 'wb');
fwrite($file, $binaryData);
fclose($file);

but I dont want to generate files in a directory. Is this possible?

EDIT: Content-Type and Content-Length Headers are set, that is not the problem.

Chris Incoqnito
  • 235
  • 3
  • 12
  • what you want to achive. Do you want to show uploaded image.I found this question little confusing. – kammy Mar 30 '15 at 06:55
  • i want the same result that http://plachold.it/500x500 gives me, that the url gives me an image, i want to give the img tag an url which returns an image. – Chris Incoqnito Mar 30 '15 at 08:25

3 Answers3

0

Grab the contents of the image, base_64 encode it, then return a a base64 image.

$file = file_get_contents($name);
list($width, $height, $type, $attr) = getimagesize($file);
echo '<img src="data:image/'.$type.';'.base64_encode($file).'"/>';
Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110
  • i dont have a file, i only have a database entry which contains a filename, like "200.jpg" and filecontent = "djhfkagkjhvköfghfsajhdhgdöoa[...]" binary data. – Chris Incoqnito Mar 30 '15 at 08:23
0
  1. You should upload images in directory by using something like this. This code will upload your image in directory.

if ($_FILES['file']['name'] != "") {
            $filename = $_FILES['file']['name']; //getting name of the file from form
            $filesize = $_FILES['file']['size'];
            $info = new SplFileInfo($filename);
            $ext = $info->getExtension();
            $filesize1 = ($filesize * .0009765625) * .0009765625;
            if (!($ext == 'jpg' || $ext == 'png' || $ext == 'jpeg')) {
                //set some error message and redirect
            }
            if ($filesize1 >= 5.0) {
                 //set message image size should be less than 5 mb
            }

            $target_path = $_SERVER['DOCUMENT_ROOT'] . "../images/profile_images/";
            move_uploaded_file($_FILES['file']['tmp_name'], "$target_path" . $_FILES['file']['name']) or
                    die("Could not copy file!");
        }
  1. Insert image name(with extension) in database.($filename here)
  2. Fetch image name from database and store in variable($profile_image here),use it in img src.

<a href='../images/profile_images/$profile_image'><img alt='Avatar' src='../images/profile_images/$profile_image'></a>
  1. You can use only Anchor tag to redirect user on image in another tab in browser.

hope this answer will help you.

kammy
  • 352
  • 2
  • 10
0

Because i had an mssql database with iso charset i have converted all of my results to utf-8, the problem was, that the bytestring also converted to utf-8.

after non converting the bytestring i also returned the bytestring and set the header content type to image/extension

Chris Incoqnito
  • 235
  • 3
  • 12