public function process(Pagemill_Tag $tag, Pagemill_Data $data, Pagemill_Stream $stream) { $src = $data->parseVariables($tag->getAttribute('src')); $attr = 'src'; if (!$src) { $src = $data->parseVariables($tag->getAttribute('href')); $attr = 'href'; } $width = $data->parseVariables($tag->getAttribute('width')); $height = $data->parseVariables($tag->getAttribute('height')); // TODO: Is this good enough? $file = TYPEF_DIR . substr($src, strlen(TYPEF_WEB_DIR)); // TODO: Should this generate an error? if (!file_exists($file) || !is_file($file)) { return; } $ext = pathinfo($file, PATHINFO_EXTENSION); if (strtolower($ext) == 'bmp') { $ext = 'jpg'; } $md5 = md5("{$src}_{$width}_{$height}_{$this->_ratio}") . ".{$ext}"; if (file_exists(TYPEF_DIR . "/files/public/timg/{$md5}")) { if (filemtime($file) < filemtime(TYPEF_DIR . '/files/public/timg/' . $md5)) { $tag->setAttribute($attr, TYPEF_WEB_DIR . "/files/public/timg/{$md5}"); $size = getimagesize(TYPEF_DIR . "/files/public/timg/{$md5}"); $tag->setAttribute('width', $size[0]); $tag->setAttribute('height', $size[1]); return; } } // Resize image now if the file is below a particular // size. We'll try it with 900kb for now. if (filesize($file) < 900000) { Gdi::Thumbnail($file, TYPEF_DIR . '/files/public/timg/' . $md5, $width, $height, $this->_ratio); if (file_exists(TYPEF_DIR . '/files/public/timg/' . $md5)) { $tag->setAttribute($attr, TYPEF_WEB_DIR . "/files/public/timg/{$md5}"); } $tag->removeAttribute('width'); $tag->removeAttribute('height'); } else { // Schedule the resizing. $queue = new Model_TimgQueue(); $queue->where('src = ?', $file); $queue->where('dst = ?', TYPEF_DIR . "/files/public/timg/{$md5}"); if (!$queue->count()) { $timg = Model_TimgQueue::Create(); $timg['src'] = $file; $timg['dst'] = TYPEF_DIR . "/files/public/timg/{$md5}"; $timg['width'] = $width; $timg['height'] = $height; $timg['ratio'] = $this->_ratio; $timg->save(); } } }
public static function resize_image($src, $width, $height, $ratio) { //return Pagemill_Tag_Timg::_ResizeImage($src, $width, $height, $ratio); // TODO: Is this good enough? $file = TYPEF_DIR . substr($src, strlen(TYPEF_WEB_DIR)); // TODO: Should this generate an error? if (!file_exists($file) || !is_file($file)) { return $src; } $ext = pathinfo($file, PATHINFO_EXTENSION); if (strtolower($ext) == 'bmp') { $ext = 'jpg'; } $md5 = md5("{$src}_{$width}_{$height}_{$ratio}") . ".{$ext}"; if (file_exists(TYPEF_DIR . "/files/public/timg/{$md5}")) { if (filemtime($file) < filemtime(TYPEF_DIR . '/files/public/timg/' . $md5)) { return TYPEF_WEB_DIR . "/files/public/timg/{$md5}"; } } // Resize image now if the file is below a particular // size. We'll try it with 900kb for now. if (filesize($file) < 900000) { Gdi::Thumbnail($file, TYPEF_DIR . '/files/public/timg/' . $md5, $width, $height, $ratio); if (file_exists(TYPEF_DIR . '/files/public/timg/' . $md5)) { return TYPEF_WEB_DIR . '/files/public/timg/' . $md5; } } else { // Schedule the resizing. $queue = new Model_TimgQueue(); $queue->where('src = ?', $file); $queue->where('dst = ?', TYPEF_DIR . "/files/public/timg/{$md5}"); if (!$queue->count()) { $timg = Model_TimgQueue::Create(); $timg['src'] = $file; $timg['dst'] = TYPEF_DIR . "/files/public/timg/{$md5}"; $timg['width'] = $width; $timg['height'] = $height; $timg['ratio'] = $ratio; $timg->save(); } } return $src; }
<?php $queue = new Model_TimgQueue(); $queue->limit(100); foreach ($queue->select() as $timg) { Gdi::Thumbnail($timg['src'], $timg['dst'], $timg['width'], $timg['height'], $timg['ratio']); $timg->delete(); }