Mike Warren
Mike Warren

Reputation: 3866

Displaying an image resource with PHP

I am trying to simply get a test image, crop it a bit, and display it. The thing is, I am doing this from PHP, and every attempt I have made thus far has frustrated me even more; /* and the internet is telling me to do things that I have already done. */

OK, so what have you done??

I have tried various ways of getting the $image itself. I have tried the following:

  1. Using imagecreatefromstring($url); /* where $url is the location of the image (I am pulling from random sites, which is a necessity for my project */
  2. Using imagecreatefromstring(file_get_contents($url)); and then, in a <div> tag, echoing the image
  3. Using imagecreatefromstring(file_get_contents($url)); and then, in an <img> tag, echoing the image
  4. Doing 3., except using imagejpeg($image)
  5. Doing 4., except, this time, putting header('Content-Type: image/jpeg');

OK, what happened?

First attempt returned a nice error saying that $image was not recognized. Second attempt seems to work, but instead of an image, I get back the following text: Resource id #5 Third attempt gives me a bunch of gibberish. Fourth attempt also gave me a bunch of gibberish. Fifth attempt gave me a black screen with this error message: The image “http://localhost/ResearchProject/sampleImageDisplay.php” cannot be displayed because it contains errors.

Here is the final code (and header.php just contains all of the necessary HTML tags to display the web page (the DOCTYPE tag, the html tag, the meta tag...) ):

<?php
    include "header.php";

    $url = "http://byebyedoctor.com/wp-content/uploads/2010/12/alopecia-areata-3.jpg";
    $image = imagecreatefromstring(file_get_contents($url));
?>
        <img>
            <?php header('Content-Type: image/jpeg');  imagejpeg($image); ?>
        </img>
        <?php imagedestroy($image); ?>
    </body>
</html>

Why can't I display this simple image??

Upvotes: 4

Views: 10753

Answers (3)

Radon8472
Radon8472

Reputation: 4931

Your major planning problem is: You try to include you image into a html code. For this you need 2 phps:

  1. A php file what contains your html-code with an img-tag (src must be the second php-file)
  2. A php file what reads you base image and outputs the BINARY image data

your_htmlpage.php

<html>
   <?php
        include "header.php";
   ?>
   <body>
        <img src="your_image.php">
   </body>
</html>

your_image.php

<?php
  $url = "http://byebyedoctor.com/wp-content/uploads/2010/12/alopecia-areata-3.jpg";
  $image = imagecreatefromstring(file_get_contents($url));
  if($image !== false) {
    header('Content-Type: image/jpeg'); 
    imagejpeg($image);
    imagedestroy($image);
  } else {
    header("X-Error", true, 404);

    echo "Error loading remote image";
  }

  ?>

Another option would be to send the image as base64encoded String to the browser. But this means that the WHOLE image-data is part of the html-code. e.g. lik this:

<html>
   <?php
        include "header.php";


        $url = "http://byebyedoctor.com/wp-content/uploads/2010/12/alopecia-areata-3.jpg";
        $image = imagecreatefromstring(file_get_contents($url));
        if($image !== false) {
          // start buffering and catch image output
          ob_start();
          imagejpg($image);
          $contents =  ob_get_contents();
          ob_end_clean();
          imagedestroy($image);

          $img_data = "data:image/jpg;base64,".base64_encode($contents);
        } else {
          $img_data = "";
        }
   ?>
   <body>
        <img src="<?php echo $img_data; ?>">
   </body>
</html>

Upvotes: 0

Dejan Bogatinovski
Dejan Bogatinovski

Reputation: 630

This code worked for me. Just create empty php file and paste this code. It should return the photo of a semi-bold man.

$url = "http://byebyedoctor.com/wp-content/uploads/2010/12/alopecia-areata-3.jpg";
$im = imagecreatefromstring(file_get_contents($url));

if ($im !== false) {
    header('Content-Type: image/png');
    imagepng($im);
    imagedestroy($im);
}
else {
    echo 'An error occurred.';
}

Upvotes: 4

Kylie
Kylie

Reputation: 11749

First off...

You have to make sure that "allow_url_fopen" is enabled in php.ini.

If you have access to this file just change

;allow_url_fopen

To

allow_url_fopen

Some hosts disable access to php.ini though.

Then all you have to do is this....

$url = "http://byebyedoctor.com/wp-content/uploads/2010/12/alopecia-areata-3.jpg";
$image = file_get_contents($url);

Then you can store it temporarily to resize it or whatever...

//Store in the filesystem.
$fp = fopen("/location/to/save/tempimage.jpg", "w");
fwrite($fp, $image);
fclose($fp);

Upvotes: 0

Related Questions