I have developed a facebook application that contains following files
1) index.php
2) cap3.php
where cap3.php generates an image using GD in PHP.
index.php displays this image using following code
<img src="cap3.php">
now I want this generated image to be posted on user's timeline.
I have tried to do so by using following code:
$facebook->setFileUploadSupport(true);
$img = 'cap3.php';
$args=array( 'source' => '#' .$current , 'message' => 'Photo uploaded via!' );
$photo = $facebook->api('/me/photos', 'POST', $args);
but the image is not posted on user's timeline
please help.
One way to do that is to put your cap3.php file in your remote host to get an URL to it. For example : http://www.example.com/cap3.php. Then, you download that image from your app and send it to facebook.
Let's see an example:
$local_img = 'cap3.png'; // I assume your cap3.php generates a PNG file
$remote_img = 'http://www.example.com/cap3.php';
// sample: check for errors in your production app!
$img_content = file_get_contents($remote_img);
file_put_contents($locale_img, $img_content);
$facebook->setFileUploadSupport(true);
$args=array( 'source' => '#' .$locale_img , 'message' => 'Photo uploaded via!' );
$photo = $facebook->api('/me/photos', 'POST', $args);
Related
I developed an API in PHP, hosted at Google App Engine, to use with my Android app.
Basically, when a user wants to change his profile picture, the android app send a request to the server containing the user id, the session key and the picture to upload. I want to upload this picture on the Google Cloud, but since Google App Engine require you to build a public upload URL to upload a file, how can I generate the upload URL and then use this URL to upload the profile picture in my Google Cloud in one request?
I tried to use a basic PHP redirect after generating the upload URL but I get a HTTP 405.
I am really stuck and I honestly don't understand why you NEED to create an URL before uploading a file to Google App Engine...
Source: https://cloud.google.com/appengine/docs/php/googlestorage/user_upload
It seems that the photo that you want to upload on Google Cloud Storage saves as a Blob on GCS that's why they ask you to create a URL. Follow the document [1] it is related to Blobstore API in python but the concept of blob is same for any language so its good to read about blobs. Also find below a sample code which might help you to upload a photo on GCS.
$options = [ 'gs_bucket_name' => 'bucket-name' ];
$upload_url = CloudStorageTools::createUploadUrl('/upload', $options);
<form action="<?php echo $upload_url?>" method="post" enctype="multipart/form-data">
<input type ="file" name="fileupload" id = "fileupload">
<input type="submit" value="Upload" name ="submit">
</form>
Upload Handler:
<?php
use google\appengine\api\cloud_storage\CloudStorageTools;
$gs_name = $_FILES['fileupload'];
$buffer = file_get_contents($gs_name['tmp_name']);
$bucket = CloudStorageTools::getDefaultGoogleStorageBucketName();
$user_pic_url = 'gs://' . $bucket
. (substr($final_file_path,0,1) != '/' ? '/' : '' )
. $final_file_path;
// set file options on Google Could Storage
$options = stream_context_create( ['gs' => ['acl' => 'public-read',
'Content-Type' => 'image/jpg']] );
echo "<br>";
$my_file = fopen($user_pic_url, 'w', false, $options);
fclose($my_file);
[1] BlobStore API: https://cloud.google.com/appengine/docs/python/blobstore/
$pdf->Output('example_025.pdf', 'I');
$DynamicNameofPic = rand(1000,10000)."_Invoice.pdf";
$FileNameDynamic = "gs://#######/".$DynamicNameofPic;
$pdf->Output($FileNameDynamic,'F');
$image_dataURL = "https://storage.googleapis.com/##########/".$DynamicNameofPic;
$image_data = file_get_contents($image_dataURL);$fileName = $DynamicNameofPic;
I am trying to tweet an image using php. I have read the documentation and followed a some tutorials. I don't have any problem when sending a message; however, it does not work with images. I can not find where my mistake is, can anyone help?? I would deeply appreciate it.
<?php
$comments = $_POST['comment'];
if (isset($_POST['Twitter']))
{
require_once('twitter/codebird-php/src/codebird.php');
\Codebird\Codebird::setConsumerKey("xxxxxx","xxxxxx");
$cb = \Codebird\Codebird::getInstance();
$cb->setToken("xxxxxx", "xxxxxxxxxx");
$params = array(
'status' => $comments,
'media[]' => "/images/image1.jpg"
);
$reply = $cb->statuses_update($params);
echo "You have posted your message succesfully";
}
else{
echo "You havent posted anythingh";
}
?>
You need to supply Twitter the fully qualified URI to your image.
You have the following code:
$params = array(
'status' => $comments,
'media[]' => "/images/image1.jpg"
);
Unfortunately, that's not a complete URL to an image, it's a relative URL. Instead, you need to provide something along the lines of this:
$params = array(
'status' => $comments,
'media[]' => "http://www.example.com/images/image1.jpg"
);
In addition, according to the Twitter API v1 documentation, you need to use statuses/update_with_media instead of statuses/update.
If you are using Twitter API v1, Twitter also recommends using v1.1 instead.
I am trying to save images from image url to the amazon s3, but image is created there in bucket, but image is not shown in browser, displays message "image cannot be displayed because it contains error.
This is my code:
require_once("aws/aws-autoloader.php");
// Amazon S3
use Aws\S3\S3Client;
// Create an Amazon S3 client object
$s3Client = S3Client::factory(array(
'key' => 'XXXXXXXXXXXX',
'secret' => 'XXXXXXXXX'
));
// Register the stream wrapper from a client object
$s3Client->registerStreamWrapper();
// Save Thumbnail
$s3Path = "s3://smmrescueimages/";
$s3Stream = fopen($s3Path . 'gt.jpg', 'w');
fwrite($s3Stream, 'http://sippy.in/gt.jpg');
#fclose($s3Stream);
echo "done";
This is the image path generated https://s3.amazonaws.com/smmrescueimages/gt.jpg
change this line
fwrite($s3Stream, 'http://sippy.in/gt.jpg');
to
fwrite($s3Stream, file_get_contents('http://sippy.in/gt.jpg'));
otherwise you save the url string instant of the image binary into your file.
dont use # to prevent error messages of php functions!
just check if a valid handler is present
$s3Stream = fopen($s3Path . 'gt.jpg', 'w');
if( $s3Stream ) {
fwrite($s3Stream, file_get_contents('http://sippy.in/gt.jpg'));
fclose($s3Stream);
}
In our application a user is allowed to upload an image of dimension 1024 X 768(around 150 KB).
When the user upload an image following things happen :
1)Image uploaded on temporary directory
2)Crop the Image into four different sizes.
3)Upload the original image and its cropped images on amazon s3 server.
The above process prove to be time consuming for the user.
After profiling with xdebug it seems that 90% of the time is being consumed by uploading image on amazon s3.
I am using given below method to save image in amazon s3 bucket
public function saveInBucket( $sourceLoc,$bucketName = '', $destinationLoc = '' ) {
if( $bucketName <> '' && $destinationLoc <> '' && $sourceLoc <> '') {
$s3 = new AmazonS3();
$response = $s3->create_object( $bucketName.'.xyz.com',$destinationLoc, array(
'contentType' => 'application/force-download',
'acl' => AmazonS3::ACL_PUBLIC,
'fileUpload' => $sourceLoc
)
);
if ( ( int ) $response->isOK() ) {
return TRUE;
}
$this->ErrorMessage = 'File upload operation failed,Please try again later';
return FALSE;
}
return FALSE;
}
I also thought of uploading image directly to amazon s3 but i can not do that since i also have to crop image into 4 different sizes
How can i speed up or improve image management process.
This happened to me before. What you can do is:
When you resize your image, you have to convert to string.
I was using WideImage class.
Example:
$image = WideImage::load($_FILES["file"]['tmp_name']);
$resized = $image->resize(1024);
$data = $resized->asString('jpg');
And then when you're uploading on Amazon, you have to use the param 'body' instead of 'fileUpload'.
Example:
$response = $s3->create_object( $bucketName.'.xyz.com',$destinationLoc, array(
'contentType' => 'application/force-download',
'acl' => AmazonS3::ACL_PUBLIC,
'body' => $data
)
);
I hope that helps.
i want to upload image to facebook album using facebook api, i search alot about it if find out that the file used is from the pc..
i want to upload image to facebook album and the image path i want to get from a website ?
$File_path='http://example.com/sample.jpg';
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/'. $ALBUM_ID . '/photos', 'post', $args);
print_r($data);
is that possible to give a file path like i mention above?
You have couple of severe mistakes in your code:
$File_path isn't the same as $FILE_PATH - remember variables are case sensitive!
realpath accepts path not URL and will return false in your case
You can only upload local files using image parameter, and for remote images url parameter should be used.
Consider using one of the next samples to achieve the desired result, for local file:
$facebook->api("/{$ALBUM_ID}/photos", "post", array(
'message' => 'Photo caption',
'image' => '#'.realpath('./path/to/local/image/file.jpg')
));
And for remote file:
$facebook->api("/{$ALBUM_ID}/photos", "post", array(
'message' => 'Photo caption',
'url' => 'http://example.com/url/of/image/file.jpg'
));