GD: php imagepng creates whitespace in image - php

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.

Related

Imagick PHP only displaying last page

I am using imagick with PHP/Windows IIS. I have a simple script that converts a TIF file to PDF an presents it to the browser. It works flawlessly with single page TIF files, but with multiple pages, its only showing the last page.
I understand that it shows the last page by default because the $im variable is an array. Any attempt I make to fix it makes it an invalid PDF. Below is my code. I am new to imagick and any help is appreciated!
$im = new imagick("tmp/tmp.tif");
$im->setImageFormat('pdf');
header('Content-Type: application/pdf');
echo $im;
ImageMagick version ImageMagick 7.0.7-11 Q16 x64 2017-11-23
ImageMagick library version ImageMagick 7.0.7-11 Q16 x64 2017-11-23
(this is very rough testing code, I will clean it up later)
The internal image iterator is pointing at the last page read. You just need to reset it to the first page with Imagick::setFirstIterator.
$im = new imagick("tmp/tmp.tif");
$im->setFirstIterator();
$im->setImageFormat('pdf');
header('Content-Type: application/pdf');
echo $im->getImage();
Or even
$im->setIteratorIndex(0);
Edit based on comments
If you are attempt to output the entire PDF document, you would use Imagick::getImagesBlob.
$im = new imagick("tmp/tmp.tif");
$im->setFirstIterator();
$im->setImageFormat('pdf');
$blob = $im->getImagesBlob();
header('Content-Type: application/pdf');
header('Content-Length: ' . strlen($blob));
echo $blob;

PHP image download with header

I've successfully implemented some PHP to download an image:
PHP:
<?php
$image = "path/to/image.jpg"
header('Content-disposition: attachment; filename=image.jpg');
header('Content-type: image/jpeg');
readfile($image);
?>
Here is an example image that I've saved to my server: http://www.thenally.com/work/php-tests/header/files/herzog.jpg
If I view the image in the browser, then right-click to save, it acts as a typical image on my local Windows box (ie. viewable in Windows Photo Viewer). When I download the image using the PHP script above, the image can't be viewed in the same way, although the filesize is accurate and I can open it in Photoshop. I don't know much about image encoding/decoding etc but obviously the file changes when I download using the method above.
EDIT:
The Windows Photo Viewer error is as follows:
"Windows Photo Viewer can't open this picture because either Photo Viewer doesn't support this file format, or you don't have the latest updates to Photo Viewer".
There are no PHP errors in my log file, but this came up in my Chrome console:
"Resource interpreted as Document but transferred with MIME type image/jpeg: "http://thenally.com/work/php-tests/header/index.php".
try this
header('Content-Transfer-Encoding: binary');
I ran a similar issue and managed to fix it by removing any blank space/new lines between PHP tags.
Basically had to do this:
<?php
//stuff
?>
--REMOVE THIS SPACE--
<?php
//stuff
?>
Just make sure you do not have any blank spaces before any PHP tags and you should be good!

Can't create/display php images - GD Image corrupt or truncated

I'm having a problem and i'd like some help please.
I have made a script for creating an image with PHP using GD library, but the problem is when i preview it on the Firefox it shows me an error in Firebug: "Image corrupt or truncated:localhost/my_scripts/images.php". NOTE: this is not an error displayed from PHP, it seems more like an error comming from the browser.
I have checked my code for syntaxes many times, i have also tried out and other gd functions, like imagegif, imagettftext etc etc but didn't have any effect. Here's my code:
//$image = #imagecreate(200, 20)or die("Cannot Initialize new GD image stream");
$image = imagecreate(200, 20);
$background = imagecolorallocate($image,0,0,0);
$foreground = imagecolorallocate($image,255,255,255);
imagestring($image,5,5,1,"This is a Test",$foreground);
header("Content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
GD library is enabled i've already checked that in phpinfo(), and I have also tried to preview it and on Chrome and looks the same. Any ideas what i might doing wrong or why happens this? and how can i fix this error??
I found something about this error and finally i figured out what was going on, so here is the solution to the problem:
Make sure that PHP code is the only thing on the page. No HTML, no spaces before or after the PHP code, etc, the only thing it should send to the browser should be the image data. If that still doesn't work then make sure that saving the PHP script in a format that does not include the UTF BOM and this should fix it.
I changed my code to UTF-8 without BOM and worked to me! Thanks Lykos.

PHP Imagepng output does not shown in the browser

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")

PHP Force File Download Code - Error

i am using the following code
<?php
$rFile = $_GET['sfile'];
$rExt = $_GET['ext'];
header("Content-disposition: attachment; filename=".urlencode( $_GET['sfile'] ).".".$_GET['ext']);
header('Content-type: Image/jpeg');
readfile( "wallpapers/".$rFILE.$rRes.$rExt );
?>
i pass the variables and the files are being downloaded.. but the images are not being created ..
i see a image file with image icon with correct name but theres no preview
the images are not opened in any image viewer (all the images i use are JPEGs)
all the downloaded images have same size 336 to 337 bytes... so i thought maybe transfer size is limited and so i use Similar SO Question but after that only the file size grew to 445bytes to 446bytes nothing more...
WHAT TO DO.. i need to provide direct links for image download...
I'll bet a beer that the 336 to 337 bytes contain a PHP error message that will tell you what the problem is.
Remove the JPEG content-type header to see the output.
at line 1 you're witing $rFile, in line 5 it's $rFILE - maybe thats the problem? if not, it's impossible to say without seeing the error-message.
I hope that you are doing some sanitizing of $_GET data or you'll have a lot bigger problems than image data not downloading correctly. That code is ripe for a directory traversal attack.
okay now i did this
$rFile = $_GET['sfile'];
$rExt = $_GET['ext'];
header("Content-disposition: attachment; filename=".urlencode( $_GET['sfile'] ).".".$_GET['ext']);
header('Content-type: Image/jpeg');
readfile( "wallpapers/".urlencode($rFile).".".$rExt );
now the files are being download with correct size.. but i still can't see the preview.. my image veiwers & editors say this
ACDsee blank screen
Windows Page & Fax Viewer no preview available
MS Paint path Paint Cannot Read This File This is not a valid bitmap file. Or its format is not supported

Categories