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
Related
I am trying to make the profile view of user using image from byte array. I have successfully rendered the image, but I have problem including the image with other profile content, because of the header('Content-Type: image/png') which takes the whole file as an image. How to resolve this problem?
$myArray = implode('', array_map(function($e) {
return pack("C*", $e);
}, $image));
if (substr($myArray, 0, 4) == "\x89PNG") header('Content-Type: image/png');
else if (substr($myArray, 0, 2) == "\xFF\xD8") header('Content-Type: image/jpeg');
else if (substr($myArray, 0, 4) == "GIF8") header('Content-Type: image/gif');
echo $myArray;
You can't serve raw image data as part of an HTML document.
There are a couple of ways you can go about serving the image:
Base64 encode it and embed it in the img src. See this thread for an example.
Save the image to a file and serve it like you would any other image.
#2 had the advantage of allowing the image to be cached by the browser. If the images are large I would try to work out a way to save it to a file.
How may I display an image without using any kind of HTML.
Let's say I have some protected images, which may only be shown to some users.
The protected image is in directory images/protected/NoOneCanViewMeDirectly.png
Then we have a PHP file in the directory images/ShowImage.php, which check if the user is permitted to view the image.
How may I display the image using PHP only.
If you're not sure what I mean this is how I want to display the image. http://gyazo.com/077e14bc824f5c8764dbb061a7ead058
The solution is not echo '<img src="images/protected/NoOneCanViewMeDirectly.png">';
You could just header out the image with the following syntax
header("Content-type: image/png");
echo file_get_contents(__DIR__."/images/protected/NoOneCanViewMeDirectly.png");
if (<here your logic to show file or not>)
$path = $_SERVER["DOCUMENT_ROOT"].<path_to_your_file>; // to get it correctly
$ext = explode(".", $path);
header("Content-Type: image/".array_pop($ext)); // correct MIME-type
header('Content-Length: ' . filesize($path)); // content length for most software
readfile($path); // send it
}
Note: 1. image/jpg is not correct MIME-type, use image/jpeg instead (additional check needed), 2. readlfile() is better than echo file_get_contents() for binary data, 3. always try to provide content length for browsers and other software
You can use imagepng to output png image to browser:
$im = imagecreatefrompng("images/protected/NoOneCanViewMeDirectly.png");
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
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 load an image through PHP, but I don't know how.
The filename is stored in the database, such as image.jpg
if($_GET['image']){
// Client requesting image, so retrieve it from DB
$id = mysql_real_escape_string($_GET['image']);
$sql = "SELECT * FROM $tbl_name WHERE id = '$id' LIMIT 1";
}
The client needs to request an image like so
http://example.com/index.php?image=1
And then it should return the image, so it can be embedded like this:
<img src="http://example.com/index.php?image=1" alt="" />
Is this possible?
$img = 'path/to/image.jpg';
header('Content-Type: image/jpeg');
readfile($img);
just tested it
You can use the GD library for that. You start by creating a resource using a function like http://php.net/imagecreatefromjpeg. You will need to provide the path as a parameter.
After that, you just output the resource using a function like http://php.net/imagejpeg.
Don't forget to send the content type header, and also to use imagedestroy on the resource.
Update:
Consider this sample:
$im = imagecreatefromjpeg('path/to/image.jpg');
header('Content-Type: image/jpeg');
imagejpeg($img);
imagedestroy($img);
I suggest you first make a file called image.php. So you will call image.php?id=1
Have image.php header be the image type. header('Content-Type: image/jpeg');
Then you can use the GDImage library in PHP to load the image, and output it. Or you can read the file and output it. The header() is key.
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