Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading an image through PHP

I'm trying to load an image through PHP, but I don't know how. The filename is stored in the database, such as image.jpg

if($_GET['image']){
    // Client requesting image, so retrieve it from DB
    $id = mysql_real_escape_string($_GET['image']);
    $sql = "SELECT * FROM $tbl_name WHERE id = '$id' LIMIT 1";
}

The client needs to request an image like so

http://example.com/index.php?image=1

And then it should return the image, so it can be embedded like this:

<img src="http://example.com/index.php?image=1" alt="" />

Is this possible?

like image 607
Adam Avatar asked Mar 31 '12 19:03

Adam


People also ask

How images can be created in PHP?

The imagecreate() function is an inbuilt function in PHP which is used to create a new image. This function returns the blank image of given size. In general imagecreatetruecolor() function is used instead of imagecreate() function because imagecreatetruecolor() function creates high quality images.


1 Answers

$img = 'path/to/image.jpg';
header('Content-Type: image/jpeg');
readfile($img);

just tested it

like image 58
joschua011 Avatar answered Sep 28 '22 09:09

joschua011