Join Spatie pdf to image converter with yandex disk - php

I have a problem with Joining 2 functions, So I am use https://github.com/spatie/pdf-to-image - this function for convert uploaded pdf(s) to image(s). this is working fine, but this function uploads converted images to the local directory, but i want to change it and save converted images to the yandex disk not a local directory. my function with curl for upload files to yandex disk working fine too, but i dont know how to join this 2 functions,
I want to change $pdf->setPage($pageNumber)->saveImage($pageNumber . '.jpg'); to uploadtoyandexdisk("folder", "", $pageNumber . '.jpg'); for uploading to yandex disk, can you help ?
//edited code
if(!empty($_FILES["fileToUpload"])){
error_reporting(E_ALL);
ini_set("display_errors", 1);
function uploadtoyandexdisk($folder, $file, $name) {
if(empty($file)){
$filename = $name;
$filepath = $_FILES["fileToUpload"]["tmp_name"];
} else{
$filename = $name;
$filepath = $file;
}
$yandexusername = "username";
$yandexpassword = "password";
$credentials = array($yandexusername,$yandexpassword);
$filesize = filesize($filepath);
$fh = fopen($filepath, 'r');
$remoteUrl = 'https://webdav.yandex.ru/uploads/'.$folder.'/';
$ch = curl_init($remoteUrl . $filename);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, implode(':', $credentials));
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_INFILE, $fh);
curl_setopt($ch, CURLOPT_INFILESIZE, $filesize);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$response=curl_exec($ch);
fclose($fh);
echo $response;
}
include "../inc/extensions/pdftoimage/Pdf.php";
$pdf = new Spatie\PdfToImage\Pdf($_FILES["fileToUpload"]["tmp_name"]);
foreach (range(1, $pdf->getNumberOfPages()) as $pageNumber) {
$pdf->setCompressionQuality(50);
$pdf->setPage($pageNumber)
->saveImage($pageNumber . '.jpg');
$imgdata = $pdf->setPage($pageNumber)->getImageData($pageNumber . '.jpg');
uploadtoyandexdisk("uploads", $imgdata, $pageNumber . '.jpg');
}
}

An easy look at the source code helps to solve the problem: while saveImage is used to save the converted data to a file, you can call getImageData with the same argument to get the raw data. If you post that raw data to the Yandex server instead of reading data from a file, you should be done

Related

How to get correctly a file via file_get_content or curl

I tried to download a file via Icecat. The file need an access to be downloaded.
My problem is :
the file_get_content does'nt download the file. My directory is on 777 and the path is correct.
if I insert the document inside the directory, the file is not unzipped.
public function getIceCatFile() {
set_time_limit (0);
$url = 'https://data.Icecat.biz/export/freexml/EN/daily.index.xml.gz';
$context = stream_context_create(array('http' => array( 'header' => "Authorization: Basic " . base64_encode($this->username . ":" . $this->password) )));
if ($this->checkDirectoryIceCat() === true) {
// does'nt download the file inside the server directory
file_get_contents($url, true, $context);
$file = $this->IceCatDirectory . 'daily.index.xml.gz';
if (is_file($file)) {
$zip = new \ZipArchive;
// error failed same if I include the file inside the directory
$icecat_file = $zip->open($this->IceCatDirectory . 'files.index.xml.gz');
if ($icecat_file === true) {
$zip->extractTo($icecat_file);
$zip->close();
echo 'file downloaded and unzipped';
} else {
echo 'failed';
}
} else {
echo 'error no file found in ' . $file;
}
}
}
Make a try like this to download file that has username and password authentication using PHP Curl,
$localfile = fopen($file_store_locally, 'wb'); // open with write enable
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILE, $localfile);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "username:password"); // see here for auth
curl_exec($ch);
curl_close($ch);
fclose($localfile); //store file data to local file

file_put_contents is unable to get whole file

I am using hostmonster as hosting provider.
I have written below code.
$mp4_url = 'http://www.w3schools.com/html/mov_bbb.mp4';
$filename = 'sample.mp4';
file_put_contents( $filename , fopen($mp4_url, 'r' ) );
I am seeing strange behaviour. Sometimes it works but sometimes it copies only few bites like sometimes size of file is 100kb sometimes 600kb sometimes it copies whole file.
Please suggest what should I do to copy any mp4 file from any server to our server.
We have to copy large files, size can be 600MB or 1GB.
Try copy(). This will do your job:
$file = 'http://www.w3schools.com/html/mov_bbb.mp4';
$newfile = $_SERVER['DOCUMENT_ROOT'] . '/sample.mp4';
if ( copy($file, $newfile) ) {
echo "Copy success!";
}else{
echo "Copy failed.";
}
use curl rather than using file_put_contents
$url='http://www.w3schools.com/html/mov_bbb.mp4';
$saveto='path';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$raw = curl_exec($ch);
curl_close($ch);
if (file_exists($saveto)) {
unlink($saveto);
}
$fp = fopen($saveto, 'x');
fwrite($fp, $raw);
fclose($fp);

Upload file from URL

I've been trying to figure out how to upload a file from a URL using the following code without using a form. In a form I can enter in a URL instead of a local file on the computer and it uploads it, So im guessing its possible?:
example location: http://www.bubblews.com/assets/images/news/521013543_1385596410.jpg
if ((isset($_FILES['upload']['name'])) && (substr_count($_FILES['upload']['type'],'image/'))) {
$prefix = "market/".$market->guid;
$filehandler = new ElggFile();
$filehandler->owner_guid = $market->owner_guid;
$filehandler->setFilename($prefix . ".jpg");
$filehandler->open("write");
$filehandler->write(get_uploaded_file('upload'));
$filehandler->close();
Try this example:
Saving image from PHP URL
$ch = curl_init('http://example.com/image.php');
$fp = fopen('/my/folder/flower.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
or this
$url = 'http://example.com/image.php';
$img = '/my/folder/flower.gif';
file_put_contents($img, file_get_contents($url));
In linux hosts you can try
exec("wget ".$_POST['url']);

how to save facebook profile picture using php graph Api

i am using this curl class for saving the file -->
class CurlHelper
{
/**
* Downloads a file from a url and returns the temporary file path.
* #param string $url
* #return string The file path
*/
public static function downloadFile($url, $options = array())
{
if (!is_array($options))
$options = array();
$options = array_merge(array(
'connectionTimeout' => 5, // seconds
'timeout' => 10, // seconds
'sslVerifyPeer' => false,
'followLocation' => false, // if true, limit recursive redirection by
'maxRedirs' => 1, // setting value for "maxRedirs"
), $options);
// create a temporary file (we are assuming that we can write to the system's temporary directory)
$tempFileName = tempnam(sys_get_temp_dir(), '');
$fh = fopen($tempFileName, 'w');
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_FILE, $fh);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $options['connectionTimeout']);
curl_setopt($curl, CURLOPT_TIMEOUT, $options['timeout']);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $options['sslVerifyPeer']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, $options['followLocation']);
curl_setopt($curl, CURLOPT_MAXREDIRS, $options['maxRedirs']);
curl_exec($curl);
curl_close($curl);
fclose($fh);
return $tempFileName;
}
}
$url = 'http://graph.facebook.com/shashankvaishnav/picture';
$sourceFilePath = CurlHelper::downloadFile($url, array(
'followLocation' => true,
'maxRedirs' => 5,
));
this above code will give me the temporary url in $sourceFilePath variable now i want to store that image in my image folder. I am stuck here...please help me in this...thank you in advance.
There is a pretty easy option for this:
$url = 'http://graph.facebook.com/shashankvaishnav/picture';
$data = file_get_contents($url);
$fileName = 'fb_profilepic.jpg';
$file = fopen($fileName, 'w+');
fputs($file, $data);
fclose($file);
You don´t even need anything else then.
Or if file_get_contents is disabled (usually it isn´t), this should also work:
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, 'http://graph.facebook.com/shashankvaishnav/picture');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
$fileName = 'fb_profilepic.jpg';
$file = fopen($fileName, 'w+');
fputs($file, $data);
fclose($file);
Edit: This is not possible anymore, since you can´t use the username for API calls anymore. You have to use an (App Scoped) ID after authorizing a User with the Facebook API instead of the username.
$filename = 'abcd.jpg';
$to = 'image/'.$filename;
if(copy($sourceFilePath,$to))
echo 'copied';
else
echo 'error';
$profile_Image = 'http://graph.facebook.com/shashankvaishnav/picture';
$userImage = $picturtmp_name . '.jpg'; // insert $userImage in db table field.
$savepath = '/images/';
insert_user_picture($savepath, $profile_Image, $userImage);
function insert_user_picture($path, $profile_Image, $userImage) {
$thumb_image = file_get_contents($profile_Image);
$thumb_file = $path . $userImage;
file_put_contents($thumb_file, $thumb_image);
}
$fbprofileimage = file_get_contents('http://graph.facebook.com/senthilbp/picture/');
file_put_contents('senthilbp.gif', $fbprofileimage);

Getting a file from my local server and uploading it to a remote server using php

I know there are many posts about this issue however I cannot find one single answer on here that describes EXACTLY what I am trying to do and I have been searching for HOURS...so hopefully you all can help me and steer me in the right direction, the main thing I cannot figure out how to do is get the source file and pass that to my update function where I will ftp that file to the remote server...
Basically what I am trying to do is the following:
1) Get a file from my local host (ex. /home/user/public_html/file.php)
2) Upload that file to a remote server of one of my clients (I have all of their FTP information stored in my database and can connect to their server just fine)
3) If the file exists on the remote server, have it over write with the one.
Now, I have tried ftp_get to get the file but that is not what I want to do because I don't need to re-write or store the same file to the local server...
Here's what I have so far...
Any help is much appreciated and I do apologize if this is answered some where else I just didn't see it! :)
AS AN UPDATE TO THIS I JUST THOUGHT OF SOMETHING...
Why do I even have to get the local file via ftp? Shouldn't I just be able to get the local file some other way and pass that to my update function where it will upload that file to the remote server?
update.php
$updater = new updater($accountInfo['ftp_user'],$accountInfo['ftp_pass'],$accountInfo['host']);
$file = "relative/path/to/my/file";
$source = $updater->getFile($file);
$updater->update($source,$file);
my update class
class updater {
var $ftp_user = "";
var $ftp_pass = "";
var $ftp_host = "";
function __construct($user,$pass,$host) {
$this->ftp_user = $user;
$this->ftp_pass = $pass;
$this->ftp_host = $host;
}
function getFile($file) {
$curl = curl_init();
$file = fopen($file, 'w');
curl_setopt($curl, CURLOPT_URL, "ftp://domain.com/".$file); #input
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($curl, CURLOPT_FILE, $file); #output
curl_setopt($curl, CURLOPT_USERPWD, "ftpuser:ftppass");
$returnFile = curl_exec($curl);
return $returnFile;
}
function update($source,$file) {
echo("Source: ".$source."<BR>");
echo("File: ".$file."<BR>");
$connection = ftp_connect($this->ftp_host);
$login = ftp_login($connection, $this->ftp_user, $this->ftp_pass);
if (!$connection || !$login) { die('Connection attempt failed!'); }
$upload = ftp_put($connection, "/home/".$this->ftp_host."/public_html/".$file, $source, FTP_BINARY);
if (!$upload) {
echo 'FTP upload failed!';
} else {
echo("UPDATED FILE ".$source." SUCCESSFULLY!<BR>");
}
ftp_close($connection);
}
Try this:
# Declare Files
$local_file = "/home/user/public_html/file.php";
$remote_file = "public_html/remote_file.php";
# FTP
$server = "ftp://ftp.yourhost.com/".$remote_file;
# FTP Credentials
$ftp_user = "ftp_username";
$ftp_password = "password";
# Upload File
$ch = curl_init();
$ftp_file = fopen($local_file, 'r');
curl_setopt($ch, CURLOPT_URL, $server);
curl_setopt($ch, CURLOPT_USERPWD, $ftp_user.":".$ftp_password);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $ftp_file);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($local_file));
$result = curl_exec($ch);
You can easily put it in a function if this does what you want.

Categories