PHP cannot load image and display it - php

I am trying to make a php script to load image and display it depending on $_GET paramters.
I want to use this later to load image into my mobile application(Android).
The problem is I cannot make image to display. I use the code below:
header("Content-Type: image/jpeg");
readfile($path);
But when I open page in firefox I get error:
Image cannot be displayed because it contains errors.
What is more I tried different approach and I can get image display in html code(I dont want this - it requires to remove headers in java android - but it proves that path is correct)
echo '<img src="data:image/jpeg;base64,'.base64_encode(file_get_contents($path) ).'"/>
What am I doing wrong? Any ideas?
Thanks in advance.

Related

Why do i get broken image icon even if i set content-type="image/jpeg"?

That question might look silly but i would appreciate if i get a good answer.
I know what http header is and we can change it using header function in php.
Suppose i have a php file an_image.php and the code of it is as below :
<?php header('Content-type:image/jpeg'); ?>
<img src="image/flower.jpg">
Why am i getting a broken icon? By changing the header content type am i not changing the output as image?
As i think img tag is still an html output so as i'm trying to set an html content into an image content so i get the broken icon.
So what is the use of content-type:image/jpeg and where can it be used?
For example flower.jpg picture is in my image folder. If i create flower.php
and open the flower.jpg using a text editor and copy the code of it and paste it on flower.php and set the header content-type:image/jpeg and try to open it on browser it doesn't work saying syntax error.
Looking for a good explanation .
The correct content type for what you're outputting is text/html. You'd use the image/jpeg content type only if you were outputting the actual image file's contents itself.
<?php
header('Content-Type: image/jpeg');
readfile('image/flower.jpg');
Common uses include having a PHP script output a protected file after verifying a user's permissions allow it to be accessed, tracking pixels (save some data then serve a 1x1 image, for example), and serving dynamically generated images.

How to treat a PHP image generation script as an image

This is an odd question but I'm stuck on how I would achieve this and I am unable to find any methods of doing so.
I have a simple php script that takes variables (containing file names) from the URL, cleans then and then uses them to generate a single image from the inputted values. This works fine and outputs a new png to the webpage using:
imagepng($img);
I also have a facebook sharing script in PHP that takes a filepath as an input and then shares the image on the users feed where this statement is used to define the image variable:
$photo = './mypic.png'; // Path to the photo on the local filesystem
I don't know how I can link these two together though. I would like to use my generation script as the image to share.
Can anyone point me in the right direction of how to do this? I am not the master of PHP so go easy please.
-Tim
UPDATE
If it helps, here are the links to the two pages on my website containing the outputs. They are very ruff mind you:
The php script generating the image:
http://the8bitman.herobo.com/Download/download.php?face=a.png&color=b.png&hat=c.png
The html page with the img tag:
http://the8bitman.herobo.com/Share.html
Treat it as a simple image:
<img src="http://yourserve/yourscript.php?onlyImage=1" />
yourscript.php
if($_GET['onlyimage']) {
header('Content-type:image/png'); //or your image content type
//print only image
} else {
//print image and text too
}

How do I directly display an image using PHP?

I have a question about displaying images in PHP. I need to have a PHP file display as JUST an image, as opposed to an image embedded in a web page, as if you had browsed to the JPEG image directly. The reason that I need it to be a PHP page as opposed to actually browsing to the image is that I need to resize the image before it is delivered. It would be easiest to use an image directly because that way I can display the image in a desktop application more easily. Is there any way to do this? Thanks!
<?php
header('Content-Type: image/jpeg');
readfile('path/to/image.jpeg');
You need to set the header Content-Type property to be the correct image type. See this link for the some examples.
http://www.electrictoolbox.com/image-headers-php/
Choose the correct header :
header("Content-type: image/png");
And use php gd to change the image size : http://php.net/manual/fr/book.image.php

PHP display as image

I am trying to make a PHP page load an image and display, as if it were an image file. Here's what I tried to do:
header("Content-type: image/png");
echo base64_encode(file_get_contents($ad->location));
But this doesn't seems to work. How should this be done?
Thank you.
You don't need to base64 encode image data when outputting it as a file.
Just pass it through.

How do I load an image in PHP

I want code that loads an image to a PHP server and then send it to browser.
For example I want sample.php to send an image to browser once it is requested.
in other words, I want to create a PHP file that acts like a proxy for an image.
why are you doing this?
why don't deliver the image directly?
if you are trying to display a random image you may as well just redirect to the image using
header("Location: address-of-image");
for delivering the file to your clients from your server and not from its original location you can just do. however your php.ini settings need to allow external file opens
readfile("http://www.example.com/image.jpg")
correct headers are not required if you are going to display the image in an img tag,
altough i would recommend it. you should check the filetype of the image or in most cases just set an octet-stream header so the browser doesnt assume an incorrect type like text or something and tries to display binary data.
to do so just do
header("Content-type: application/octet-stream")
one more thing to consider may be setting correct headers for caching...
You need to use
$image = fopen("image.png");
Modify the headers(not sure exacly if it's correct)
headers("Content-type: image/png");
And then send the image
echo fread($image, file_size("image.png"));

Categories