Upload with curl from file_get_contents / imagecreatefromstring - php

I have the follow code:
$image = imagecreatefromstring(file_get_contents('http://externaldomain.com/image.png'));
And want to make a curl request:
$files = array();
$files[] = '#' . $image['tmp_name'] . ';filename=' . $image['name'] . ';type=' . $image['type'];
$ch = curl_init('index.php?route=upload');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $files);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
The main problem is, how to get the file name and extension from $image variable?
I can replace $image['tmp_name'] with tempnam(), but other things?
Have another way to do?

imagecreatefromstring returns a GD handle. There is no tmp_name, name, OR type associated with it. You're trying to treat that GD handle as if it was the result of a standard POST-based $_FILES data structure.
a GD handle cannot be passed to curl for upload, because it's not really an image, and won't be until you use imagejpeg/imagepng/imagewhatever to save it out to a file, which you can then point curl at, e.g.
$image = imagecreatefromstring(...);
imagejpeg($image, 'tempfile.jpg');
$files[] = '#tempfile.jpg;filename=tempfile.jpg;type=image/jpeg';
curl...

Related

Convert POSTed Image to Base64

I have a site that I am working on and it uses the imgur api to upload user's profile picture, but it needs to be base64 to use the function I have:
function imgur($image) {
$client_id = "client_id_some_numbers_and_stuff";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.imgur.com/3/image.json');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $client_id));
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => base64_encode($image)));
$reply = curl_exec($ch);
curl_close($ch);
$reply = json_decode($reply);
$link = $reply->data->link;
return $link;
}
I get the image from a form input (<input type="file" id="pp" name="pp">). When I get the data from $_POST["pp"], it is set to the string of the filename, and nothing useful.
How can I get the image file and either pass it into the imgur() function, or convert it into base64 and then pass that into the imgur() function, and just remove the base64 encoding from the function.
Thanks :)
Edit: This post is different that the one marked as duplicate becuase I want to know how to send the form data elsewhere, and not have it uploaded to my site.
info of upload file contains $_FILES array
you need to get your upload file path in server and translate it into base64
$image = $_FILES["pp"]["tmp_name"];
$type = pathinfo($image, PATHINFO_EXTENSION);
$data = file_get_contents($image);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
imgur($base64);

Get file contents without file_get_contents

I'm not allowed to use file_get_contents. Originally I got the file contents by simply doing this:
$filepath = $_FILES['resume-attachment']['tmp_name'];
$filecontent = file_get_contents($filepath);
$encodedFile = base64_encode($filecontent);
Unfortunately, it's not allowed.
$filepath = $_FILES['resume-attachment']['tmp_name'];
$filename = $_FILES['resume-attachment']['name'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $filepath);
$filecontent = curl_exec($ch);
curl_close($ch);
$encodedFile = base64_encode($filecontent);
The code above is my attempt at using cURL. I get the filename uploaded, so I get some reaction, but the uploaded file is 0.0 bytes of size... which is not correct. I would think that perhaps the issue could be that I shouldn't treat $filepath as a URL, but what would the alternative be? I should also mention that I'm not trying to post it from this code. I simply want to get the file contents, and then encode it. It's part of an XML string later on.

PHP Unable to view image when uploaded from client to server

I am trying to upload images from client to server. My Client and server machines are separate so I need to send the image data from client to server.
My Client side php code goes like (Full client-side code):
$filename = $_FILES["fileToUpload"]["name"];
$filedata = $_FILES['fileToUpload']['tmp_name'];
$imagedata = file_get_contents($_FILES['fileToUpload']['tmp_name']);
$fields = array(
'filename' => $filename,
'filedata' => "#$filedata"
'imagedata' => "#$imagedata"
);
$field_string = http_build_query($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://10.2.16.102/temp.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_INFILESIZE, $filesize);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $field_string);
$server_output = curl_exec ($ch);
curl_close ($ch);
and my Server side code is (Full server-side code):
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
echo " and the filename is ". $_POST['filename'];
$uploadpath = "/uploads/";
$target_file = $_SERVER['DOCUMENT_ROOT']. $uploadpath . $_POST['filename'];
file_put_contents($target_file, $_POST['imagedata']);
//move_uploaded_file($_POST['filedata'],$target_file);
//copy($_POST['filedata'],$target_file);
?>
I am able to store the image in my uploads folder and the filesize is also the same but I am not able to see the image. The image viewer says "Couldn't load the png file". Do I have to convert the file into png format before storing. Also how to do that? Any help?
In a nutshell, I need a converter which can convert the file format into png or any other image format which can convert my file into an image at the server end.
Use $_FILES instead of $_POST in move_uploaded_file function e.g:
move_upload_file($_FILES[filedata][tmp_name], YOUR_PATH/$_FILES[filedata][name]);
Here's PHP doc:
http://php.net/manual/fr/function.move-uploaded-file.php

Can I perform such an action with cURL?

I need to give my images an extension, depending on their type and also create a completely new name for them. Can I get the extension before I send the file to the server?
$_POST['image'] is a URL of an image. $new_name is supposed to be added while running the switch.
$image = $_POST['image'];
$ch = curl_init($image);
curl_setopt($ch, CURLOPT_HEADER, 0);
$imagedata = curl_exec($ch);
list($width, $height, $type) = getimagesize($filename);
switch ($type) {
case 1: ....... //Give the filename a .gif extension and so on for every filetype
}
$fp = fopen('../images/' . $new_name, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_close($ch);
fclose($fp);
!FOUND ANSWER! (sorry if I'm answering my own question wrong (in means of simply "editing" the question), comment on how to do it correctly, if I'm wrong ;)
I simply need to run the cURL function 2 times, but with different names. The first one simply gets me the info about the file, and the second one saves it wit the $new_name
$image = $_POST['image'];
$ch = curl_init($image); curl_setopt($ch, CURLOPT_HEADER, 0);
$imagedata = curl_exec($ch); $info = curl_getinfo($ch,
CURLINFO_CONTENT_TYPE); curl_close($ch);
if($info == "image/jpeg") //Running a code that will give the image an
appropriate extension...
$cf = curl_init($image); $fp = fopen('../images/' . "image.jpg",
'wb'); curl_setopt($cf, CURLOPT_FILE, $fp); curl_exec($cf);
curl_close($cf); fclose($fp);

Send cURL request from URL?

Greetings,
I'm looking for a way to send a curl request given a full url. All of the examples and documentation I can find look something like this:
$fullFilePath = 'C:\temp\test.jpg';
$upload_url = 'http://www.example.com/uploadtarget.php';
$params = array(
'photo'=>"#$fullFilePath",
'title'=>$title
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, $upload_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$response = curl_exec($ch);
curl_close($ch);
The problem is that the file, "test.jpg" is actually generated dynamically by a script on the server (so it doesn't exist on the file system).
How can I send the request, instead using $file = "http://www.mysite.com/generate/new_image.jpg"
One solution that came to mind was loading "new_image.jpg" into memory with fopen or file_get_contents() but once I get to that point I'm not sure how to send it as POST to another site
By far the easiest solution is going to be to write the file to a temporary location, then delete it once the cURL request is complete:
// assume $img contains the image file
$filepath = 'C:\temp\tmp_image_' . rand() . '.jpg'
file_put_contents($filepath, $img);
$params = array(
'photo'=>"#$filepath",
'title'=>$title
);
// do cURL request using $params...
unlink($filepath);
Note that I am inserting a random number to avoid race conditions. If your image is not particularly big, it would be better to use md5($img) in your filename instead of rand(), which could still result in collisions.

Categories