0

I want to show an image and wrote below codes

$path = "C:\xampp\htdocs\me\1.jpg";
$image1 = imagecreatefromjpeg($path);
header('Content-Type: image/jpeg');
imagejpeg($image1);

But when I run it in Firefox it shows:

The image “http://127.0.0.1/me/Untitled%201.php” cannot be displayed because it contains errors.

What is problem?

Edit:

I deleted header function but it has this error:

Warning: imagecreatefromjpeg(C: mpp\htdocs\me.jpg) [function.imagecreatefromjpeg]: failed to open stream: Invalid argument in C:\xampp\htdocs\me\Untitled 1.php on line 136

Warning: imagejpeg() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\me\Untitled 1.php on line 138

after all works it shows some chars like this

    $.' ",#(7),01444'9=82<.342ÿÛC  2!!22222222222222222222222222222222222222222222222222ÿÀ¸)"ÿÄ ÿĵ}!1AQa"q2‘¡#B±ÁRÑð$3br‚ %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖרÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ ÿĵw!1AQaq"2B‘¡±Á #3RðbrÑ $4á%ñ&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz‚ƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖרÙÚâãäåæçèéêòóôõö÷øùúÿÚ?ùþŠ( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š
1
  • the problem was solved by removing <html> tags Commented May 7, 2011 at 12:51

3 Answers 3

2

In this specific case, you forgot to escape the backslashes \ in the file path. Either use escaped backslashes \\ or - much better - forward slashes: /

   $path = "C:/xampp/htdocs/me/1.jpg";

To debug stuff like this, remove the header() line to see the image's source code to see the PHP error messages that are breaking it.

Sign up to request clarification or add additional context in comments.

4 Comments

Was going to say that. On windows servers I usually use c:\\folder\\folder2\\file.ext single slashes don't work unless you use forward slashes or double backslashes. It's also clearly visible in your error, it states C: xampp instead of C:\xampp
@mehdi did you put the header back in after fixing the problem? Removing the header is meant for debugging purposes only. To see the image, you need the header.
The image “127.0.0.1/me/Untitled%201.php” cannot be displayed because it contains errors.
Strange. So there is no human-readable error message in the peculiar characters? If 1.jpg indeed is a valid JPG file, I don't know what this is then.
1

You image contains errors, what errors? Try temponary removing header() functions.

$path = "C:\xampp\htdocs\me\1.jpg";
$image1 = imagecreatefromjpeg($path);
#header('Content-Type: image/jpeg');
imagejpeg($image1);

Comments

1

(C: mpp\htdocs\me.jpg)

As yor see this differs from

(C:\mpp\htdocs\me.jpg)

So your need change your code:

$path = "C:/xampp/htdocs/me/1.jpg";
$image1 = imagecreatefromjpeg($path);
header('Content-Type: image/jpeg');
imagejpeg($image1);

Or like this:

$path = 'C:\xampp\htdocs\me\1.jpg';
$image1 = imagecreatefromjpeg($path);
header('Content-Type: image/jpeg');
imagejpeg($image1);

Also to debug:

  1. Remove header.
  2. Make echo ...

:

$path = 'C:\xampp\htdocs\me\1.jpg'; <-- single quoted
echo $path;
$image1 = imagecreatefromjpeg($path);
//header('Content-Type: image/jpeg'); <-- commented
imagejpeg($image1);

Arsen

1 Comment

@mehdi that's the expected output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.