How to read an image with PHP?

Go To StackoverFlow.com

6

i know that

$localfile = $_FILES['media']['tmp_name'];

will get the image given that the POST method was used. I am trying to read an image which is in the same directory as my code. How do i read it and assign it to a variable like the one above?

2012-04-05 18:49
by NoName
Your example just reads the filename, not the contents of the image. What is it you're trying to do - Cal 2012-04-05 18:52


11

The code you posted will not read the image data, but rather its filename. If you need to retrieve an image in the same directory, you can retrieve its contents with file_get_contents(), which can be used to directly output it to the browser:

$im = file_get_contents("./image.jpeg");
header("Content-type: image/jpeg");
echo $im;

Otherwise, you can use the GD library to read in the image data for further image processing:

$im = imagecreatefromjpeg("./image.jpeg");
if ($im) {
  // do other stuff...
  // Output the result
  header("Content-type: image/jpeg");
  imagejpeg($im);
}

Finally, if you don't know the filename of the image you need (though if it's in the same location as your code, you should), you can use a glob() to find all the jpegs, for example:

$jpegs = glob("./*.jpg");
foreach ($jpegs as $jpg) {
  // print the filename
  echo $jpg;
}
2012-04-05 18:52
by Michael Berkowski
What is the difference between these two? $localfile = $FILES['media']['tmpname']; $filename = $_FILES['media']['name'] - NoName 2012-04-05 18:54
One is the filename of the file stored on your server (tmp_name), the other was the original filename on the client computer that uploaded the file (name) - Cal 2012-04-05 18:55
@Mozammil name points to the files original name as uploaded by the user. tmp_name is a filename assigned by PHP in temporary space, which points to the actual file data. It only exists until the script terminates, then tmp_name is no longer valid - Michael Berkowski 2012-04-05 18:56
@Mozammil No, I have no experience with SimpleImag - Michael Berkowski 2012-04-05 19:00
But say i have a file named image.jpg which is stored in my server, same directory. How do i get its filename, the same as $localfile = $FILES['media']['tmpname'] - NoName 2012-04-05 19:06
@Mozammil my first or third example above. If you know the filename, you just store it in a variable $filename = "./image.jpg"; if you don' tknow the filename, retrieve it with glob()Michael Berkowski 2012-04-05 19:08
and if i echo $localfile what would it output - NoName 2012-04-05 19:21
@Mozammil It would output whatever you put into it. If it holds a filename, it will output a filename, not the file data. If you call the file in with file_get_contents() echoing it out will output the actual binary file data - Michael Berkowski 2012-04-05 19:22


0

If you want to read an image and then render it as an image

$image="path-to-your-image"; //this can also be a url
$filename = basename($image);
$file_extension = strtolower(substr(strrchr($filename,"."),1));
switch( $file_extension ) {
    case "gif": $ctype="image/gif"; break;
    case "png": $ctype="image/png"; break;
    case "jpeg":
    case "jpg": $ctype="image/jpeg"; break;
    default:
}

header('Content-type: ' . $ctype);
$image = file_get_contents($image);
echo $image;

If your path is a url, and it is using https:// protocol then you might want to change the protocol to http

Working fiddle

2017-02-15 06:15
by Raj Sharma
Determining the file's MIME type by file's extension is not a good practice, i think. Wouldn't it be better if it checked the file's MIME type using mimecontenttype - SebasSBM 2018-05-24 10:04
Ads