Post Photoset using Tumblr API PHP - php

I am trying to upload multiple photos into a photoset post to Tumblr. I am using PHP to connect to Tumblr and authenticate. I can post individual photos and video but can't seem to figure out photosets. There are other posts on Stack that help other languages except for PHP.
I have a directory of photos. I get the contents of those file and put them into an array using scandir. But when I try to post that array to Tumblr, it doesn't work.
$scanned_directory = array_diff(scandir($directory), array('..', '.'));
$connection->post("blog/$hostname/post", array('type' => 'photo',
'state' => $dest, 'link' => $sourceurl, 'source' => $sourceurl,
'caption' => $caption, 'tags' => $tags , 'data' =>
$scanned_directory));
What am I doing wrong?
Thanks for the help!

I think you should iterate the $scanned_directory variable and make sure you don't submit the just the path and filename to the Tumblr API, but the actual contents of your image file with file_get_contents(), a working example would look probably something like this:
foreach($scanned_directory as $filename){
$data[] = file_get_contents($directory.'/'.$filename);
}
$connection->post("blog/$hostname/post", array('type' => 'photo', 'state' => $dest, 'link' => $sourceurl, 'source' => $sourceurl, 'caption' => $caption, 'tags' => $tags , 'data' => $data));
That should likely do the trick, just make sure the $directory variable is actually an absolute path to your image directory (e.g. '/var/www/myhost.tld/public/images/photoset')

Related

Wordpress upload media in a specific folder for a special post

I am beginner in wordpress. I have to upload a file in the subfolder of root(http://www.domain.com/myfoldername) for a special post. It will work only for a special post, not for all. No need to change my default directory and I wan't not use any plugin.
Can anyone help me to do this? I already searched, but didn't get any solution.
Thanks
Through WordPress you can only upload files in the default upload directory. If you don't want to use any plugin, you cannot upload file in a different directory. You can upload file anywhere you want through FTP and then just put the link of the file in the post.
Try this code for your specific page.
add_filter('upload_dir', 'upload_image_specific_calback');
function upload_image_specific_calback( $param ){
//$_GET['post'] which is your target post like 10 is post id.
//After click update button.
if(isset($_GET['post'])){
if($_GET['post'] == 71){
$param = array(
'path' => get_home_path().'myfoldername',
'url' => home_url().'/myfoldername',
'subdir' => '',
'basedir' => get_home_path(),
'baseurl' => home_url(),
'error' => false
);
}
}
//$_POST['post_id'] which is your target post like 10 is post id.
//instant upload time before save
if(isset($_POST['post_id'])){
if($_POST['post_id'] == 71) {
$param = array(
'path' => get_home_path().'myfoldername',
'url' => home_url().'/myfoldername',
'subdir' => '',
'basedir' => get_home_path(),
'baseurl' => home_url(),
'error' => false
);
}
}
error_log("path={$param['path']}");
error_log("url={$param['url']}");
error_log("subdir={$param['subdir']}");
error_log("basedir={$param['basedir']}");
error_log("baseurl={$param['baseurl']}");
error_log("error={$param['error']}");
return $param;
}

How to send image using Mandrill but image name should not rename?

Image file attachments in mandrill api working fine. But here,
'type' => 'image/jpeg',
'name' => "", // Here I didn't know what I am use
'content' => base64_encode(file_get_contents($img))
You can do something like
'name' => basename($img)
if you do not want to rename the image.
Hope this helps,

AWS Elastic Transcoder watermarks

I'm trying to add a watermark to all my videos being processed through Amazon elastic transcoder but I can't get it to working.
I created the preset with one watermark set(BottomRight) as in docs.
Here's my PHP source
$output = array(
'Key' => $folderNameOut,
'PresetId' => '139450346',
'ThumbnailPattern' => 'thumbs/filename-{count}',
'Watermarks' => array(
'InputKey' => 'watermark/watermark.png',
'PresetWatermarkId' => 'BottomRight'
));
I tried putting the watermark image in both thumbs and video buckets, even HTTP URL but doesn't work.
Can someone help me with this?
There is a missing array inside the watermarks array
$output = array(
'Key' => $folderNameOut,
'PresetId' => '139450346',
'ThumbnailPattern' => 'thumbs/filename-{count}',
'Watermarks' => array(
array(
'InputKey' => 'watermark/watermark.png',
'PresetWatermarkId' => 'BottomRight'
)
));

cakephp upload plugin - define what directory to save files in

I'm using cakephp-upload plugin of jose gonzalez to upload images to our app. By default, they're being saved in a directory like this: webroot/files/user/photo/{user_id} which is fine, except for when we want to display such images using $this->Html->image() which searches for images in the webroot/img directory.
I have already tried to display the images with
echo $this->Html->image('../files/user/photo/' .
$user['User']['photo_dir'] . '/' .
$user['User']['photo']);
which works but I was wondering if there's some way to tell this plugin to save into the img directory? The documentation doesn't mention any of that.
And also, is there any way to tell the $this->Form->input('User.photo', array('type' => 'file')); to accept only image files?
as you can see in this file, path is set like:
public $defaults = array(
'rootDir' => null,
'pathMethod' => 'primaryKey',
'path' => '{ROOT}webroot{DS}files{DS}{model}{DS}{field}{DS}',
...
you could change it to make:
'path' => '{ROOT}webroot{DS}img{DS}'
and for your second question, you could use accept attribute, like:
$this->Form->input('User.photo',
array(
'type' => 'file',
'options' => array('accept' => 'image/*')
)
);

Mandrill email attachments file path

I am trying to add some attachments to an email that is being sent using the mandrill api via a php wrapper. I have tried a number of different things to try to successfully attach the file but to no avail.
I am using cakephp 2.x but I don't think that has any particular significance in this instance (maybe it does?!).
I am using the php wrapper maintained by mandrill at https://bitbucket.org/mailchimp/mandrill-api-php
Here is the code:
$mandrill = new Mandrill(Configure::read('Site.mandrill_key'));
$params = array(
'html' => '
<p>Hi '.$user['User']['name'].',</p>
<p>tIt is that time of the year again.<br />
Please login to the website members area and upload your renewal requirements.</p>
<p>Kind regards.</p>',
"text" => null,
"from_email" => Configure::read('Site.email'),
"from_name" => Configure::read('Site.title'),
"subject" => "Renewal Pending",
"to" => array(array('email' => $user['User']['email'])),
"track_opens" => true,
"track_clicks" => true,
"auto_text" => true,
"attachments" => array(
array(
'path' => WWW_ROOT.'files/downloads/renewals',
'type' => "application/pdf",
'name' => 'document.pdf',
)
)
);
$mandrill->messages->send($params, true);
}
This shows that an attachment has been added to the email and is a pdf but the actual pdf has not been attached.
I also tried by adding the path directly onto the file as in:
"attachments" => array(
array(
'type' => "application/pdf",
'name' => WWW_ROOT.'files/downloads/renewals/document.pdf',
)
I have googled and read every article I can find but cannot find any specific reference as to how I should specify the path for mandrill to correctly attach my attachment.
Any help will be greatly appreciated.
OK. So thanks to Kaitlin for her input.
The PHP way to deal with this is to get the file and then base64_encode it:
$attachment = file_get_contents(WWW_ROOT.'files/downloads/file.pdf');
$attachment_encoded = base64_encode($attachment);
and then in the attachments part of the mandrill array you pass the :
"attachments" => array(
array(
'content' => $attachment_encoded,
'type' => "application/pdf",
'name' => 'file.pdf',
)
So easy! Thanks again Kaitlin!
It looks like you're passing a parameter called path, but the Mandrill API doesn't take the path of a file for attachments. If you're using the send or send-template call, attachments should be an associative array (hash) with three keys: type, name, and content.
The content parameter should be the contents of the file as a Base64 encoded string, so instead of path, you'll want to get the file contents, Base64 encode them, and then pass them in a parameter called content instead of path.
You can see the full details of the parameters, including for attachments, in the Mandrill API docs here: https://mandrillapp.com/api/docs/messages.html#method=send

Categories