How do I directly display an image using PHP? - 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

Related

PHP cannot load image and display it

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.

Change image size to Save on PHP server

I have a form in my Android app that send information to php server with an image pick button. I want to resize image before saving on server with php codes :
<?php
move_uploaded_file($_FILES['file']['tmp_name'],'uploads/'.$_FILES['file']
['name']);
$orgfile='uploads/'.$_FILES['file']['name'];
list($width,$height)=getimagesize($orgfile);
$newfile=imagecreatefromjpeg($orgfile);
$thumb='uploads/a/'.$_FILES['file']['name'];
$truecolor=imagecreatetruecolor(600,400);
imagecopyresampled($truecolor,$newfile,0,0,0,0,600,400,$width,$height);
imagejpeg($truecolor,$thumb,100);
unlink($orgfile);
?>
This code just resize jpeg images and another formats (png or gif and even jpg) saved a black image.
It is necessary to mention that name of image file changed to a random number like "32165465423" and I don't know the image format to use "imagecreatefrompng" or "imagecreatefromgif" in my php file.
I want a code like "imagecreatefromall" or another ...
Thanks guys(sorry for bad English)
You will have to detect the type of image, based on that you can run the function. See the one cool php library for reference
https://github.com/eventviva/php-image-resize/blob/master/lib/ImageResize.php#L77

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.

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.

Showing php page as an image

is that possible to show whole php page as an image file like png or jpg ?
for example,
<img src="details.php?id=44">
just like a screenshot of the page.
Thx
<?
// details.php file
// {
// do stuff to create your image and store in $my_image
// }
header("Content-type: image/jpg");
echo $my_image;
?>
1) output the mime type in the header.
2) echo your binary data
easy!
Not in the way you're thinking. You can use the PHP ImageMagick library or the PHP GD Library to generate an image dynamically and output it, but in order to get screenshots of a page you'll really need an outside service.
it depends on what you want to show. if its html/js/css, it varies across browsers.
If its unicode text to be displayed as images, you can use GD library for the purpose
I think you are talking about taking screenshots of a url or webpage. You can you freely available like:
http://www.shrinktheweb.com/
http://picoshot.com/
There are many such freely available services. If you want to host such service on your own server. You will need to create a software which can open your browser and take screenshot of the url. Ther are some softwares available as well. I hope it helps.

Categories