Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

base-image.inc.php

Go to the documentation of this file.
00001 <?
00012 abstract class BaseImage extends MyObject
00013 {
00017     protected static $dataDir = '';
00018     
00022     protected static $origDir = '';
00023 
00027     protected static $webDir = '';
00028 
00032     protected $sizeAr = array();
00033     
00037     protected $fullPath = '';
00038     
00042     protected $origPath = '';
00043 
00047     public $doResize = true;
00048     
00058     function __construct($data = null, $table = 'images', $dataDir = null, $webDir = null, $origDir = null)
00059     {
00060         if ($dataDir === null)
00061             $dataDir = Config::get('img_data_dir');
00062         if ($webDir === null)
00063             $webDir = Config::get('img_dir');
00064         if ($origDir === null)
00065             $origDir = Config::get('img_orig_dir');
00066             
00067         $this->objectEnglish = 'image';
00068         
00069         //we need these directories.
00070         self::$dataDir = realpath($dataDir);
00071         self::$origDir = realpath($origDir);
00072         self::$webDir = $webDir;
00073 
00074         //do some automagic on defaults..
00075         if (!Config::get('default_image_path'))
00076         {
00077             Config::set("default_image_path", "lib/img/base-image");
00078             Config::set("default_image_file", "missing-image.jpg");
00079             Config::set("default_image_width", "228");
00080             Config::set("default_image_height", "338");
00081         }
00082         
00083         //setup our default sizes...
00084         $this->sizeAr['tiny'] = new ImageSize("Tiny Icon", "tiny", 16, 16, true, 'square');
00085         $this->sizeAr['icon'] = new ImageSize("Medium Icon", "icon", 45, 45, true, 'square');
00086         $this->sizeAr['lgicon'] = new ImageSize("Large Icon", "lgicon", 95, 95, true, 'square');
00087         $this->sizeAr['thumb'] = new ImageSize("Thumbnail", "thumb", 120, 120, true, 'square');
00088         $this->sizeAr['profile'] = new ImageSize("Profile", "profile", 250, 250, true);
00089         $this->sizeAr['web'] = new ImageSize("Web Image", "web", 540, 1200, true);
00090         $this->sizeAr['large'] = new ImageSize("L", "800x600", 800, 1600); 
00091         $this->sizeAr['hires'] = new ImageSize("XL", "1600x1200", 1600, 1200); 
00092         
00093         //call our parent constructor.
00094         parent::__construct($data, $table);
00095 
00096         $this->fullTextFields[] = 'caption';
00097     }
00098 
00104     public function load($data, $deep = true)
00105     {
00106         parent::load($data, $deep);
00107 
00108         if (!$this->id)
00109         {
00110             $this->width = Config::get('default_image_width');
00111             $this->height = Config::get('default_image_height');
00112         }
00113         
00114         //set our paths too
00115         $this->setPaths();
00116     }
00117 
00123     function getPagesXml()
00124     {
00125         $xml = parent::getPagesXml();
00126 
00127         $xml .= <<<XML
00128             <page name="bulkconfig" desc="config for bulk uploads">
00129                 <param name="content_id" required="1"/>
00130                 <param name="auth" required="1"/>
00131             </page>
00132             <page name="bulkform" desc="form for bulk uploads">
00133                 <param name="content_id"/>
00134             </page>
00135             <page name="bulkpost" desc="post handler for bulk uploads">
00136                 <param name="content_id" required="1"/>
00137                 <param name="auth" required="1"/>
00138             </page>
00139             <page name="resize" desc="resize images!">
00140                 <param name="id" required="1"/>
00141             </page>
00142 XML;
00143         return $xml;
00144     }
00145 
00151     public function init($page = 'main', $vars = array())
00152     {
00153         parent::init($page, $vars);
00154 
00155         if ($this->id)
00156             $this->setPaths();
00157     }
00158 
00162     function delete()
00163     {
00164         $this->deleteFiles();
00165         
00166         parent::delete();
00167     }
00168 
00172     function deleteFiles()
00173     {
00174         //delete all our sizes
00175         foreach ($this->sizeAr AS $size)
00176         {
00177             if (file_exists($size->fullPath))
00178                 unlink($size->fullPath);
00179         }
00180 
00181         //delete our originals... suppress errors
00182         @unlink("$this->origPath/$this->file");
00183 
00184         //now remove our dir... suppress errors
00185         @rmdir($this->origPath);
00186         @rmdir($this->fullPath);
00187     }
00188 
00192     function setPaths()
00193     {
00194         //get our full path to the image dir...
00195         if ($this->id)
00196             $this->fullPath = self::$dataDir . "/$this->id";
00197         //otherwise... show a default image.
00198         else
00199             $this->file = DEFAULT_IMAGE_FILE;
00200         
00201         if ($this->id)
00202             $this->origPath = self::$origDir . "/$this->id";
00203         
00204         //setup our basic file paths...
00205         if ($this->file)
00206         {
00207             //break it up.
00208             $fileAr = explode(".", $this->file);
00209 
00210             //get our extension
00211             $ext = $fileAr[count($fileAr)-1];
00212             
00213             //remove it.
00214             unset($fileAr[count($fileAr)-1]);
00215 
00216             //now get the base file name...
00217             $base = implode(".", $fileAr);
00218 
00219             //make all our size file names
00220             foreach ($this->sizeAr AS $size)
00221             {
00222                 $size->fullPath = $this->fullPath .
00223                     '/'. $base . "-" . $size->extension . "." . $ext;
00224                 
00225                 if ($this->id)
00226                     $size->webPath = self::$webDir . $this->id . "/" .  
00227                         $base . "-" . $size->extension . "." . $ext;
00228                 else
00229                     $size->webPath = DEFAULT_IMAGE_PATH . "/" .  
00230                         $base . "-" . $size->extension . "." . $ext;
00231             }
00232         }
00233     }
00234 
00240     function getSize($search)
00241     {
00242         foreach ($this->sizeAr AS $size)
00243         {
00244             if ($size->extension == $search)
00245                 return $size;
00246         }
00247         return false;
00248     }
00249 
00253     function editFormAddFields($form)
00254     {
00255         //the file field shouldnt be required for images we already have.
00256         if ($this->id)
00257             $form->add("UploadField", "file", array(
00258                 "dir" => self::$origDir
00259             ));
00260         else
00261             $form->add("UploadField", "file", array(
00262                 "required" => true,
00263                 "dir" => self::$origDir
00264             ));
00265     }
00266 
00271     function editPagePostSaveSync($form)
00272     {
00273         //we want this script to run no matter what...
00274         set_time_limit(0);
00275         ignore_user_abort(true);
00276         
00277         //what do we do with the image on edits?
00278         if ($this->id)
00279         {
00280             if (!$form->getData('file'))
00281                 $form->remove('file');
00282             else
00283                 $this->deleteFiles();
00284         }
00285 
00286         parent::editPagePostSaveSync($form);
00287     }
00288 
00294     function editPagePostSuccess($form, $url)
00295     {
00296         //only do the resize if we got a file...
00297         if ($form->hasField('file'))
00298         {
00299             $this->prepFile();
00300             $this->resizeUploadedFile();
00301         }
00302 
00303         //save our album.
00304         $album = new Album($this->content_id);
00305         $album->save();
00306 
00307         //get a url if we dont have one...
00308         if ($url == null)
00309             $url = $this->getUrl();
00310         
00311         Util::redirect($url);
00312     }  
00313 
00318     function prepFile()
00319     {
00320         //make sure our paths are set correctly
00321         $this->setPaths();
00322         
00323         //make the folder if it doesnt exist.
00324         //make our original folder.
00325         if (!is_dir($this->origPath))
00326             mkdir($this->origPath);
00327         
00328         //first change that name...
00329         $newFile = $this->cleanName($this->file);
00330         
00331         //move it...
00332         rename(self::$origDir . "/$this->file", "$this->origPath/$newFile");
00333         
00334         //and now save the change
00335         $this->file = $newFile;
00336         $this->save();
00337         
00338         //make sure our paths are set correctly
00339         $this->setPaths();
00340     }
00341 
00346     function resizeUploadedFile()
00347     {
00348         //this can take a while... background it.
00349         Util::spawnHttpAsync($this->getUrl(".resize?id=$this->id"));
00350     }
00351 
00360     public function getResizeCommands()
00361     {
00362         $this->doResize = false;
00363 
00364         $this->resize();
00365     }
00366     
00370     function resize()
00371     {
00372         //correct paths = good
00373         $this->setPaths();
00374         
00375         //make the dir
00376         if (!is_dir($this->fullPath))
00377             mkdir($this->fullPath);
00378         
00379         //first off get its info...
00380         $info = getimagesize("$this->origPath/$this->file");
00381         $this->width = $info[0];
00382         $this->height = $info[1];
00383         
00384         //we want to save those images..
00385         $this->save();
00386 
00387         //now find our biggest size...
00388         foreach ($this->sizeAr AS $size)
00389         {
00390             if ($this->width >= $size->maxWidth || $this->height >= $size->maxHeight || $size->required)
00391                 $this->resizeToSize($size);
00392         }
00393 
00394         return true;
00395     }
00396 
00400     function resizeToSize($size)
00401     {
00402         if ($this->doResize)
00403             copy("$this->origPath/$this->file", $size->fullPath);
00404         else
00405             echo "cp " . escapeshellarg("$this->origPath/$this->file") . " " . escapeshellarg($size->fullPath) . ";\n";
00406         
00407         //center crop?
00408         if ($size->scaleType == 'square')
00409             $this->centerCrop($size);
00410         
00411         //resize
00412         Img::resize($size->fullPath, $size->fullPath, "$size->maxWidth:$size->maxHeight", $this->doResize);
00413     }
00414 
00420     function centerCrop($size)
00421     {
00422         
00423         //big width...  that also meets the crop requirement.:
00424         if ($this->width > $this->height && $this->width >= $size->maxWidth)
00425             $crop = $this->height;
00426         else if($this->height > $size->maxWidth)
00427             $crop = $this->width;
00428         
00429         if ($crop < $size->maxWidth)
00430             $crop = $size->maxWidth;
00431     
00432         $cmd = "/usr/bin/convert -gravity Center -crop {$crop}x{$crop}+0+0 " .
00433             escapeshellcmd($size->fullPath) . " " . escapeshellcmd($size->fullPath);
00434         if ($this->doResize)
00435             Util::shellExec($cmd);
00436         else
00437             echo "$cmd;\n";
00438                     
00439     }
00440 
00449     function cleanName($name)
00450     {
00451         //make spaces into dashes.
00452         $name = str_replace("_", "-", $name);
00453         $name = str_replace(" ", "-", $name);
00454 
00455         //take out anything non conforming.
00456         $name = preg_replace('/[^a-zA-z0-9.-]/', '', $name);
00457 
00458         return $name;
00459     }
00460 
00464     function drawViewPage()
00465     {
00466         //draw our web image.
00467         $this->drawImage("web", false);
00468 
00469         //do we show them the admin links
00470         if ($this->canEdit() || $this->canDelete())
00471         {
00472             echo "<p>";
00473             $this->drawViewNav();
00474             echo "</p>";
00475         }
00476 
00477         $this->drawSizes();
00478     }
00479 
00483     function drawViewNav()
00484     {
00485         if ($this->canEdit())
00486             $this->drawEditLink("Edit this $this->english");
00487         echo " ~ ";
00488         if ($this->canDelete())
00489             $this->drawDeleteLink("Delete this $this->english");
00490     }
00491 
00495     function drawSizes()
00496     {
00497         //get our other sizes...
00498         $web = $this->getSize("web");
00499         
00500         $flag = 0;
00501         $hanes = 'L';
00502         foreach ($this->sizeAr AS $size)
00503         {
00504             if ($size->maxWidth > $web->maxWidth)
00505             {
00506                 if ($this->width >= $size->maxWidth || $this->height >= $size->maxHeight)
00507                 {
00508                     if (!$flag)
00509                     {
00510                         echo "\n<p>Download: ";
00511                         $flag = 1;
00512                     }
00513                     
00514                     echo "<a href=\"$size->webPath\">$hanes</a> ";
00515                 }
00516             
00517                 $hanes = 'X' . $hanes;
00518             }
00519         }
00520         echo "</p>\n";
00521     }
00522 
00530     function url($type = "thumb")
00531     {
00532         if ($size = $this->getSize($type))
00533             return $size->webPath;
00534         else
00535             return false;
00536     }
00537 
00547     function getImage($type = "thumb", $link = true, $drawCaption = true)
00548     {
00549         $rval = '';
00550         //get our size...
00551         if ($size = $this->getSize($type))
00552         {
00553             //create our view link
00554             $viewLink = $this->getUrl(array("page" => "view", "id" => $this->id));
00555         
00556             //do we want to link it?
00557             if ($link)
00558                 $rval .= "<a href=\"$viewLink\">";
00559         
00560             //get our scaling...
00561             $sizeAr = $size->getScaled($this->width, $this->height);
00562 
00563             //draw image
00564             $rval .= "<img class=\"img_$type\" src=\"$size->webPath\" width=\"$sizeAr[x]\" height=\"$sizeAr[y]\" border=\"0\" alt=\"$alt\" />";
00565         
00566             if ($link)
00567                 $rval .= "</a>";
00568             
00569             //did we get a caption?
00570             if ($this->caption && $drawCaption)
00571                 $rval .= "<p>$this->caption</p>";
00572         }
00573         
00574         return $rval;
00575     }
00576     
00580     function drawImage($type = "thumb", $link = true, $drawCaption = true)
00581     {
00582         echo $this->getImage($type, $link, $drawCaption);
00583     }
00584 
00588     function drawRow()
00589     {
00590         $size = $this->getSize("thumb");
00591         echo "<div id='imageCell' style=\"height: {$size->maxHeight}px;\">\n";
00592         $this->drawImage("thumb", true, false);
00593         echo "</div>\n";
00594     }
00595 
00599     function initBulkConfigPage()
00600     {
00601         global $me;
00602 
00603         //authenticate it!
00604         $_COOKIE['auth'] = $this->params('auth');
00605         $me->authenticate();
00606         
00607         //gotta be logged in.
00608         $this->assertLogin();
00609 
00610         $this->setTemplate(new BlankTemplate());
00611     }
00612 
00620     function drawBulkConfigPage()
00621     {
00622         # Configuration file for Thin File Upload
00623         #
00624         # Online documentation is available at http://upload.thinfile.com/docs/
00625         # Lines begining with the '#' symbol denote comments. They will not
00626         # be processed.
00627         #
00628 
00629 
00630         #
00631         # The url is the upload destination. It should point to the url on your
00632         # server that will accept the uploaded file
00633         # examples:
00634         #     url=http://upload.thinfile.com/demo/upload.php
00635         #     url=http://upload.thinfile.com/cgi-bin/upload.cgi
00636         #
00637 
00638         $contentId = $this->params('content_id');
00639         echo "url=" . $this->getUrl(".bulkpost?content_id=$contentId&auth=" . $this->params('auth'), null, true) . "\n";
00640 
00641         #
00642         # To change the welcome message displayed when the applet starts up,
00643         # change the message property. It should be a valid url and should point
00644         # to a web page.
00645         #
00646         # examples:
00647         #   message=http://upload.thinfile.com/demo/init.php
00648         #
00649 
00650         echo "allow_types=jpg,gif,png\n";
00651         echo "filter_action=reject\n";
00652         echo "reject_message=Only images are allowed to be uploaded.\n";
00653         
00654         #this is if they dont accept the applet...
00655         #echo "denied_message=You must agree to run this as 'trusted'.  Otherwise the applet doesn't have permission to open and upload your images.\n";
00656 
00657         #
00658         # If you want to impose a limit on the total size of the file upload
00659         # enter a value in kilobytes for the max_upload parameter. A value
00660         # of 0, the default means unlimited. Please make sure that the server
00661         # side configuration does not impose a lower limit than what you choose
00662         # for max_upload
00663         #
00664         # Example:
00665         #    max_upload=1024
00666         #    will impose a limit of 1 Mega Byte
00667         #
00668 
00669         echo "max_upload=0\n";
00670 
00671         #
00672         # The max_upload property checks the sum of file sizes. You can impose
00673         # a limit on the size of individual files using the max_file property.
00674         # The value is in kilobytes. 0 means no limit.
00675         #
00676         # Example:
00677         #   max_file=2048
00678         #   for a limit of 2 Mega Bytes
00679 
00680         echo "max_file=0\n";
00681 
00682         #
00683         # size_exceeded message will be displayed when either the max_upload or
00684         # max_file setting has been exceeded. If you enter a text message it
00685         # will be displayed as a popup. If you enter a URL, the chosen page be
00686         # loaded with in the applet.
00687         #
00688         # example:
00689         echo "size_exceeded=Oops!  Your files were too big!  Try uploading fewer in one try.\n";
00690         #
00691 
00692         #
00693         # As the name suggests the full_path setting determines if absolute pathnames
00694         # should be sent to the server. If you switch this off, folder information will
00695         # be stripped from the filenames.
00696         #
00697 
00698         echo "full_path=no\n";
00699 
00700         #
00701         # When the translate_path setting is switched on, windows style pathnames will
00702         # be converted to unix style paths. In other words '\' becomes '/'.  This
00703         # setting is required for Resumable file upload.
00704         #
00705 
00706         echo "translate_path=yes\n";
00707 
00708         #
00709         # When encode_path setting is switched on, pathnames are URLEncoded. This is
00710         # usefull if you are dealing with filenames that contain special characters.
00711         # this setting is required for resumable file upload.
00712         #
00713 
00714         echo "encode_path=yes\n";
00715 
00716         #
00717         # The progress indicator can display a thumbnail of each image as it is being
00718         # uploaded. To enable this feature uncomment the show_thumb property below.
00719         # examples:
00720         echo "show_thumb=1\n";
00721 
00722         #
00723         # If you need to disable the multiple upload feature, and to upload files one
00724         # at a time, switch to bachelor mode. When bachelor property is set the applet
00725         # will complain if you try to upload more than one file. Use the angry_bachelor
00726         # property to set the error message to be displayed.
00727         #
00728         # bachelor=1;
00729         # angry_bachelor=http://upload.thinfile.com/demo/single.html
00730         #
00731         # Sometimes you might want to bring up a file-open dialog box. If you use the
00732         # browse setting the drop target listens for mouse clicks and brings up a file
00733         # dialog. If instead of clicking on the drop target you wish to display a browse
00734         # button set the browse_button property as well.
00735         # example:
00736         #    browse=1
00737         #    browse_button=1
00738 
00739         echo "browse=1\n";
00740         echo "browse_button=1\n";
00741 
00742         #
00743         # By default the progress indicator will be hidden (closed) when the upload
00744         # completes. By uncommenting the following line you can continue to keep the
00745         # progress bar visible even after upload has been completed. The user will
00746         # then have to manually close the progress bar.
00747         # example:
00748         #   monitor_keep_visible=yes
00749         #
00750 
00751         #
00752         # The applet or the entire browser can be redirected to another page
00753         # when upload comples. Select the destination URL with the
00754         # external_redir parameter.
00755         #
00756         # If you do not enter a value for the external_target property, the URL
00757         # given external_target will be loaded with in the applet. Otherwise
00758         # the page will be loaded in the target frame. To redirect the entire
00759         # browser window use '_top' as the target.
00760         #
00761         # If you wish to delay the redirect, enter a value for the
00762         # redirect_delay property (in milliseconds).
00763         # example:
00764         #echo "external_redir=" . $this->getUrl('.bulksuccess', null, true) . "\n";
00765         #echo "external_target=_top\n";
00766         #echo "redirect_delay=1000\n";
00767 
00768         echo "scale_images=yes\n";
00769         echo "img_max_width=600\n";
00770         echo "img_max_height=800\n";
00771     }
00772 
00776     function initBulkFormPage()
00777     {
00778         $this->assertLogin();
00779         
00780         $this->pageTitle = 'Bulk Image Upload';
00781     }
00782 
00788     function drawBulkFormPage()
00789     {
00790         global $me;
00791         
00792         //which album?
00793         $contentId = $this->params('content_id');
00794 
00795         //show our form if we can.
00796         if ($contentId)
00797         {
00798             echo "<table><tr><td width=\"50%\">";
00799             
00800             echo "<p>Welcome to our bulk upload tool.</p>";
00801             
00802             echo "<p>Please click 'Run', 'Always', or 'Yes' when it asks you to.  If
00803             you dont, you'll have to " .
00804             $this->getLink(".edit?content_id=$contentId", "upload images one by
00805             one") . ".</p>\n";
00806             
00807             echo "<p>If for some reason the tool doesn't show up, or just hangs on upload, you will
00808             need to <a
00809             href=\"http://www.java.com/en/download/index.jsp\">install Java&copy;</a></p>\n";
00810 
00811             echo "<p><b>Make sure you have the latest version of Java&copy; installed.</b></p>\n";
00812             
00813             echo "</td><td width=\"50%\" align=\"center\">";
00814             $this->drawBulkApplet($contentId);
00815             echo "</td></tr></table>";
00816         }
00817         //do we need to show a content select form?
00818         else
00819             $this->drawBulkFormSelect();
00820     }
00821 
00827     function drawBulkFormSelect()
00828     {
00829         global $me;
00830 
00831         //we need to do a search for albums...  
00832         $albums = new Albums();
00833         $rs = $albums->search(array('user_id' => $me->id, 'show_empty' => 1));
00834 
00835         if (count($rs))
00836         {
00837             $items = array();
00838             foreach ($rs AS $obj)
00839                 $items[$obj->id] = $obj->getName();
00840                 
00841             $form = new Form();
00842             $form->action = $this->getUrl('.bulkform');
00843 
00844             $form->add('SelectField', 'content_id', array(
00845                 'options' => $items,
00846                 'required' => true,
00847                 'title' => 'Choose an album to upload to:'
00848             ));
00849             $form->addSubmit('Continue');
00850 
00851             $form->draw();
00852         }
00853 
00854         echo "<br/>" . $this->getLink('album.edit', 'Click here') . " if you want to create a new album.";
00855     }
00856 
00862     function drawBulkApplet($contentId)
00863     {
00864         //use applet for weird browsers...
00865         $user_agent = $_SERVER['HTTP_USER_AGENT'];
00866         
00867         //give IE some weird update stuff
00868         if(strstr($user_agent,"MSIE"))
00869         {
00870             echo "<object classid=\"clsid:8AD9C840-044E-11D1-B3E9-00805F499D93\"";
00871             echo " width=\"300\" height=\"310\"";
00872             echo " codebase=\"http://java.sun.com/update/1.5.0/jinstall-1_5-windows-i586.cab#version=1,4,1\">";
00873         }
00874         //and firefox wants a nice standard object.
00875         else
00876         {
00877             echo '<object type="application/x-java-applet;version=1.4.1" width= "300" height= "310"  id="thinup" name="ThinUpload">';
00878             echo '<param name="MAYSCRIPT" value="yes">';
00879         } 
00880 
00881         if (!Config::get('thin_image_path'))
00882             echo '<param name="archive" value="http://' . Config::get('site_hostname') . Config::get('site_webpath') . 'ThinImage.jar">';
00883         else
00884             echo '<param name="archive" value="' . Config::get('thin_image_path') . 'ThinImage.jar">';
00885         echo '<param name="code" value="com.thinfile.upload.ThinImageUpload">';
00886         echo '<param name="name" value="Thin Image Upload">';
00887         echo '<param name="props_file" value="' . $this->getUrl(".bulkconfig?content_id=$contentId&auth=$_COOKIE[auth]", null, true) . '">';
00888         echo '</object>';
00889     }
00890 
00894     function initBulkPostPage()
00895     {   
00896         global $me;
00897         
00898         //authenticate it!
00899         $_COOKIE['auth'] = $this->params('auth');
00900         $me->authenticate();
00901         
00902         //we want this script to run no matter what...
00903         set_time_limit(0);
00904         ignore_user_abort(true);
00905         
00906         $this->setTemplate(new BlankTemplate());
00907     
00908         //try and authenticate the user...
00909         if (!$me->id)
00910             throw new PageError('We could not authenticate you.');
00911     }
00912 
00917     function drawBulkPostPage()
00918     {
00919         global $me;
00920         
00921         $contentId = $this->params('content_id');
00922         
00923         //get all our file info..
00924         $data = $_FILES['userfile'];
00925         $files = $data['name'];
00926         $tmp = $data['tmp_name'];
00927         $type = $data['type'];
00928 
00929         //go thru our files...
00930         if (count($files))
00931         {
00932             foreach ($files AS $key => $file)
00933             {
00934                 //create our new image
00935                 $image = new Image();
00936                 $image->file = $file;
00937 
00938                 if ($image->hasField('content_id'))
00939                     $image->content_id = $contentId;
00940                 
00941                 $image->user_id = $me->id;
00942                 $image->add_date = date("Y-m-d H:i:s");
00943                 $image->edit_date = date("Y-m-d H:i:s");
00944 
00945                 //if its legit...?
00946                 if (self::isImage($tmp[$key]))
00947                 {
00948                     //a real file?
00949                     if ($image->moveUploadedFile($tmp[$key]))
00950                     {
00951                             //save it and process...
00952                             $image->save();
00953                             $image->prepFile();
00954                             $image->resizeUploadedFile();
00955                     }
00956                     else
00957                         echo "There was an error uploading $image->file, skipped.<br>\n";
00958                 }
00959                 else
00960                     echo "$image->file is not a valid image file, skipped.<br>\n";
00961             }
00962             
00963             $album = new Album($contentId);
00964             $album->save();
00965         
00966             $this->drawBulkSuccessText($contentId);
00967         }
00968         else
00969             echo "There were no files uploaded.";
00970     }
00971     
00976     function drawBulkSuccessText($contentId)
00977     {
00978         echo "Your images have been uploaded.<br><br>You may upload more if you wish.";
00979         echo "<br><br>Please allow a minute or two for your images to show up.";
00980     }
00981 
00989     static public function isImage($path)
00990     {
00991         list($width, $height, $type, $attr) = getimagesize($path);
00992 
00993         //it must be a gif, jpg, or png
00994         if ($type < 4 && $type)
00995             return true;
00996         else
00997             return false;
00998     }
00999 
01005     function moveUploadedFile($tmp)
01006     {
01007         return move_uploaded_file($tmp, self::$origDir . "/$this->file");
01008     }
01009 
01013     function initResizePage()
01014     {
01015         //we're gonna be called async... finish regardless
01016         ignore_user_abort();
01017 
01018         //we dont need no stinkin template
01019         $this->setTemplate(new BlankTemplate());
01020 
01021         $this->id = (int)$this->params('id');
01022     }
01023 
01027     function drawResizePage()
01028     {
01029         if ($this->resize())
01030             echo 'Success!';
01031         else
01032             echo 'Failure. :/';
01033     }
01034 
01040     function getRssItem()
01041     {
01042         $item = parent::getRssItem();
01043 
01044         $item->title = $this->file;
01045         $item->description = $this->getImage('web');
01046 
01047         return $item;
01048     }
01049 
01053     function getPublicData()
01054     {
01055         $data = parent::getPublicData();
01056 
01057         if ($this->hasField('caption'))
01058             $data['caption'] = $this->caption;
01059 
01060         //save our sizes.
01061         $data['sizes'] = array();
01062         foreach ($this->sizeAr AS $size)
01063         {
01064             $data['sizes'][$size->extension]['html'] = $this->getImage($size->extension, false, false);
01065             $data['sizes'][$size->extension]['url'] = $this->url($size->extension);
01066         }
01067         return $data;
01068     }
01069 
01077     public function getCreateFieldsArray()
01078     {
01079         $fields = parent::getCreateFieldsArray();
01080 
01081         $fields['content_id'] = "content_id int(11) default 0 not null";
01082         $fields['caption'] = "caption text default '' not null";
01083         $fields['file'] = "file varchar(255) default '' not null";
01084         $fields['image_size'] = "image_size varchar(30) default '' not null";
01085         $fields['width'] = "width int(11) default 0 not null";
01086         $fields['height'] = "height int(11) default 0 not null";
01087     
01088         return $fields;
01089     }
01090 
01098     public function getCreateIndexesArray()
01099     {
01100         $fields = parent::getCreateIndexesArray();
01101 
01102         $fields['content_id'] = "key(content_id)";
01103 
01104         return $fields;
01105     }
01106 }
01107 
01114 class ImageSize
01115 {
01119     public $name;
01123     public $extension;
01127     public $maxWidth;
01131     public $maxHeight;
01135     public $required;
01139     public $scaleType;
01140     
01151     function __construct($name, $extension, $maxWidth, $maxHeight, $required = false, $scaleType = 'ratio')
01152     {
01153         $this->name = $name;
01154         $this->extension = $extension;
01155         $this->maxWidth = $maxWidth;
01156         $this->maxHeight = $maxHeight;
01157         $this->required = $required;
01158         $this->scaleType = $scaleType;
01159     }
01160 
01169     function getScaled($x, $y)
01170     {
01171         $rval = array();
01172         
01173         if ($this->scaleType == 'square')
01174         {
01175             if ($x < $this->maxWidth)
01176                 $rval['x'] = $x;
01177             else
01178                 $rval['x'] = $this->maxWidth;
01179                 
01180             if ($x < $this->maxHeight)
01181                 $rval['y'] = $x;
01182             else
01183                 $rval['y'] = $this->maxHeight;
01184 
01185             return $rval;
01186         }
01187         else if ($this->scaleType == 'ratio')
01188         {
01189             if ($x && $y)
01190             {
01191                 $xRatio = $this->maxWidth / $x;
01192                 $yRatio = $this->maxHeight / $y;
01193 
01194                 if ($xRatio > $yRatio)
01195                     $ratio = $yRatio;
01196                 else
01197                     $ratio = $xRatio;
01198 
01199                 if ($ratio >= 1)
01200                 {
01201                     $rval['x'] = $x;
01202                     $rval['y'] = $y;
01203                 }
01204                 else
01205                 {
01206                     $rval['x'] = round($ratio * $x);
01207                     $rval['y'] = round($ratio * $y);
01208                 }
01209                 
01210                 return $rval;
01211             }
01212             else
01213                 return false;
01214         }
01215     }
01216 }
01217 ?>

Generated on Fri Oct 27 12:26:40 2006 for BaseJumper by doxygen 1.3.9.1