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'
));
Related
Right now I'm working on allowing user image uploads to my site using the Google Cloud Storage. Uploading regular image files such as jpg, png, gif, and webp works fine. However, SVG images do not work. They get uploaded ok but when I have the PHP code echo the URL as an image source, all browsers just display the missing image icon. However, it does appear as if the image is downloading in the network tab of the code inspector. Not only that, pasting the link into it's own tab causes the file to download. This makes me think that the server is telling the browser to download the file rather than serve it as an image. Here is the code that I am using:
include 'GDS/GDS.php';
//create datastore
$obj_store = new GDS\Store('HomeImages');
$bucket = CloudStorageTools::getDefaultGoogleStorageBucketName();
$root_path = 'gs://' . $bucket . '/' . $_SERVER["REQUEST_ID_HASH"] . '/';
$public_urls = [];
//loop through all files that are images
foreach($_FILES['images']['name'] as $idx => $name) {
if ($_FILES['images']['type'][$idx] === 'image/jpeg' || $_FILES['images']['type'][$idx] === 'image/png' || $_FILES['images']['type'][$idx] === 'image/gif' || $_FILES['images']['type'][$idx] === 'image/webp' || $_FILES['images']['type'][$idx] === 'image/svg+xml') {
//path where the file should be moved to
$original = $root_path . 'original/' . $name;
//move the file
move_uploaded_file($_FILES['images']['tmp_name'][$idx], $original);
//don't use the getImageServingUrl function on SVG files because they aren't really images
if($_FILES['images']['type'][$idx] === 'image/svg+xml')
$public_urls[] = [
'name' => $name,
'original' => CloudStorageTools::getPublicUrl($original, true),
'thumb' => CloudStorageTools::getPublicUrl($original, true),
'location' => $original
];
else
$public_urls[] = [
'name' => $name,
'original' => CloudStorageTools::getImageServingUrl($original, ['size' => 1263, 'secure_url' => true]),
'thumb' => CloudStorageTools::getImageServingUrl($original, ['size' => 150, 'secure_url' => true]),
'location' => $original
];
}
}
//store image location and name in the datastore
foreach($public_urls as $urls){
$image = new GDS\Entity();
$image->URL = $urls['original'];
$image->thumbURL = $urls['thumb'];
$image->name = $urls['name'];
$image->location = $urls['location'];
$obj_store->upsert($image);
}
//redirect back to the admin page
header('Location: /admin/homeimages');
Having run into this issue just now, I found a solution. It turns out that every file in a bucket has metadata attached and stored as key-value pairs. The key we're after is 'Content-Type', and the value isn't always correct for SVG. the value needs to be 'image/svg+xml'. I don't know how to set that programmatically, but if you only have a few objects, then it's easy to do in the file's ellipses menu in the online interface for the bucket.
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.
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 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);
I am use following methods to upload photo :
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($file);
$data = $facebook->api('me/photos', 'post', $args);
print_r($data);
After success to upload photo, $data return "Array ( [id] => 1.16874205038E+14 )".
How can i access the photo on my apps?
echo sprintf('%s', $data['id']);
With sprintf you can ask php to treat long number as a string and after that you can work with that string as you wish.
$photo = $facebook->api(sprintf('%s', $data['id']));
This will return an array of your photo details. http://developers.facebook.com/docs/reference/api/photo
PS: i'm not sure if you need an implicit casting to string in this particular case, so you can try to get the $photo with or without sprintf.