![]() |
|
Snippets |
|
A simple class to generate thumbnail linked to the propel model. sfThumbnailPlugin is needed and sfPropelPlugin (of course)
Override two methods create one, add a constant and go !
Give me your opinion !
<?php class MediaPeer extends BaseMediaPeer { // directory of file (under uploads file) const IMAGE_DIR = 'images/media'; // override public static function doDelete($values, PropelPDO $con = null) { // delete every image file linked return sfPropelThumbnail::deleteThumb($values,$con, 'Media'); } // Configuration public static function getThumbConfig() { return array( 'image'=> // each fields to thumbnailize array( // each thumbnail configuration for field array('thumb_dir' => 'thumbnail', 'size' => array(200, 500)), array('thumb_dir' => 'fullsize' , 'size' => array(500, 700), 'crop'=>true), array('thumb_dir' => 'verysmall', 'size' => array(60,60) , 'crop'=>true) ) ); } }
<?php class Media extends BaseMedia { public function setImage($value, $thumb=true) { // if first call thumbnailize image and after put file name in the db if($thumb) sfPropelThumbnail::generateThumbnail($value,$this ,'Media', 'image'); else parent::setImage($value); } }
<?php /* * @author: Simon Bonjean (sim@45-ouest.be) */ class sfPropelThumbnail { /* * getConfiguration */ protected static function getDefaultConfig($class) { $dir = constant($class .'Peer::IMAGE_DIR'); $configs = call_user_func(array($class .'Peer','getThumbConfig')); return array($dir, $configs); } /* * Generate the thumbnails */ public static function generateThumbnail($value,$that,$class,$fields_name) { // Variable $upload_path = sfConfig::get('sf_upload_dir'); // Get path to upload list($dir, $configs) = self::getDefaultConfig($class); // Get Config and dir name $configs = $configs[$fields_name]; // Get config for name $methode = 'set'.ucfirst($fields_name); // Get Methode if(empty($value)) { // delete all thumbs foreach($configs as $config) { $thumb_dir = $config['thumb_dir']; if(is_file($upload_path . "/$dir/$fields_name/$thumb_dir/" . $that->getImage())) unlink($upload_path . "/$dir/$fields_name/$thumb_dir/" . $that->getImage() ); } // Set l'image sans faire de thumb call_user_func(array($that,$methode),$value, false); } else { // delete old thumb foreach($configs as $config){ $thumb_dir = $config['thumb_dir']; if(is_file($upload_path."/$dir/$fields_name/$thumb_dir/".$that->getImage())) unlink( $upload_path . "/$dir/$fields_name/$thumb_dir/".$that->getImage() ); } // Set l'image sans faire de thumb call_user_func(array($that,$methode),$value, false); // generate each thumbs foreach($configs as $config) { $thumb_dir = $config['thumb_dir']; $size = $config['size']; // CREATE DIRECTORY IF NOT EXIST !!! if(!is_dir($upload_path."/$dir/")) mkdir($upload_path."/$dir/"); // base name if(!is_dir($upload_path."/$dir/$fields_name/")) mkdir($upload_path."/$dir/$fields_name/"); // field name if(!is_dir($upload_path."/$dir/$fields_name/$thumb_dir/")) mkdir($upload_path."/$dir/$fields_name/$thumb_dir/"); // thumb name if(isset($config['crop']) && $config['crop'] == true) { // Make Crop self::cropImage($size[0], $size[1], $upload_path."/$dir/$fields_name/".$that->getImage(), $upload_path."/$dir/$fields_name/$thumb_dir/".$that->getImage()); } else { // Make Thumb $thumbnail = new sfThumbnail($size[0], $size[1]); $thumbnail->loadFile($upload_path."/$dir/$fields_name/".$that->getImage()); $thumbnail->save($upload_path."/$dir/$fields_name/$thumb_dir/".$that->getImage(), 'image/png'); } } } } /* * delete each thumbnail when you delete binded element */ public static function deleteThumb($values,PropelPDO $con, $class) { // Variable list($dir, $configs) = self::getDefaultConfig($class); $upload_path = sfConfig::get('sf_upload_dir'); if($retVal = call_user_func(array('Base' . $class .'Peer','doDelete'),$values, $con)) foreach($configs as $fields_name => $fields_config) { $files = self::_getThumb($values, $class, $fields_name); if($files){ foreach($files as $file) { if(is_file($upload_path."/$dir/$fields_name/".$file)) { unlink( $upload_path . "/$dir/$fields_name/".$file ); } foreach($fields_config as $config){ $thumb_dir = $config['thumb_dir']; if(is_file($upload_path."/$dir/$fields_name/$thumb_dir/".$file)) { unlink( $upload_path . "/$dir/$fields_name/$thumb_dir/".$file ); } } } } } return $retVal; } /* * retruive all piture */ protected static function _getThumb($values, $class, $fields_name){ // Variable $files = false; $fields_name = 'get' . ucfirst($fields_name); if ($values instanceof Criteria) { $tmps = call_user_func(array($class.'Peer','doSelect'), $values); foreach($tmps as $value) if($temp_thumb = call_user_func(array($value,$fields_name)) != '') $files[] = $temp_thumb; } elseif (get_class($values) == $class) { if($temp_thumb = call_user_func(array($values,$fields_name)) != '') $files[] = $temp_thumb; } else { $tmps = call_user_func(array($classPeer,'retrieveByPKs'),$values); foreach($tmps as $value) if($temp_thumb = call_user_func(array($value,$fields_name)) != '') $files[] = $temp_thumb; } return $files; } /* * This method thumbnailize image with crop option (center default) */ public static function cropImage($nw, $nh, $source, $dest) { $size = getimagesize($source); $len = strlen($source); $stype = substr($source,$len-3,3); $w = $size[0]; $h = $size[1]; switch($stype) { case 'gif': $simg = imagecreatefromgif($source); break; case 'jpg': $simg = imagecreatefromjpeg($source); break; case 'png': $simg = imagecreatefrompng($source); break; } $dimg = imagecreatetruecolor($nw, $nh); $wm = $w/$nw; $hm = $h/$nh; $h_height = $nh/2; $w_height = $nw/2; if($w> $h) { $adjusted_width = $w / $hm; $half_width = $adjusted_width / 2; $int_width = $half_width - $w_height; imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h); } elseif(($w <$h) || ($w == $h)) { $adjusted_height = $h / $wm; $half_height = $adjusted_height / 2; $int_height = $half_height - $h_height; imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h); } else { imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h); } imagejpeg($dimg,$dest,150); } }