Reputation: 14751
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‚ƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚâãäåæçèéêòóôõö÷øùúÿÚ?ùþŠ( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š
Upvotes: 0
Views: 7062
Reputation: 961
(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:
:
$path = 'C:\xampp\htdocs\me\1.jpg'; <-- single quoted
echo $path;
$image1 = imagecreatefromjpeg($path);
//header('Content-Type: image/jpeg'); <-- commented
imagejpeg($image1);
Arsen
Upvotes: 1
Reputation: 6127
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);
Upvotes: 1
Reputation: 449475
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.
Upvotes: 2