Am dealing with webservices in my project. i am not sure about the bankend logic like what kind of method they used to store images in database.
This is the response i got as my profile image from server
Array
(
[0] => -119
[1] => 80
[2] => 78
[3] => 71
[4] => 13
[5] => 10
[6] => 26
.
.
.
[3778] => 123
[3779] => 60
[3780] => -82
[3781] => -57
[3782] =
)
How do convert this numerical array into PNG Image?
I have created a new image file and put all contents into that file using
file_put_contents('downloads/myImage.png', $profileImage);
$profileImage variable contains the above array.
this code creates a new image with some dump of datas. I know i have missed something by doing this. please help me with this.
i have used this same code to create PDF files from the server in this same project. that's working fine. thats why i tried with the same code to get my image. But its not working with images
Your question is a little bit unclear — I can't tell if you want to save the PNG file somewhere or deliver it as the response to an HTTP request. Also, you seem to imply that $profileImage is an array variable, but provide it as an argument to file_put_contents(), which expects a string.
But either way, the binary data you are being provided with looks fine.
If $profileImage is an array of signed byte values, then you can convert it to a string of binary data with the following command"
$rawPNG = implode(array_map('chr',$profileImage));
If you need to, you can then use file_put_contents() to write this data to a file. (The function is binary-safe, so there shouldn't be any problems.)
Alternatively, if you want to return a PNG image to a web client, just echo it with an appropriate Content-Type header:
header('Content-Type: image/png');
die ($rawPNG);
Related
I'm using http://image.intervention.io/ with Laravel to resize images before storing them in an AWS S3 bucket. Here is the applicable code:
$extension = $image->extension();
$realPath = $image->getRealPath();
$resizedImage = Image::make($realPath) // ->resize(255, 255); // resize or not, make() seems to always return an empty Image
$file_path = "posts/$post->id/images/image-$post->id-num-$pictureCount.$extension/";
$path = Storage::disk('s3')->put($file_path, $resizedImage->__toString(), ['visibility' => 'public']);
When debugging (breakpoint on the line $path is set) this I can see that $realPath holds a value like /private/var/folders/23/jl2dytp168g9g9s5j51w2m780000gn/T/php9047Fh - and going there I can see the image that I'm trying to make()/resize().
The $resizedImage object has the following fields:
[encoded] =>
[mime] => image/jpeg
[dirname] => /private/var/folders/23/jl2dytp168g9g9s5j51w2m780000gn/T
[basename] => phpTNRdXe
[extension] =>
[filename] => phpTNRdXe
I'm guessing that the encoded property should hold something like the base 64 image data?
When checking the image in AWS S3 bucket, it is always getting uploaded as a image with 0 bytes. What am I doing wrong here?
Intervention is configured to use GD if it makes any difference
Laravel File class is a subclass of Symfony File class which in turn subclasses \ SplFileInfo
The file object can be passed directly in Image::make. Image::make gets the realPath for \SplFileInfo objects.
Image::make returns an instance of Image and $resizedImage->__toString() returns the value of its encoded field. encoded starts out as an empty string.
The image should be encoded before accessing its encoded value.
$resizedImage->encode();
Then,
$path = Storage::disk('s3')->put($file_path, $resizedImage->__toString(), ['visibility' => 'public']);
I can't find out how to achieve this and i struggle whole morning to get it somehow but i can't.
I pull some data from steam from their steam api service in json file, but problem is i don't know if they did this on purpose or just assumed that people won't use their api so they posted a link to an image in json file but actual image on server don't exists, even if i visit their website for that package i pulled from json file image doesn't exists even on their website.
So i was wondering how do i override this?
When i pull json file the array returns this (it's just part where image is, actual json file is extremely large to copy it here)
[name] => Enclave
[page_image] => http://cdn.akamai.steamstatic.com/steam/subs/31131/header.jpg?t=1380891628
[apps] => Array
(
[0] => Array
(
[id] => 253980
[name] => Enclave
)
)
So if you visit a website for that game image doesn't exists, if you follow image link
http://cdn.akamai.steamstatic.com/steam/subs/31131/header.jpg?t=1380891628
it returns 404 error file not exists.
So in cases where image doesn't exists how do i override this and show my own image?
I tried with
if(!empty($image))
if(property_exists($image))
if($image !== false)
and of course $image path to json data [page_image]
none of combination works
However when image exists it shows me the image properly so $image is set properly.
Any idea how do i override this ?
getimagesize is a way to check if a resource actually is an image. getimagesize returns an array, if the link points to a valid image resource. You can use getimagesize on remote files also :
$image = 'http://cdn.akamai.steamstatic.com/steam/subs/31131/header.jpg?t=1380891628';
if (is_array(#getimagesize($image))) {
//image exists
} else {
//image does not exists
}
Instead of suppressing getimagesize warnings by #, change the level of error reporting in runtime, before the call :
error_reporting(E_ALL & ~E_WARNING);
you can switch back and forth so E_WARNING's still will be echoed in the rest of your application.
If you really want to do this from PHP, then you may:
$fh = #fopen($image, 'r');
$imageexists = $fh!=false;
if ($fh) fclose($fh);
I want to get the full path of image in PHP. I used <input> tag for image uploading. I'm unable to get the full path. when I alert the value of <input type="file">, I'm getting
c:/fake-path/image.jpg
Here is my code:
<input type="file" name="upload_captcha_background" id="upload_captcha_background" />
var file_path= jQuery("#upload_captcha_background").val();
alert(file_path);
and in PHP I'm fetching the value like:
$ux_image_path =$_FILES['upload_captcha_background'];
If you mean the path where the image was located on the user computer before he/she uploaded it to your form - you can never know it in php or javascript or anywhere else.
In PHP you can see the path on SERVER (usually in the temporary folder) where the file was stored so you can read or copy it. If may just happen that the server is as well your user computer (i.e. you are testing the website on http :// localhost) but don't confuse those two things.
You can never be sure of getting a full filepath or even a reliable filename or content-type submitted in a file upload file.
you should never do so....and i think trying it in latest browsers is useless(from what i know) .. all latest browsers on the other hand , will not allow this.
Some related Stackoverflow questions:
How to get the full path of the file from a file input
Full path from file input using jQuery
Retrieving the full path (server-side) of a file uploaded using Firefox
the structure for
$_FILES[]
as follows
/*
Array
(
[image] => Array
(
[name] => Array
(
[0] => 400.png
)
[type] => Array
(
[0] => image/png
)
[tmp_name] => Array
(
[0] => /tmp/php5Wx0aJ
)
[error] => Array
(
[0] => 0
)
[size] => Array
(
[0] => 15726
)
)
)
*/
if you are trying to upload a file and copy it to a directory
use the following code
$tmp_name = $_FILES['upload_captcha_background']["tmp_name"];
$name = $_FILES['upload_captcha_background']['name'];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
I'm trying to upload a zip file and a csv file from HTML form.
On PHP, When I printed $_FILES (Actually $request->getFiles() in symfony), I got following.
Array
(
[zipfile] => Array
(
[name] => tempfiles.zip
[type] => application/octet-stream
[tmp_name] => C:\wamp\tmp\php5D42.tmp
[error] => 0
[size] => 850953
)
[csvfile] => Array
(
[name] => test.csv
[type] => application/vnd.ms-excel
[tmp_name] => C:\wamp\tmp\php5D52.tmp
[error] => 0
[size] => 312
)
)
I'm wondering with the type and tmp_name. I need to take few decisions based on type. Is it safe to take decisions on existing type? Will I get same result for similar files on Linux server?
Again tmp_name have .tmp extension. Is it consistent on both windows/linux? If not, is there any way that the code I write on windows (decision using type) will work on linux without any issue?
Using this type can be dangerous Because user can change the type of the files and can upload a php script.
You should validate the type first just like get_image_size() to validate a image file.I have no idea about .zip file
It is not safe to trust the type form $_FILES, you need to validate the file type in server side.
For .tmp extension, it is ok both on windows or linux.
can anyone shed some light on this:
Array
(
[video] => Array
(
[name] => 20051210-w50s.flv
[type] =>
[tmp_name] => /tmp/php38JFea
[error] => 0
[size] => 669036
)
)
I'm uploading an flv file, but the [type] is not being filled, is this common?
cheers in advance!
The type field is a MIME type set by the client. In this case, it just didn't know what a flv file is, and didn't set it. It is a good thing not to rely on this information, as it can be freely altered by an attacker.
If you want to get reliable info about an uploaded video file, you need to do a server-side check. The getid3 for example seems to be able to recognize flv files.
The built-in getimagesize() function can do the same for many image formats.