PHP render html code to image - php

render.php
$form = '<b>Hi</b><br /><img src="test.jpg" /><br />New File';
echo $form;
header("Content-Type: image/png");
imagejpeg($form);
But this code not work
I want this page render to img file :
<img src="render.php" />

you are trying to output both HTML and image-data - that doesnt make any sense.
Instead you should put an HTML-img-tag into your website, the src-attribute being render.php which sets the header via header() and directly outputs image data - without any additional HTML.
https://stackoverflow.com/a/1851856/351861

You should use extra php file for images, something like:
<img src="render.php?id=15" />
Because if your php script returns only one picture it is used wrong. so you pass it what image to show.
And render.php might be something like
$pic_id = (int)$_GET['id'];// cast to int because only numbers are allowed here
$pic_path = 'assets/img/';
$f = $pic_path.$pic_id.".jpg";// it's just an example so it's simplyfied
header("Content-Type: image/png");
imagejpeg($form);
The point here is that you can check user's permissions to see the picture very flexible and log some data about downloader.

Related

Can't get php to output an image

I want my PHP to output a simple image but when I do it gives me a white square, so I thought the IMG couldn't be found but when I put a simple HTML IMG tag it can be found the code is very simple. also if I try an image from the internet it also won't work.
<!--testing if image can be found-->
<!--<img src="img.jpeg">-->
<?php
header("Content-type: image/jpeg");
readfile("img.jpeg");
exit(0);
?>
Basically what's happening here is that you output some HTML comments before you output the image which means your image file is invalid and so your internet browser doesn't know what to display and defaults to what you're seeing (a white box in the middle of your screen).
Additionally when you use header you should ensure that you haven't already returned data!
If you check your source in your browser you'll see something like...
<!--testing if image can be found-->
<!--<img src="img.jpeg">-->
ÿØÿàJFIFÿá‹rExifII*Àж..................................
...with the additioonal data continuing for some time!
If you remove the comments from your PHP file then it will work as intended; your (entire) file should look something like:
<?php
header("Content-type: image/jpeg");
readfile("img.jpeg");
Note: You may want to add additional headers RE content length etc. but these aren't strictly necessary for most browsers.
To display an image using content type header, you must put only php on the page. If you add any other content it'll display a blank square
ex.
<?php header("Content-type: image/png"); readfile("./resources/img/favicon.png"); ?>

How do I extract an image from a MEDIUMTEXT, MyISAM field? [duplicate]

I have a PHP function that does on-the-fly image resizing for thumbnail creation.
I am having trouble as it's just displaying raw image stream instead of the actual image.
My code is using a function called thumbnail:
$thumbnail = thumbnail($item['filename'], 209, 137);
imagejpeg($thumbnail);
I've tried putting in:
header("Content-type: image/jpeg");
However, this just expects the full page to be an image. I have absolutely no idea where to go from here, been working at it for a while. I'd rather not save the image to disk although it's looking like this might be necessary.
You either
Do it the normal way
This mean you point at one url, and serve the contents of one image:
<img src="myimage.php">
and myimage.php is a script that looks like:
header('Content-type: image/jpeg');
imagejpeg($thumbnail);
die;
This technique has the advantage of being.. the normal way of doing things.
OR
Output the image inline
Using data uris outputting the contents as a base64 encoded string
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">
This technique is most appropriate with small images.
It has the advantage of meaning all the images are in the main http request - at the (possibly sizable) disadvantage of making the page harder to edit/build and possibly negating the benefits of browser caching (unless the html page is itself cached).
Being normal is easier
Regarding this statement in the question:
However, this just expects the full page to be an image
That's right - if you do it the normal way you want to point at your php script with the src attribute of an image tag, and server only an image - i.e. the exact same response as if you were pointing at an image file with a browser.
Unless you have a good reason to do things a different way - the "normal" way is a good place to start.
You can point an html img tag to an php file.
<img src='thumbnail.php?file=<?php echo $item['filename']; ?>' />
Then on your php file you display the image and change the headers since all it is doing is displaying an image.
$thumbnail = thumbnail($_GET['filename'], 209, 137);
imagejpeg($thumbnail);
header("Content-type: image/jpeg");
You need to insert the image like you would a normal image in HTML and create the image in a separate PHP file:
image.php
<?php
$img = imagecreate(100,100); //Create an image 100px x 100px
imagecolorallocate($img, 255,0,0); //Fill the image red
header('Content-type: image/jpeg'); //Set the content type to image/jpg
imagejpeg($img); //Output the iamge
imagedestroy($img); //Destroy the image
?>
myWebpage.html
<img src="image.php" />

Displaying image from a folder

image displays in the browser whrn the following code is used.But when the code is used inside html,the image does not appear instead a small box appear.
<?php
header('Content-Type: image/jpeg');
readfile('http://localhost/picture013.jpg');
?>
for example when i use no image is displayed.
<html>
<body>
<?php
header('Content-Type: image/jpeg');
readfile('http://localhost/picture013.jpg');
?>
</body>
</html>
in short i wanted to display all the images from a folder.
Maybe you can try this one
<?php
$dir = "localhost/image"; // your folder name ex: image
$imgs = glob($dir ."/*.jpg"); // get your image files with .jpg
foreach ($imgs AS $i) {
echo "<img src='$i'>"; //
}
?>
Think about what this is doing for a moment. That file already exists, you're not inventing image data from nothing, so why are you not just creating an element and link to that image?
Put an <img src="some/location/picture013.jsp"> on the page with an echo/print instead, and whatever page needs to show that image will work just fine. In this case it's already a legal URL, so you can just add <img src="http://localhost/picture013.jpg"> to whatever page needs it.
If it wasn't, you could make the server simply resolve the location you're pointing to with an .htaccess rule, or simply have the file live in an already web-visible directory. There is no reason to make PHP proxy the image data when it's already a normal web-visible resource; in fact, you're just over-complicating your code with unnecessary steps.

Using header('Content-type: image/png') and echo"<html>" in one .php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to use imagecreatefromjpeg, imagecreatetruecolor, imagecopyresized and imagejpeg while making use of the echo "<html><body>"; etc...
For some reason I get "The image could not be displayed because there were errors on the page" until I comment out header('Content-type: image/png'); and then I just get a picture of a broken picture, like a torn page.
All I've seen is that I can't have header('Content-type: image/png'); and html in the same .php file. If that's the case can anybody tell me how to resize an image for a thumbnail gallery while still having html in a .php file?
Thanks in advance.
You are mixing up two different things.
A webpage with an image on it, does not contain that image in general. Instead, it often refers to an external source. I said in general, because, yes, an image can be embedded in an HTML page, see below.
You have two options:
You can create an apart PHP file where you create the image and output its bytes. In your HTML code, you refer to that image:
page.html:
<html>
<body>
<img src="myimage.php" alt="" />
</body>
</html>
myimage.php:
<?php
header("Content-Type: image/png");
createimageandso_on();
// Do the drawing.
?>
Or you can embed the image in your HTML file, using base64 encoding:
<?php
$contents = all_bytes_from_created_image();
// Get the bytes from the created image.
$base64 = base64_encode($contents);
?>
<html>
<body>
<img src="data:image/png;base64,<?php echo $base64; ?>" alt="" />
</body>
</html>
The second option is suitable for smaller images, since the base64 encoded string will produce large portions of text.
Edit
If I understand it correctly, you want to read images from a directory and resize them to the same size, using them as thumbnails?
What you might just want to do is create a PHP file where you read a source image and give them the same size.
Just like 'normal' PHP files, PHP can do something with the request parameters you give. Perhaps you've ever seen this:
http://example.com/somepage.php?key=value&anotherkey=anothervalue
That string behind the question mark (key=value&anotherkey=anothervalue) is the query string. PHP can do something with the values:
<?php
echo $_GET['key']; // returns "value"
echo $_GET['anotherkey']; // returns "anothervalue"
?>
Now we can just do the same when creating an image. You don't have to make twenty PHP files with almost the same code, but just a single file which reads a file (you name it) and resizes it to the specified width (you name it) and height (you name it).
thumbnail.php
<?php
// Get some request parameters we're going to use.
// We're expecting the parameters below to exist.
$file = $_GET['filepath'];
$width = $_GET['width'];
$height = $_GET['height'];
// Now we're gonna create the image from the given file.
$src = imagecreatefromjpeg($file);
imagecreatetruecolor($width, $height);
// And the rest of the file reading and image creation.
header("Content-Type: image/jpeg");
imagejpeg($image);
?>
webpage.html
<html>
<body>
<?php
$width = 100;
$height = 100;
$files = read_some_directory_and_return_a_list_of_filenames();
foreach ($files as $file) {
// Echo an image tag in the HTML document;
// use as image our thumbnail.php file and give it a query string:
echo "<img src=\"thumbnail.php?width=".$width."&height=".$height."&filepath=".$file."\" alt=\"\" />";
}
?>
</body>
</html>
What you've seen is correct - you can't have a file with Content-type: image/png that contains html contents. The browser will interpret the html code as encoded html data, which is wrong.
What you should do is leave the content type as text/html and send the image in an html document, as in my example below. I left out the <head> for simplicity, but you should add one.
<!DOCTYPE html>
<html>
<body>
<img src="myimage.png" width="100" height="100" />
</body>
</html>
You cannot have markup inside your image you want to push down to the browser. You're telling the browser to expect an image and then sending down some markup, that's a no-no.
What we can do is include a .php file which uses your imagecreatetruecolor, etc... functions inside some img tags:
<html>
<head><title>Image Test</title></head>
<body>
<img src="image.php?file=A" />
<img src="image.php?file=B" />
</body>
</html>
Then image.php would use our image/jpeg headers and contain your imagecreatefromjpeg code.
<?
header('Content-Type: image/jpeg');
if ($_GET['file'] == 'A') {
$img = imagecreatefromjpeg('image1.jpg');
} elseif ($_GET['file'] == 'B') {
$img = imagecreatefromjpeg('image2.jpg');
}
// do your modification etc... here
imagejpeg($img);
imagedestroy($img);
You can only output "image code" in your image.php file because the browser is going to treat it as if it's a regular jpeg.

Change PHP GD image output file url name/link

I have this problem, i have this script in php that creates a image on the fly, the problem is that the outputted image on the browser is allright, but i need to change it's name.
Ex: Index.php
<?php $url = "http://www.somesite.com/cls/image_scrc?_aaa=qJ7VgSxWBLG3FfNQVB%2BKI58kfHQulPHZLuLYaZAG6Tk%3D&_ab=3ctGTf547wdsAGjY%2F5FASE%2BpBnbQEAcrhbJzCHQ7mGs%3D&_acb=89e62acf3b4d254abf0c3ab30d6ebb33" ?>
<img src="<?php echo $url ?>" />
The image_scrc.php is the file that creates the image, and as you can see i have several data that is passed by the get method.
In the image_scrc.php i have tryed
header('Content-type: image/jpg');
header('Content-Disposition:inline; filename="'.$random_name_jpeg.'"');
but the html link is is always appearing like this
http://www.somesite.com/cls/image_scrc?_aaa=qJ7VgSxWBLG3FfNQVB%2BKI58kfHQulPHZLuLYaZAG6Tk%3D&_ab=3ctGTf547wdsAGjY%2F5FASE%2BpBnbQEAcrhbJzCHQ7mGs%3D&_acb=89e62acf3b4d254abf0c3ab30d6ebb33
if i select the image on browser and then select copy image link it copies just like this also.
however, when I save the image it assumes the random_name.jpg, but only on save!
i've tried everything, even htaccess rules but nothing seems to work !!
it's this possible to acomplish? transform this
http://www.somesite.com/cls/image_scrc?_aaa=qJ7VgSxWBLG3FfNQVB%2BKI58kfHQulPHZLuLYaZAG6Tk%3D&_ab=3ctGTf547wdsAGjY%2F5FASE%2BpBnbQEAcrhbJzCHQ7mGs%3D&_acb=89e62acf3b4d254abf0c3ab30d6ebb33
to this
http://www.somesite.com/cls/random_name.jpg
i cant have the image on the server side! and must be displayed on the fly
Thanks in advance.

Categories