From 43454eba363997d44815a90264ca072f1d014f21 Mon Sep 17 00:00:00 2001 From: Ian Denhardt Date: Thu, 5 Aug 2010 13:11:34 -0400 Subject: [PATCH] got uploads sortof working - database portion doesn't work though. --- .../GNUsocialPhotos/GNUsocialPhotosPlugin.php | 5 +- plugins/GNUsocialPhotos/actions/photos.php | 61 +------------ .../GNUsocialPhotos/actions/photoupload.php | 7 +- .../classes/gnusocialphoto.php | 2 +- plugins/GNUsocialPhotos/lib/photolib.php | 87 +++++++++++++++++++ 5 files changed, 98 insertions(+), 64 deletions(-) create mode 100644 plugins/GNUsocialPhotos/lib/photolib.php diff --git a/plugins/GNUsocialPhotos/GNUsocialPhotosPlugin.php b/plugins/GNUsocialPhotos/GNUsocialPhotosPlugin.php index d93bb8cca8..f7cb6f3982 100644 --- a/plugins/GNUsocialPhotos/GNUsocialPhotosPlugin.php +++ b/plugins/GNUsocialPhotos/GNUsocialPhotosPlugin.php @@ -42,9 +42,12 @@ class GNUsocialPhotosPlugin extends Plugin switch ($cls) { case 'PhotosAction': + include_once $dir . '/lib/photolib.php'; include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php'; return false; case 'PhotouploadAction': + include_once $dir . '/lib/photolib.php'; + include_once $dir . '/classes/gnusocialphoto.php'; include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php'; return false; default: @@ -57,7 +60,7 @@ class GNUsocialPhotosPlugin extends Plugin function onCheckSchema() { $schema = Schema::get(); - $schema->ensureTable('GNUsocialPhotos', + $schema->ensureTable('GNUsocialPhoto', array(new ColumnDef('object_id', 'integer', null, false, 'PRI', true, null, null, true), new ColumnDef('path', 'varchar(150)', null, false), new ColumnDef('thumb_path', 'varchar(156)', null, false), // 156 = 150 + strlen('thumb.') diff --git a/plugins/GNUsocialPhotos/actions/photos.php b/plugins/GNUsocialPhotos/actions/photos.php index aef2478b17..29a70e4589 100644 --- a/plugins/GNUsocialPhotos/actions/photos.php +++ b/plugins/GNUsocialPhotos/actions/photos.php @@ -97,7 +97,7 @@ class PhotosAction extends Action $this->elementStart('li'); $this->elementStart('a', array('href' => 'http://' . common_config('site', 'server') . '/file/' . $file)); if (!file_exists(INSTALLDIR . '/file/thumb.' . $file)) { - $this->makeThumb($file); + photo_make_thumbnail($file); } $this->element('img', array('src' => 'http://' . common_config('site', 'server') . '/file/' . 'thumb.' . $file)); $this->elementEnd('a'); @@ -107,63 +107,4 @@ class PhotosAction extends Action $this->elementEnd('ul'); } } - - function makeThumb($filename) - { - $height_dest = 192; - $width_dest = 256; - - $size_src = getimagesize(INSTALLDIR . '/file/' . $filename); - $image_type = $size_src[2]; - - switch($image_type) { - case IMAGETYPE_JPEG: - $image_src = imagecreatefromjpeg(INSTALLDIR . '/file/' . $filename); - break; - case IMAGETYPE_PNG: - $image_src = imagecreatefrompng(INSTALLDIR . '/file/' . $filename); - break; - case IMAGETYPE_GIF: - $image_src = imagecreatefromgif(INSTALLDIR . '/file/' . $filename); - break; - default: - return false; - } - - $width_src = $size_src[0]; - $height_src = $size_src[1]; - - $ratio_src = (float) $width_src / (float) $height_src; - $ratio_dest = (float) $width_dest / (float) $height_dest; - - if ($ratio_src > $ratio_dest) { - $height_crop = $height_src; - $width_crop = (int)($height_crop * $ratio_dest); - $x_crop = ($width_src - $width_crop) / 2; - } else { - $width_crop = $width_src; - $height_crop = (int)($width_crop / $ratio_dest); - $x_crop = 0; - } - - $image_dest = imagecreatetruecolor($width_dest, $height_dest); - - imagecopyresampled($image_dest, $image_src, 0, 0, $x_crop, 0, $width_dest, $height_dest, $width_crop, $height_crop); - switch ($image_type) { - case IMAGETYPE_JPEG: - imagejpeg($image_dest, INSTALLDIR . '/file/' . 'thumb.' . $filename, 100); - break; - case IMAGETYPE_PNG: - imagepng($image_dest, INSTALLDIR . '/file/thumb.' . $filename); - break; - case IMAGETYPE_GIF: - imagegif($image_dest, INSTALLDIR . '/file/thumb.' . $filename); - break; - } - - imagedestroy($image_src); - imagedestroy($image_dest); - - return true; - } } diff --git a/plugins/GNUsocialPhotos/actions/photoupload.php b/plugins/GNUsocialPhotos/actions/photoupload.php index 286dd4386d..aafa2d3fef 100644 --- a/plugins/GNUsocialPhotos/actions/photoupload.php +++ b/plugins/GNUsocialPhotos/actions/photoupload.php @@ -44,6 +44,9 @@ class PhotouploadAction extends Action function handle($args) { parent::handle($args); + if($_SERVER['REQUEST_METHOD'] == 'POST') { + $this->handlePost(); + } $this->showPage(); } @@ -88,12 +91,12 @@ class PhotouploadAction extends Action // CSRF protection - $token = $this->trimmed('token'); +/* $token = $this->trimmed('token'); if (!$token || $token != common_session_token()) { $this->showForm(_('There was a problem with your session token. '. 'Try again, please.')); return; - } + } */ if($this->arg('upload')) { $this->uploadPhoto(); diff --git a/plugins/GNUsocialPhotos/classes/gnusocialphoto.php b/plugins/GNUsocialPhotos/classes/gnusocialphoto.php index ecb90616e1..0e3d24eedc 100644 --- a/plugins/GNUsocialPhotos/classes/gnusocialphoto.php +++ b/plugins/GNUsocialPhotos/classes/gnusocialphoto.php @@ -32,7 +32,7 @@ if (!defined('STATUSNET')) { require_once INSTALLDIR . '/classes/Memcached_DataObject.php'; -class GNUsocialPhoto extends Memcahced_DataObject +class GNUsocialPhoto extends Memcached_DataObject { public $object_id; // integer public $path; // varchar(150) diff --git a/plugins/GNUsocialPhotos/lib/photolib.php b/plugins/GNUsocialPhotos/lib/photolib.php new file mode 100644 index 0000000000..9e5ff61fab --- /dev/null +++ b/plugins/GNUsocialPhotos/lib/photolib.php @@ -0,0 +1,87 @@ +. + * + * @category Widget + * @package GNU Social + * @author Ian Denhardt + * @copyright 2010 Free Software Foundation, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 + */ + + +function photo_make_thumbnail($filename) +{ + $height_dest = 192; + $width_dest = 256; + + $size_src = getimagesize(INSTALLDIR . '/file/' . $filename); + $image_type = $size_src[2]; + + switch($image_type) { + case IMAGETYPE_JPEG: + $image_src = imagecreatefromjpeg(INSTALLDIR . '/file/' . $filename); + break; + case IMAGETYPE_PNG: + $image_src = imagecreatefrompng(INSTALLDIR . '/file/' . $filename); + break; + case IMAGETYPE_GIF: + $image_src = imagecreatefromgif(INSTALLDIR . '/file/' . $filename); + break; + default: + return false; + } + + $width_src = $size_src[0]; + $height_src = $size_src[1]; + + $ratio_src = (float) $width_src / (float) $height_src; + $ratio_dest = (float) $width_dest / (float) $height_dest; + + if ($ratio_src > $ratio_dest) { + $height_crop = $height_src; + $width_crop = (int)($height_crop * $ratio_dest); + $x_crop = ($width_src - $width_crop) / 2; + } else { + $width_crop = $width_src; + $height_crop = (int)($width_crop / $ratio_dest); + $x_crop = 0; + } + + $image_dest = imagecreatetruecolor($width_dest, $height_dest); + + imagecopyresampled($image_dest, $image_src, 0, 0, $x_crop, 0, $width_dest, $height_dest, $width_crop, $height_crop); + switch ($image_type) { + case IMAGETYPE_JPEG: + imagejpeg($image_dest, INSTALLDIR . '/file/' . 'thumb.' . $filename, 100); + break; + case IMAGETYPE_PNG: + imagepng($image_dest, INSTALLDIR . '/file/thumb.' . $filename); + break; + case IMAGETYPE_GIF: + imagegif($image_dest, INSTALLDIR . '/file/thumb.' . $filename); + break; + } + + imagedestroy($image_src); + imagedestroy($image_dest); + + return true; +} -- 2.39.5