Saving images from dynamic PHP url - php

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 );

Related

How can Fopen read png files in php

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);
?>

How to processing an image downloaded from AWS S3 with Laravel 5?

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.

base64 decode mixed results

I have the following code in PHP, and it works for the most, fine. I am sending a image from a mobile device to this script, which decodes it into a img file and creates a file out of it on the server. I am 99.9% sure every time its a base64 encoded.
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: image/jpeg');
$data = ($_POST['imageData']);
define('UPLOAD_DIR', 'images/');
$img = str_replace('data:image/jpeg;base64,', '', $data);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.jpg';
file_put_contents($file, $data);
echo ('{"imgUrl" : "' . $file . '"}');
?>
This then returns the image URL back to be added to a database.
The problem is, most of the time it does decode into a .jpg file, and other times into a txt file. I cannot see why it does it, as its a little random. But I have noticed that sometimes it will come as a $_POST, and other times, $_POST is Null. So I looked at using:
$data = json_decode(file_get_contents('php://input'));
But again, it seems inconsistant. But I put a logic statement such as:
$data = ($_POST['imageData']);
if($data == NULL) {
$data = json_decode(file_get_contents('php://input'));
}
Is there any reason I should be aware of why the code works, and sometimes does not work ?
I know this question is old, but is one of the first appearance by looking at this topic. So everyone looking at this question can find a link to a proper answer right away.
You should check this PHP - get base64 img string decode and save as jpg (resulting empty image )
Also check the conditions you're using, because
if ($data === NULL)
it may be different for
if ($data == NULL)
Also, you're saving the base64 string incorrectly to an image file.
Check that link and let me know how if it helped.

Download images from specific array of urls with php

I need to download some images from a specific array of urls, something like:
<?php
$images = array('http://url1.com/img1.png','http://url1.com/img2.png');
// download this images from this paths
I saw some scripts here but don't get them exactly on how can i link them to this array.
The expected output would be: When i run the script to download those images from that array to a specific folder from my server: home/user/public_html/images. It's much appreciated anyone who helps me, i am trying but can't make the connections, currently a rookie.
Something like this maybe.
$images = array('http://ecx.images-amazon.com/images/I/214RgVjsvTL.jpg','http://ecx.images-amazon.com/images/I/515pMJlul8L.jpg');
foreach($images as $name=>$image) {
//get image
$imageData = file_get_contents($image); //$image variable is the url from your array
$name = explode("/", $image);
$handle = fopen("images/".$name[5],"x+");
fwrite($handle,$imageData);
fclose($handle);
}

Replace .dds to .png?

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);

Categories