how would i make a dynamic image? - php

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

Related

imagegrabscreen function in php only gets a black screen, how do I fix this?

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?

How to get and save THIS transparent image with PHP

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.

Return image using restler

Is it possible to use Luracast Restler to return an image? Something like calling:
http://myserver.com/api/users/002/avatar
to download a png?
It is possible to serve images with Restler.
You need to do the following in your API method
Set the content type header for the right image type (png, jpeg etc)
header("Content-Type: image/png");
echo the image content
Example
$im = imagecreatefrompng("test.png");
header('Content-Type: image/png');
imagepng($im); //this sends the image as the response
imagedestroy($im);
use exit or die to stop execution instead of the usual return result

PNG manipulation removes transparency

I am simply trying to return an PNG image through PHP, but I have a problem with the transparency not showing right. (Basically one PHP file will be capable of returning any of my images.)
I use simple code to return the image:
<?php
$im = imagecreatefrompng("images/fakehairsalon.png");
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>
The original image looks like this:
And the one returned via PHP (and that piece of code) looks like this:
Is there anything I can do to prevent this and make the image come through normal?
As explained in a user comment, you must do this:
<?php
$im = imagecreatefrompng("borrame.png");
header('Content-Type: image/png');
imagealphablending($im, true); // setting alpha blending on
imagesavealpha($im, true); // save alphablending setting (important)
imagepng($im);
imagedestroy($im);
?>
Update: This answer was assuming that your code is an except from a bigger script you are using to do on-the-fly image manipulation.
If you don't want to change the original file, this is a plain waste of memory and CPU cycles. You can use file system functions to read it as a regular file, such as readfile().
It's also worth noting that using PHP to deliver a file only makes sense if you want to do something else as well, such as:
Restricting access to the file
Keeping a counter

PHP ImageFilter

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.

Categories