I have writen a small script to go in a Facebook App that can filter images for you. I am having trouble with the GRAYSCALE filter It seems to only display what I think is byte code for the image, instead of the image. I think this may have something to do with the headers and content type. I need to display the image filtered by PHP with this code:
header("content-type: image/jpeg");
$image = imagecreatefromjpeg("http://majik.zbrowntechnology.info/upload/zbt_1794056140.jpg");
imagefilter($image, IMG_FILTER_GRAYSCALE);
imagepng($image);
imagedestroy($image, 'test.jpg');
on an HTML page. Any ideas?
You set Content-Type to image/jpeg but send a PNG image.
header("Content-Type: image/jpeg");
imagejpeg($image);
This should work.
BTW: imagedestroy() only has one argument
The script works fine for me, I see the grayscaled image(even with the wrong parts listed above)
If you see there the source I would guess at first, that there is any output before the headers can be sent.
Set error_reporting to E_ALL, so you can see if and where there is some unintended output.
Related
Here is the code I'm using:
<?php
$im = imagegrabscreen();
imagepng($im, "C:/pathtofile/image.png");
imagedestroy($im);
The images get saved but when I open them, they are just black. I'm using Apache for my server, I already enabled "Allow services to interact with desktop" in services. I have also tried using jpg instead of png. Everything just results in a black image. I have also tried with this header: header('Content-type: image/png'); but that doesn't make any difference.
Anyone have any suggestions?
I am trying to get a image from a URL, and then save it to the server. It is a simple task, yeah, but this image has transparency... The thing is, when i save that file to my PC or when I try to get and save it via PHP to my server, then I get the same result: the image gets all messed up with its transparency set to black. The URL of the image is:
http://cellufun.com/p/avatarimage/agb.aspx?i=Body|M04001|Shirt|M10020|
Another weird thing is my browser says the image is MIME Type image/png but I can't use imagecreatefrompng(); because I get the "image not a valid PNG" error...
Here is my code (I did try other solutions, this is what I tryed the last time):
<?php
$result=file_get_contents("http://cellufun.com/p/avatarimage/agb.aspx?i=Body|F04001|Shirt|F10020|Pants|MO55|");
$img = imageCreateFromstring($result);
imageAlphaBlending($img, true);
imageSaveAlpha($img, true);
imagecolortransparent($img);
header("Content-type: image/png");
imagepng($img);
?>
Oh, and I just tryed this image against the copy() function, and still get the same result... It looks like the image is smaller in size then the original image...
Just tryed this:
file_put_contents("files/test.png",file_get_contents($result));
and still not working... so it has something to do with the image itself, because whatever i try to get the data, it does not work.
Try this code (changes at lines 2-4):
<?php
$img = imageCreateFromgif("http://cellufun.com/p/avatarimage/agb.aspx?i=Body|M04001|Shirt|M10020|");
$index = imagecolorexact($img, 0, 0, 0);
imagecolortransparent($img, $index);
header("Content-type: image/png");
imagepng($img);
?>
It works for me although it's a little trick ;) ..and I don't know exactly why the quality is worse..
PS: The main problem I see is that the image at the url you provided is not really a png.
I'm trying to use Php's gd library to show emails on a my page as an image, so they can't be used by spammers. The problem is that the tutorial I am following uses a header with Content-type: image/jpeg. This means that I can only have images on this page.
How can I use the GD library to show the image in an Html/Php page? Or is there any other way of doing it?
When you change the header of the page, the whole page is rendered according to that header. In the case of using something like emails, you have a few options if you want to keep it single-paged.
Your first option is to use base64 encoding.
//create the image, $im
//omit the header settings
ob_start();
imagejpeg($im);
$data = ob_get_contents();
ob_end_clean();
echo '<img src="data:image/jpeg;base64,'.base64_encode($data).'">';
Here we place an output buffer to stop anything from going to the client, and store it in our variable $data. Then we just encode it using base64_encode and output to the page the way that the browser can interpret it.
There's also a get method that you can use.
if(isset($_GET['email'])) {
header("Content-type: image/jpeg");
//create the image, $im
imagejpeg($im);
imagedestroy($im);
die;
}
echo '<img src="?email">';
Really, we're just sending an email input and processing with an if statement.
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")
I have a online.png and a offline.png
My idea is to do serverstatus.php?sid=1
then have it check if the server is online or not, i know how to run the checks for that.
Then have it return the online or offline image with the mime type of png but really a php file and show up like a image. No text or anything added to it. just the original online or offline png. So it can be used for img tags.
From PHP documentation at http://php.net/manual/en/function.imagepng.php
<?php
$im = imagecreatefrompng("test.png");
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
You can use the imagecreatefrompng function