/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
-
- function createFrom($file)
+ /**
+ * create an email from file.
+ * these must have been set first.
+ * ontable / onid.
+ *
+ */
+ function createFrom($file, $filename=false)
{
// copy the file into the storage area..
if (!file_exists($file) || !filesize($file)) {
return false;
}
+ $filename = empty($filename) ? $file : $filename;
- $imgs = @getimagesize($file);
+ if (empty($this->mimetype)) {
+ require_once 'File/MimeType.php';
+ $y = new File_MimeType();
+ $this->mimetype = $y->fromFilename($filename);
+ }
- if (empty($imgs) || empty($imgs[0]) || empty($imgs[1])) {
- // it's a file!!!!
- } else {
- list($this->width , $this->height) = $imgs;
+ $this->mimetype= strtolower($this->mimetype);
+
+ if (array_shift(explode('/', $this->mimetype)) == 'image') {
+
+ $imgs = @getimagesize($file);
+
+ if (empty($imgs) || empty($imgs[0]) || empty($imgs[1])) {
+ // it's a file!!!!
+ } else {
+ list($this->width , $this->height) = $imgs;
+ }
}
$this->filesize = filesize($file);
$this->created = date('Y-m-d H:i:s');
+
+
+ if (empty($this->filename)) {
+ $this->filename = basename($filename);
+ }
+
//DB_DataObject::debugLevel(1);
if (!$this->id) {
$this->insert();
*/
function getStoreName()
{
- $opts = PEAR::getStaticProperty('Pman', 'options');
+ $opts = HTML_FlexyFramework::get()->Pman;
$fn = preg_replace('/[^a-z0-9\.]+/i', '_', $this->filename);
return implode( '/', array(
$opts['storedir'], '_images_', date('Y/m', strtotime($this->created)), $this->id . '-'. $fn
if (file_exists($fn)) {
unlink($fn);
}
-
+ // delete thumbs..
$b = basename($fn);
$d = dirname($fn);
- $dh = opendir($d);
- while (false !== ($fn = readdir($dh))) {
- if (substr($fn, 0, strlen($b)) == $b) {
- unlink($d. '/'. $fn);
+ if (file_exists($d)) {
+
+ $dh = opendir($d);
+ while (false !== ($fn = readdir($dh))) {
+ if (substr($fn, 0, strlen($b)) == $b) {
+ unlink($d. '/'. $fn);
+ }
}
}
-
}
-
+ /**
+ * check mimetype against type
+ * - eg. img.is(#image#)
+ *
+ */
+ function is($type)
+ {
+ if (empty($this->mimetype)) {
+ return false;
+ }
+ return 0 === strcasecmp($type, array_shift(explode('/',$this->mimetype)));
+ }
/**
* onUpload (singlely attached image to a table)
/**
* return a list of images for an object, optionally with a mime regex.
* eg. '%/pdf' or 'image/%'
+ *
+ * usage:
+ *
+ * $i = DB_DataObject::factory('Images');
+ * $i->imgtype = 'LOGO';
+ * $ar = $i->gather($somedataobject, 'image/%');
+ *
+ * @param {DB_DataObject} dataobject = the object to gather data on.
+ * @param {String} mimelike LIKE query to use for search
+
*/
- function gather($obj, $mime_like='')
+ function gather($obj, $mime_like='', $opts=array())
{
+ //DB_DataObject::debugLevel(1);
if (empty($obj->id)) {
return array();
}
+
$c = clone($this);
- $c->on_table = $obj->tableName();
- $c->on_id = $obj->id;
- if (!empty($mime_regex)) {
- $c->whereAdd("mimetype LIKE '". $c->escape($mime_like) ."'");
+ $c->ontable = $obj->tableName();
+ $c->onid = $obj->id;
+ $c->autoJoin();
+ if (!empty($mime_like)) {
+ $c->whereAdd("Images.mimetype LIKE '". $c->escape($mime_like) ."'");
}
return $c->fetchAll();
}
+
+
+ /**
+ * set or get the dataobject this image is associated with
+ * @param DB_DataObject $obj An object to associate this image with
+ * (does not store it - you need to call update() to do that)
+ * @return DB_DataObject the dataobject this image is attached to.
+ */
+ function object($obj=false)
+ {
+ if ($obj === false) {
+ $ret = DB_DataObject::factory($this->ontable);
+ $ret->get($this->onid);
+ return $ret;
+ }
+ $this->ontable = $obj->tableName();
+ $this->onid = $obj->id; /// assumes our nice standard of using ids..
+ return $obj;
+ }
function toRooArray($req = array()) {
- if (empty($req['query']['imagesize'])) {
- return $this->toArray();
- }
+ // echo '<PRE>';print_r($req);exit;
$ret= $this->toArray();
- $ret['url'] = $this->URL($req['query']['imagesize']);
-
+
+
+ if (!empty($req['query']['imagesize'])) {
+ $baseURL = isset($req['query']['imageBaseURL']) ? $req['query']['imageBaseURL'] : false;
+
+ $ret['url'] = $this->URL(-1, '/Images/Download',$baseURL);
+
+ $ret['url_view'] = $this->URL(-1, '/Images',$baseURL);
+
+ if (!empty($req['query']['imagesize'])) {
+ $ret['url_thumb'] = $this->URL($req['query']['imagesize'], '/Images/Thumb',$baseURL);
+ }
+ }
+
+
return $ret;
}
-
- function URL($size)
+
+ /**
+ * URL - create a url for the image.
+ * size - use -1 to show full size.
+ * provier = baseURL + /Images/Thumb ... use '/Images/' for full
+ *
+ *
+ */
+ function URL($size , $provider = '/Images/Thumb', $baseURL=false)
{
- $size = max(100, (int) $size);
- $size = min(1024, (int) $size);
-
+ if (!$this->id) {
+ return 'about:blank';
+
+ }
+
$ff = HTML_FlexyFramework::get();
- return $ff->baseURL . "/Images/Thumb/$size/{$this->id}/{$this->filename}";
+ $baseURL = $baseURL ? $baseURL : $ff->baseURL ;
+ if ($size < 0) {
+ return $baseURL . $provider . "/{$this->id}/{$this->filename}";
+ }
+ //-- max?
+ //$size = max(100, (int) $size);
+ //$size = min(1024, (int) $size);
+
+
+ return $baseURL . $provider . "/$size/{$this->id}/{$this->filename}";
}
+ /**
+ * size could be 123x345
+ *
+ *
+ */
+ function toHTML($size, $provider = '/Images/Thumb')
+ {
+
+
+
+ $sz = explode('x', $size);
+ $sx = $sz[0];
+ //var_dump($sz);
+ if (!$this->id || empty($this->width)) {
+ $this->height = $sx;
+ $this->width = empty($sz[1]) ? $sx : $sz[1];
+ $sy = $this->width ;
+ }
+ if (empty($sz[1])) {
+ $ratio = empty($this->width) ? 1 : $this->height/ ($this->width *1.0);
+ $sy = $ratio * $sx;
+ } else {
+ $sy = $sz[1];
+ }
+ // create it?
+
+
+ return '<img src="' . $this->URL($size, $provider) . '" width="'. $sx . '" height="'. $sy . '">';
+
+
+ }
+
+
+
+
function setFromRoo($ar, $roo)
{
// not sure why we do this..
}
-
+ if (!empty($ar['_copy_from'])) {
+ $copy = DB_DataObject::factory('Images');
+ $copy->get($ar['_copy_from']);
+ $this->setFrom($copy->toArray());
+ $this->setFrom($ar);
+ $this->createFrom($copy->getStoreName());
+
+ $roo->addEvent("ADD", $this, $this->toEventString());
+
+ $r = DB_DataObject::factory($this->tableName());
+ $r->id = $this->id;
+ $roo->loadMap($r);
+ $r->limit(1);
+ $r->find(true);
+ $roo->jok($r->toArray());
+
+
+ }