problem with image in php
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: imagejp开发者_如何学JAVAeg() 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‚ƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚâãäåæçèéêòóôõö÷øùúÿÚ?ùþŠ( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š
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.
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);
(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:
- Remove header.
- 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
精彩评论