sasezakiの日記

2007-08-28

ドラッグ&ドロップでtumblrにphotoをwrite 00:32

test.bat

php "c:\tumblrPhotoWrite.php" -f %1
pause

tumblrPhotoWrite.php

<?php
require_once 'Pel/PelJpeg.php';
require_once 'Zend/Console/Getopt.php';
require_once 'Zend/Debug.php';
require_once 'Diggin/Service/Tumblr.php';

$email = '';
$password = '';

$opt =  new Zend_Console_Getopt('f:');
$file = $opt->f;

$input_jpeg = new PelJpeg($file);
$original = imagecreatefromstring($input_jpeg->getBytes());
$original_w = ImagesX($original);
$original_h = ImagesY($original);

$tumblrWidthMax = 500;
if($original_w > $tumblrWidthMax) {
    $scale = $tumblrWidthMax / $original_w;
}

Zend_Debug::dump($scale);

$scaled_w = $original_w * $scale;
$scaled_h = $original_h * $scale;

$scaled = ImageCreateTrueColor($scaled_w, $scaled_h);
ImageCopyResampled($scaled, $original,
                   0, 0, /* dst (x,y) */
                   0, 0, /* src (x,y) */
                   $scaled_w, $scaled_h,
                   $original_w, $original_h);

$output_jpeg = new PelJpeg($scaled);

$exif = $input_jpeg->getExif();

if ($exif != null) {
  $output_jpeg->setExif($exif);
  
  $tiff = $exif->getTiff();
  $ifd0 = $tiff->getIfd();
  $_exif = $ifd0->getSubIfd(PelIfd::EXIF);
  $data['date_time_original'] = $_exif->getEntry(PelTag::DATE_TIME_ORIGINAL)->getText();
  $data['model'] = $ifd0->getEntry(PelTag::MODEL)->getText();
}

Zend_Debug::dump($data);
$tumblr = new Diggin_Service_Tumblr();
$tumblr->setAuth($email, $password);
if(isset($data)){
    $catpion = "This Photo is taken at ".$data['date_time_original']."<br /> with ".$data['model'];
} else {
    $caption = null;
}
$tumblr->writePhoto('data', $output_jpeg->getBytes(), $caption);

echo "tumblrにpostしました";