I was working on something and I needed the data out of a png file and I can only use PHP. Don't ask why just give me the answer lol
Lets suppose below is your image.
You have to define its url in a variable using the below line
$image = 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png';
after that you can use file_get_content function to get data/content of image.
$image_content = file_get_contents($image);
also, if you want to convert it into base64 string, you can use base64_encode function.
$image_base64Data = base64_encode($image_content);
Whole Code will be ......
<?php
$image = 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png';
$image_content = file_get_contents($image);
$image_base64Data = base64_encode($image_content);
?>
Related
I'm using below code
Here $data is a byte array.
$decoded_data = base64_decode($data);
$im = imagecreatefromstring($decoded_data);
if ($im !== false) {
header('Content-Type: image/png');
$image_nam = "png_".time().".png";
imagepng($im, $image_nam);
imagedestroy($im);
}
But I don't get the exact output
try this simple code for save image into directory
file_put_contents('/myfolder/imageName.jpg', base64_decode($img));
you can also try this
$base_to_php = explode(',', $base64Img);
$data = base64_decode($base_to_php[1]);
$filepath = "/path/imageName.png"; // or image.jpg
file_put_contents($filepath,$data);
I would suggest you to refer link -
This could be possible an issue with metadata present in base64 string.
As per source the problem could be due to data:image/png;base64, is included in binary string.
The metadata present in the binary string results in invalid image data when the decoded back. Can you try it by removing metadata in the function before decoding the string.
You may use function and write output to a new file.
$position_s= strpos($base64_string , "data:image/png;base64,");
substr($base64_string, 0, $position_s) . substr($base64_string, $position_s+ strlen("data:image/png;base64,");
Note: If you are preferring explode function, it would be best to set limits:
explode(',',$base64_string,2)
I want download an image from AWS S3 and process it with php. I am using "imagecreatefromjpeg" and "getimagesize" to process my image but it seem that
Storage::disk('s3')->get(imageUrlonS3);
retrieve the image in binary and is giving me errors. This is my code:
function createSlices($imagePath) {
//create transform driver object
$im = imagecreatefromjpeg($imagePath);
$sizeArray = getimagesize($imagePath);
//Set the Image dimensions
$imageWidth = $sizeArray[0];
$imageHeight = $sizeArray[1];
//See how many zoom levels are required for the width and height
$widthLog = ceil(log($imageWidth/256,2));
$heightLog = ceil(log($imageHeight/256,2));
//more code here to slice the image
.
.
.
.
}
// ex: https://s3-us-west-2.amazonaws.com/bucketname/image.jpg
$content = Storage::disk('s3')->get(imageUrlonS3);
createSlices($content);
What am I missing here ?
Thanks
I think you are right in your question what the problem is - the get method returns the source of the image of itself, not the location of the image. When you pass that to createSlices, you're passing the binary data, not its file path. Inside of createSlices you call imagecreatefromjpeg, which expects a file path, not the image itself.
If this indeed the case, you should be able to use createimagefromstring instead of createimagefromjpeg and getimagesizefromstring instead of getimagesize. The functions createimagefromstring and getimagesizefromstring each expects the binary string of the image, which I believe is what you have.
Here's the relevant documentation:
createimagefromstring - http://php.net/manual/en/function.imagecreatefromstring.php
getimagesizefromstring - http://php.net/manual/en/function.getimagesizefromstring.php
Resulting code might look something like this:
function createSlices($imageData) {
$im = imagecreatefromstring($imageData);
$sizeArray = getimagesizefromstring($imageData);
//Everything else can probably be the same
.
.
.
.
}
$contents = Storage::disk('s3')->get($imageUrlOnS3);
createSlices($contents);
Please note I haven't tested this, but I believe from what I can see in your question and what I read in the documentation that this might just do it.
I have a data uri variable in php
$imageURL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyAAAAJYCAYAAACadoJwAAAgAElEQ…ACIiACIiAC5U1AAqS891erEwEREAEREAEREAEREIFAEfj/bfXX..."
I am trying to insert this into a pdf using fpdf for which I need to convert this into a image I guess. I tried doing something like
base64_decode($imageURL);
but this does not work. How I successfully insert this data uri into pdf.
$image_content = base64_decode(str_replace("data:image/png;base64,","",$imageURL)); // remove "data:image/png;base64,"
$tempfile = tmpfile(); // create temporary file
fwrite($tempfile, $image_content); // fill data to temporary file
$metaDatas = stream_get_meta_data($tempfile);
$tmpFilename = $metaDatas['uri'];
Now you can use that image into fpdf like:
$pdf->Image($tmpFilename,null,null,0,0);
Or you can specify image type by adding image type parameter like this:
$pdf->Image($tmpFilename,null,null,0,0,'PNG');
Please check to http://www.fpdf.org/en/doc/image.htm
I'm trying to save a jpg file from a dynamic url, looks like this,
http://bks7.books.google.se/books?id=TL3JGsUOArkC&printsec=frontcover&img=1&zoom=1&&source=gbs_api
file_get_contents can't get the content correctly, here is my code,
<?php
$image_url = "http://bks7.books.google.se/books?id=TL3JGsUOArkC&printsec=frontcover&img=1&zoom=1&&source=gbs_api";
$img = file_get_contents($image_url);
$folder = 'C:/xampp/htdocs/test/test.jpg';
file_put_contents($folder, file_get_contents($img));
?>
Appreciate any ideas or alternative as "easy" methods.
One problem is that you have 2 file_get_contents calls. The first call:
$img = file_get_contents($image_url);
Returns the response from the request to the URL and stores it in the $img variable. The second call:
file_put_contents($folder, file_get_contents($img));
Doesn't make any sense. Instead, just do this:
file_put_contents($folder, $img );
I am looking for an way to replace .dds to .png with php.
The reason is this every images is automaticly inserted into the database with the extension .dds the .dds is an format that the game uses to render its images so i cant replace this.
So what i do is fetch the icons from the database and then the file name must be changed to .png before i insert it into an other database.
I have tried it with preg_replace but i need some help on how to set it up.
$str = str_replace('.dds', '.png', $mystring);
I don't think replacing extension from DDS to PNG is enought. I think you should rather convert from DDS to PNG:
<?php
$url = 'http://server.com/image.dds';
$data = json_decode(file_get_contents('http://api.rest7.com/v1/image_convert.php?url=' . $url . '&format=png'));
if (#$data->success !== 1)
{
die('Failed');
}
$image = file_get_contents($data->file);
file_put_contents('rendered_page.png', $image);