PHP Imagepng output does not shown in the browser - php

I spent my last night to solve this problem.
I have a php file which is supposed to return a PNG image with relevant headers. The relevant file content is basically. (No previous output or whitespace before the header statement)
header('Content-Type: image/png');
$img = #imagecreatefrompng($path);
imagepng($img);
imagedestroy($img);
But the browser (Firefox) says sth like there is an error with the image, therefore it cannot be displayed.
If I save the file to another place and download it with FTP, it is shown. Like:
imagepng($img, "/tmp/hedede.png");
If I remove the header statement and printout the file content it shows PNG header with all the other garbage data. If I save this page on browser as a PNG file, browser again does not show the saved PNG file, but Irfanview shows it.

Please check your file (in the path) MIME type, don't trust the file type by its extension. Hope it helps. If your file is not a png image, the code does not run properly and doesn't display the given image.

This is GD Library Version Problems.
imagepng($resource)
...work old version GD Libray. New Version Would be:
imagepng($resource,"php://output")

Related

send content type image/TIFF to the browser is not working

I'm using Imagick (6.7.7-4 and module version 3.2.0RC).
I am trying to create an image and render it in the browser dynamically, for example, by HTML
<img src='image_generator.php?QUERY_PARAMS_TO_DETERMINE_TEXT'>
This is the image_generator.php content:
/* Create text */
$draw->setFillColor('white');
$draw->setFontSize( 19 );
$draw->setGravity(Imagick::GRAVITY_CENTER);
$draw->annotation(0, -50, 'points');
$image->drawImage($draw);
/* Give image a format */
$image->setImageFormat('tiff'); // png works
/* Output the image with headers */
header('Content-type: image/tiff'); // png works
echo $image;
When I open image_generator.php in php and the image format is set to TIFF, it automatically downloads a PHP file and does not show an image as of the PNG case.
If changing the extension of the downloaded file to ".tif" results in the correct image, then, the problem is, the browser is using the page name as the file name. That's not uncommon when a server side script or applet is programmatically serving an image (or other file for download).
Change the "attachment" name using the following header.
/* Give image a format */
$image->setImageFormat('tiff'); // png works
/* Output the image with headers */
// This line will change the download name
header('Content-Disposition: attachment; filename="[NAME.TIFF]");
// This line will set the Mime Type
header('Content-type: image/tiff');
echo $image;
That's it.
I know I am years late to this party, but, because various people run into this and think it's bad code or configuration, or simply need to know how to rename their downloads, I figured I would answer it for others who run into it.

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.

'Save As' image with src from PHP

I'm currently displaying images and hiding the src code by having a php file output the image. But when I right click on the image displayed and go down to 'Save As' it prompts me to download the php file not the actual image (obviously because it points to that src).
What can I do to download the actual image instead of displayImage.php?
It doesn't prompt you to download the PHP file, it simply uses that as the file name, because that is the file name from which it got the image data. If you manually input a valid image file name and try to open what you saved, it should still be a valid image.
You may also be able to give it a sensible name by including the file name in a Content-Disposition: header from your PHP file, e.g.
$filename = 'image.jpg';
header('Content-Disposition: inline; filename="'.$filename.'"');
// Don't forget the Content-Type as well...
// Output image here
...however this relies on the browser handling this sensibly, which not all of them do :-(
You can send a filename in the header.
header("Content-Type: image/png");
header('Content-Disposition: inline; filename="some.png"');
Send the correct content type in the image generator script:
header('Content-type: image/jpg');
If you want to have the .jpg extension when a PHP script is outputting an image, you'll need to do a htaccess or httpd.conf rewrite, where you can rewrite a .jpg request, to your php image generator script.
See mod_rewrite http://httpd.apache.org/docs/current/mod/mod_rewrite.html

working with image from imagecreatefromstring PHP

So i have an iphone app that that uploads an image to my webserver and i looked around and people seem to be doing something like
$data = file_get_contents($_FILES['file']['tmp_name']);
$image = imagecreatefromstring($data);
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
I looked at the php docs, but i still don't understand what the header() does; does it convert the image into whatever format i want?
And for the imagepng(), where is the image outputted to? memory? is that why i need the imagedestroy()?
and where would i put in
move_uploaded_file()
Thanks in advance!
This code is intended to return as output an image - you could use it as a valid src for an image tag. That is, you could do this:
<img src="thatfile.php?something=1" />
The headers tell the browser that the data the server is going to send is an image (a PNG image, specifically).
In your example code, the file never actually gets written anywhere: the data stays in memory until the script ends, then it is simply "forgotten". imagedestroy frees up the memory and is good practice, but it really isn't necessary since the memory will be garbage collected after the request ends. If you want to preserve the image in a file, you'd have to use one of the related functions such as imagepng: http://www.php.net/manual/en/function.imagepng.php. The only difference between writing the file or not in your example code is the lack of a second argument for imagepng - second argument would be the desired file path.
It would help to read through the docs on this entire subject to gain a firm grasp of how these functions work and what each does. There are plenty of demos on the doc pages that show this in action.
This particular example gets the image uploaded through POST from the $_FILES array and simply outputs it back to the browser. The header is there to inform the browser that the content following is a PNG image.
Since you create an image from a string, it doesn't have "an extension". It's just an image resource at this point. You can create an actual file from it using imagepng, imagejpeg or any of the other methods to save an image resource to a file. You decide the extension (and file name) at that stage yourself.
E.g.:
imagepng($image, 'path/to/file.png');
and where would i put in move_uploaded_file()?
You wouldn't, since you don't have an uploaded file, only a string.
Header is purely for the server to let the browser know "Oh hey this is a png image please render it so"
imagepng encodes it into the png format and "prints" to the output
imagedestroy frees the memory taken by the image resource.
If you need to force extension you can use mod_rewrite
Here's a sample couple lines from my .htaccess:
RewriteEngine on
RewriteRule images/000000/00FF00/newmyinfo.jpg images/newmyinfo.php?bgcolor=000000&color=00ff00 [L]
Hope this helps!

GD: php imagepng creates whitespace in image

I have some problem with GD when i creates images with php.
The strange thing is that it works on one server with php version 5.3.1 but not on php version 5.2.14. (I'm not sure if it's the php version or the GD lib that is doing this.)
This file is created with convert and saved in a directory in captcha::get_file().
And this file is generated with imagecreatefrompng() and imagepng()
I made some small changes to the script and made a gif. But there is still a problem with the png
What causes this, and how can I fix it?
Here is the phpcode:
<?php
session_start();
require_once("./captcha.php"));
// creates the image with convert and returns the location of the picture
// document_root/picture/picture.png
$picloc = captcha::get_file();
$image = #imagecreatefrompng($picloc);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
unlink($picloc);
?>
not positive, but the problem may be in your content length header.
header('Content-Length: ' . strlen($image));
at this point in your code, $image is a resource data type, not a string. try simply removing the content-length header line, and see what happens.

Categories