GD imagefilter no image output - php

I'm trying to convert an jpg image to grayscale with GD imagefilter but i can't get the filtered image to output on the browser. I use this code
<?php
$thumb="w=250&q=100";
$imgstring = get_image('cover_image',1,1,0,NULL,$thumb);
$im = imagecreatefromjpeg($imgstring);
imagefilter($im, IMG_FILTER_GRAYSCALE);
header("Content-type: image/jpg");
imagejpeg($im);
imagedestroy($im);
?>
**The $imgstring is the url string of the image ('http://xxx.xx/image.jpg').
With the header content line the result is an error "Warning: Cannot modify header information - headers already sent by..." followed by what appears to be the image code (a lot of question marks characters and other garbage).
Without the header content line the result is just the image code(?) again (symbols and characters)
What's wrong?

The error is exactly what it says - something in your code has caused output to be performed, which kills the header() call, preventing the content-type from being set. In the absence of any valid content-type header, the client browser assumes text/plain, and is showing your JPEG image - as raw "garbage".
You need to figure out WHERE in your code that output is being performed (I'm willing to bet a shiny penny it's in that get_image() function, and eliminate it.

Related

ANSI <--> UTF-8 charset issue, when displaying images from the database (PHP)

I am loading images from a MySQL database, to display them in an Web GUI.
This is pretty standard and worked pretty well, till I tried to install the software in russia...
Here a example of the code that loads the image:
// Load overview image
if ($global_mode == 'overview') {
// Load the image from the database.
mysql_select_db("$db_x");
$sql = "SELECT $db_x.sensor_images.image
FROM $db_x.sensor_images
WHERE $db_x.sensor_images.image_id = '" . $global_image_id . "'";
$sql = mysql_query($sql);
$row = mysql_fetch_assoc($sql);
// Image output.
header('Content-type: image/jpeg');
echo $row['image'];
}
I installed the software on many european based laptops and I never had the problem, that images were not displayed...
Apparently on russian laptops (Windows 7, XAAMP, MySQL) this was not the case, images were not displayed.
I started to do some research and found out (on my laptop, where images get displayed...), that if I change the Encoding of the php file (in this case the show_image.php), I could replicate the error I had on russian laptops.
If the encoding is set to ANSI, the images get displayed...
Here I have disabled the header, so the browser displays the binary data (the encoding of the PHP file is set to ANSI)...
EXAMPLE A
Now I set the Encoding of the PHP file to UTF-8
By doing this images do not get displayed any more...
This is the output when I try to display the data without the header...
EXAMPLE B
As you can see, the output is different...
On my laptop (european):
ANSI: images get displayed, the data (without header) looks like EXAMPLE A
UTF-8: images get not displayed, the data (without header) looks like EXAMPLE B
On russian laptops:
ANSI: images get not displayed, the data (without header) looks like EXAMPLE B
UTF-8: images get not displayed, the data (without header) looks like EXAMPLE B
I still don't understand why changing the encoding of a php file has an impact on the output of binary data, respectively an image...
On the russian laptops the PHP files get always interpreted as if the encoding was set to UTF-8, no matter if I set it to ANSI or something else...
Please help!
Thx.
In your IDE you see "UTF-8" and "UTF-8 without BOM". You're choosing UTF-8, which in this case means with BOM. The BOM is prepended to the file and is the first thing that's output. This may a) break the output of your header, thereby breaking the data display, and b) giving the browser a clue that the following data is supposedly UTF-8 encoded, hence the browser is interpreting the data as UTF-8, which results in a lot of UNICODE REPLACEMENT CHARACTERS �. Check your error logs, you should see PHP complain about Headers already sent.
The data you're sending is always the same, it's just interpreted in different encodings depending on the machine's default and the presence or absence of a UTF-8 BOM.
The only reason it breaks at all under any circumstances is that you're outputting the wrong headers and/or are sending additional content before or after the image data. Check with a low-level tool like curl what exactly is output, and find and remove anything that doesn't belong.
Save the php File that sends out the picture as "UTF-8 w/o BOM".If there are any files inlcuded, it is mandatory that they are saved as either "ANSI" or "UTF-8 w/o BOM", too. There also must be no space nor any text before the <?php -Tag.
If you want to send any text, e.g. an error message because the picture file is non-existent, you need to send the header("Content-Type: charset=utf-8"); right before the text in order to display all characters correctly - but not in combination with the image:
<?php
include "/someSafeDir/utils.inc.php";
$pic= secureGet($_GET['pic']);
$imagePath=/somePath/
$picture=$imagePath.$pic;
if (file_exists($picture)){
if ($fd = fopen ($picture, "r")) {
$fsize = filesize($picture);
header ("Content-type: image/jpeg");
header ("Content-length: $fsize");
readfile($picture);
} else
echo "File \"$pic\" could not be opened.\n";
fclose ($fd);
} else {
header("Content-Type: text/html; charset=utf-8");
echo "File \"$pic\" not existent";
}
?>
It could be that your PHP server charset is not the correct one.
In your php.ini file, try having the following directive:
default_charset = "UTF-8"
And restart your server.

Encoding bug with an image and file_get_contents in PHP

I use this code to retrieve and display an image:
header("Content-type: image/png");
echo file_get_contents(site_domain().image_asset_module_url("header.png",$this->name));
on my local WAMP it works, but on the remote server file_get_contents returns a wrong-encoded string:
Local:
‰PNG IHDR^jRÀ2¡ pHYsÒÝ~üÿÿIDATxÚ콘Uõµþ¿`ŠŠÔéÃÕ¨¹&&ù'77¹i¦˜è‰=V:RlH‡™aAlH™B¯Jbh...
Remote:
�PNG IHDR^jR�2� pHYs��~���IDATx����U����`������ը�&&�'77�i��草=V:Rl...
If I use utf8_encode I get:
PNG IHDR^jRÀ2¡ pHYsÒÝ~üÿÿIDATxÚì½Uõµþ¿`ÔéÃÕ¨¹&&ù'77¹i¦è=V:RlHaAlHB¯Jbh...
So I always get a break picture on my remote Server - why and what is the solution?
The data is always the same. file_get_contents does not alter data in any way. You're also not dealing with text in some encoding, but with binary data. Any sort of text-encoding or conversion thereof does not apply here.
Your first sample is the binary image data as interpreted as Latin-1 encoded text.
Your second sample is the same binary data as interpreted as UTF-8 encoded text.
I.e., the data is fine, the interpretation is wrong. The interpretation should be set by the Content-Type header, perhaps this is not being set correctly on the remote server. For this problem, inspect the raw HTTP response headers and see How to fix "Headers already sent" error in PHP.
I would have rather used
<?php
$file = 'http://url/to_image.png';
$data = file_get_contents($file);
header('Content-type: image/png');
echo $data;
Or Can you try this
$remoteImage = "http://www.example.com/gifs/logo.gif";
$imginfo = getimagesize($remoteImage);
header("Content-type: $imginfo['mime']");
readfile($remoteImage);

php base64 encode imagecreatetruecolor

I have a php script that generates an image then outputs the image as a png.
$img = imagecreatetruecolor($graphWidth,$graphHeight);
...
*drawing routines*
...
header("Content-type: image/png");
imagepng($img);
What I now need is to get the php script to base64 encode the image data and output that string (so I can eventually pass that encoded data to a java script which decodes and adds this image to a generated pdf).
I have tried many times to get it working myself using other stackoverflow posts/answers etc. but I don't understand enough about this technology to have gotten it to work.
Can someone help me with this please?
Thanks in advance,
Chris
The reason this doesn't work is because the image in $img is a resource, not an actual PNG image. When you call imagepng(), it outputs the image.
What I would do is create an output buffer, and base-64 encode it.
ob_start();
imagepng($img);
$buffer = ob_get_clean();
ob_end_clean();
echo base64_encode($buffer);
Don't forget to change your Content-Type header, as this is no longer image/png.

PHP: GD reference to binary

I have a gd image reference (I've already manipulated the image how I want to), and now I want to store the image. Instead of saving it directly to a file, is there a way I can get the binary data, that way I can convert it to base64 then save the base64 string. The only option I've been able to find is imagejpeg($image), but that either saves it as a file or prints it directly to the browser.
You can output to the buffer, and then capture it in this way.
ob_start();
imagejpeg($image);
$data = ob_get_contents();
ob_end_clean();

problem in showing an img open from zip archive

iam get the img date from the zip archive by getFromIndex()
and the date like that (this the first line of text appearing)
‰PNG ��� IHDR���#���#���ªi
know how i can makke appear
iam used
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
but its did not worked
and used
header('Content-Type: image/png');
imagepng($data);
imagedestroy($im);
but its did not worked and giving me
Warning: imagepng(): supplied argument is not a valid Image resource
now you asked me about the date format
we assume the output is
$date = file_get_contents('http://sstatic.net/so/img/logo.png');
the text appear is the same format
but i must use $date because i must get the contents first
I suspect this image data is not the only output of this script, right?
But it must be.
The data already looks like a PNG file (notice the magic number). What happens if you output the data to a file ? Or How about just echo the data and don't call imagepng. Will that work ?

Categories