[Spidertree-cvs] spidertree/classes SAlbum.php,1.7,1.8 SImage.php,1.4,1.5
Status: Pre-Alpha
Brought to you by:
spiderr
|
From: <sp...@us...> - 2004-02-13 07:00:23
|
Update of /cvsroot/spidertree/spidertree/classes In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25018/classes Modified Files: SAlbum.php SImage.php Log Message: first pass at quasi-working thumbnails Index: SAlbum.php =================================================================== RCS file: /cvsroot/spidertree/spidertree/classes/SAlbum.php,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- SAlbum.php 9 Feb 2004 06:23:02 -0000 1.7 +++ SAlbum.php 13 Feb 2004 06:54:55 -0000 1.8 @@ -98,7 +98,7 @@ * @return TRUE if successful, FALSE if not. mErrors will hold error strings * @access public **/ - function loadImages( $iImageSize=IMAGE_SIZE_SMALL ) { + function loadImages( $iImageSize=IMAGE_SIZE_THUMB ) { if( !empty( $this->mALID ) ) { $sql = "SELECT i.* FROM ".STREE_DB_PREFIX."album a, ".STREE_DB_PREFIX."album_map am, ".STREE_DB_PREFIX."image i WHERE a.album_id=? AND am.album_id=a.album_id AND am.image_id=i.image_id"; Index: SImage.php =================================================================== RCS file: /cvsroot/spidertree/spidertree/classes/SImage.php,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- SImage.php 9 Feb 2004 06:23:02 -0000 1.4 +++ SImage.php 13 Feb 2004 06:54:55 -0000 1.5 @@ -38,9 +38,10 @@ require_once( KERNEL_PKG_PATH.'TikiBase.php' ); define( 'IMAGE_SIZE_ORIGINAL', 'upload' ); -define( 'IMAGE_SIZE_SMALL', 'small' ); -define( 'IMAGE_SIZE_MEDIUM', 'medium' ); -define( 'IMAGE_SIZE_LARGE', 'large' ); +define( 'IMAGE_SIZE_THUMB', '120' ); +define( 'IMAGE_SIZE_SMALL', '200' ); +define( 'IMAGE_SIZE_MEDIUM', '400' ); +define( 'IMAGE_SIZE_LARGE', '800' ); class SImage extends TikiBase { // {{{ properties @@ -243,6 +244,22 @@ } } + function generateThumbnails() { + $sizeArray = array( IMAGE_SIZE_LARGE, IMAGE_SIZE_MEDIUM, IMAGE_SIZE_SMALL, IMAGE_SIZE_THUMB ); + if( $this->mUID && (isset( $this->mRow['file_name'] ) || ($this->load() && isset( $this->mRow['file_name'] ) ) ) ) { + $this->mDB->StartTrans(); + foreach( $sizeArray as $dim ) { + $exec = "convert -size ".$dim."x".$dim." ".getImagePath( $this->mRow['file_name'], $this->mUID, IMAGE_SIZE_ORIGINAL )." +profile '*' -resize ".$dim."x".$dim." ".getImagePath( $this->mRow['file_name'], $this->mUID, $dim ); +$ret = exec( $exec ); +pvd( $ret ); + // update existing row + $sql = "UPDATE ".STREE_DB_PREFIX."thumbnail_queue SET end_date=?"; + + } + $this->mDB->CompleteTrans(); + } + } + } @@ -254,7 +271,7 @@ global $gSpidertree; $ret = NULL; if( is_numeric( $iUID ) ) { - $userDir = $gSpidertree['content_path'].'/'.getUserPath(); + $userDir = $gSpidertree['content_path'].'/'.getUserPath( $iUID ); mkdir_p( $userDir ); mkdir_p( $userDir.IMAGE_SIZE_ORIGINAL ); mkdir_p( $userDir.IMAGE_SIZE_SMALL ); @@ -270,6 +287,12 @@ } +function getImagePath( $iFileName, $iUID, $iImageSize ) { + global $gSpidertree; + $path = $gSpidertree['content_path'].getUserPath( $iUID ).$iImageSize.'/'; + mkdir_p( $path ); + return $path.$iFileName; +} function getImageURL( $iFileName, $iUID, $iImageSize ) { global $gSpidertree; @@ -345,6 +368,99 @@ return array(0, 0); } +function resize_image($src, $dest, $target=0, $target_fs=0, $keepProfiles=0) { + global $gallery; + + if (!strcmp($src,$dest)) { + $useTemp = true; + $out = "$dest.tmp"; + } + else { + $out = $dest; + $useTemp = false; + } + + $regs = getimagesize($src); + if ($regs[2] !== 2 && $regs[2] !== 3) { + $target_fs = 0; // can't compress other images + } + if ($target === 'off') { + $target = 0; + } + + // Check for images smaller then target size, don't blow them up. + $regs = getDimensions($src, $regs); + if ((empty($target) || ($regs[0] <= $target && $regs[1] <= $target)) + && (empty($target_fs) || ((int) fs_filesize($src) >> 10) <= $target_fs)) { + if ($useTemp == false) { + fs_copy($src, $dest); + } + processingMsg(" ". _("No resizing required")); + return 1; + } + $target=min($target, max($regs[0],$regs[1])); + + if ($target_fs == 0) { + compress_image($src, $out, $target, $gallery->app->jpegImageQuality, $keepProfiles); + } else { + $filesize = (int) fs_filesize($src) >> 10; + $max_quality=$gallery->app->jpegImageQuality; + $min_quality=5; + $max_filesize=$filesize; + $min_filesize=0; + if (!isset($quality)) { + $quality=$gallery->album->fields['last_quality']; + } + processingMsg(" ". sprintf(_("target file size %d kbytes"), + $target_fs)."\n"); + + do { + compress_image($src, $out, $target, $quality, $keepProfiles); + $prev_quality=$quality; + printf(_("-> file size %d kbytes"), round($filesize)); + processingMsg(" " . sprintf(_("trying quality %d%%"), + $quality)); + clearstatcache(); + $filesize= (int) fs_filesize($out) >> 10; + if ($filesize < $target_fs) { + $min_quality=$quality; + $min_filesize=$filesize; + } else if ($filesize > $target_fs){ + $max_quality=$quality; + $max_filesize=$filesize; + } else if ($filesize == $target_fs){ + $min_quality=$quality; + $max_quality=$quality; + $min_filesize=$filesize; + $max_filesize=$filesize; + } + $quality=($max_quality + $min_quality)/2; + $quality=round($quality); + if ($quality==$prev_quality) { + if ($filesize==$max_filesize) { + $quality--; + } else { + $quality++; + } + } + } while ($max_quality-$min_quality > 2 && + abs(($filesize-$target_fs)/$target_fs) > .02 ); + + $gallery->album->fields['last_quality']=$prev_quality; + printf(_("-> file size %d kbytes"), round($filesize)); + processingMsg(_("Done.")); + } + if (fs_file_exists("$out") && fs_filesize("$out") > 0) { + if ($useTemp) { + fs_copy($out, $dest); + fs_unlink($out); + } + return 1; + } else { + return 0; + } +} + function rotate_image($src, $dest, $target, $type) { global $gallery; @@ -644,6 +760,79 @@ } } +function exec_wrapper($cmd) { + global $gallery; + + list($results, $status) = exec_internal($cmd); + + if ($status == $gallery->app->expectedExecStatus) { + return 0; + } else { + if ($results) { + gallery_error(join("<br>", $results)); + } + return 1; + } +} + +function exec_internal($cmd) { + global $gallery; + + $debugfile = ""; + if (isDebugging()) { + print "<p><b>". _("Executing:") ."<ul>$cmd</ul></b>"; + $debugfile = tempnam($gallery->app->tmpDir, "dbg"); + } + + fs_exec($cmd, $results, $status, $debugfile); + + if (isDebugging()) { + print "<br>" . _("Results:") ." <pre>"; + if ($results) { + print join("\n", $results); + } else { + print "<b>" ._("none") ."</b>"; + } + print "</pre>"; + + if (file_exists($debugfile)) { + print "<br> ". _("Error messages:") .": <pre>"; + if ($fd = fs_fopen($debugfile, "r")) { + while (!feof($fd)) { + $buf = fgets($fd, 4096); + print $buf; + } + fclose($fd); + } + unlink($debugfile); + print "</pre>"; + } + print "<br> ". sprintf(_("Status: %s (expected %s)"), + $status, $gallery->app->expectedExecStatus); + } + + return array($results, $status); +} + +function fs_exec($cmd, &$results, &$status, $debugfile="") { + if (!empty($debugfile)) { + $cmd = "($cmd) 2>$debugfile"; + } + return exec($cmd, $results, $status); +} + +function fs_import_filename($filename, $for_exec=1) { + if ($for_exec) { + if (strstr($filename, " ")) { + $filename = "\"$filename\""; + } + } + + return $filename; +} + + + */ |