/** * */ public function initialize() { /*$this->dispatcher->connect('routing.load_configuration', array( 'sfImagePoolRouting' , 'listenToRoutingLoadConfigurationEvent' ));*/ self::$base_dir_name = sfConfig::get('app_sf_image_pool_folder'); if (!file_exists(self::getBaseDir())) { mkdir(self::getBaseDir(), 0777); } $this->connectTests(); }
/** * Return path to new image on filesystem. Creates folders if they don't exist. * * @return string */ public function getDestination() { $folder_path = implode(DIRECTORY_SEPARATOR, array(sfImagePoolPluginConfiguration::getBaseDir(), $this->resizer_options['scale'] ? 'scale' : 'crop', $this->resizer_options['width'], $this->resizer_options['height'])); // if folder not found for this resize, then attempt to create it. if (!file_exists($folder_path)) { if (!mkdir($folder_path, 0777, true)) { throw new sfImagePoolException(sprintf('Could not create "%s"', $folder_path)); } } return $folder_path . DIRECTORY_SEPARATOR . $this->image['filename']; }
/** * Take array of data for a single upload and create an image, assigning * content of $tags as tag (CSV string or single tag or array). * * This common logic is abstracted out so it can easily be used by other plugins * that require image upload but need to handle the actual upload logic themselves. * * @param array $upload * @param mixed $tags * @return sfImagePoolImage */ public static function createImageFromUpload($upload, $tags = null) { // upload was ok, mime type ok and file isn't too big so let's move it into the image pool // location and then create a new object for it. $file = new sfValidatedFile($upload['name'], $upload['type'], $upload['tmp_name'], $upload['size'], sfImagePoolPluginConfiguration::getBaseDir()); // this will generate the unique filename $new_filename = $file->save(); $image_data = array('original_filename' => $file->getOriginalName(), 'filename' => $new_filename, 'mime_type' => $file->getType()); $image = new sfImagePoolImage(); $image->fromArray($image_data); // now set tags if they've been supplied if ($tags) { $image->addTag($tags); } $image->save(); return $image; }
/** * Return path to folder where originals are stored. * * @return string */ public function getUploadDir() { return sfImagePoolPluginConfiguration::getBaseDir(); }
function pool_image_uri($image, $dimensions = 200, $method = 'crop', $absolute = false) { // remove Symfony escaping if applied if ($image instanceof sfOutputEscaper) { $image = $image->getRawValue(); } $offsite = false; if (is_array($dimensions)) { $width = $dimensions[0]; $height = $dimensions[1]; } else { if (strpos(strtolower($dimensions), 'x') !== false) { list($width, $height) = explode('x', $dimensions); } else { $height = $width = $dimensions; } } $cache_options = sfConfig::get('app_sf_image_pool_cache'); $class = $cache_options['class']; // If we are on a secure page we want to use the ssl option to avoid security warnings $ssl = sfContext::getInstance()->getRequest()->isSecure(); $off_site_index = $ssl ? 'off_site_ssl_uri' : 'off_site_uri'; // If remote and remote uri set, plus image exists if ($class::IS_REMOTE && !empty($cache_options[$off_site_index]) && $image) { // check whether crop exists - if it doesn't business as usual $is_crop = 'crop' == $method; $crop = sfImagePoolCropTable::getInstance()->findCrop($image, $width, $height, $is_crop, $class::CROP_IDENTIFIER); if ($crop) { $absolute = false; $offsite = true; } } if (!function_exists('url_for')) { sfApplicationConfiguration::getActive()->loadHelpers(array('Url')); } // If we have an empty sfImagePool instance (ie. no image) then output a placeholder if set in config to do so if (!$image['filename']) { if (sfConfig::get('app_sf_image_pool_placeholders', false)) { if (sfConfig::get('app_sf_image_pool_use_placeholdit', false)) { $url = sprintf('http://placehold.it/%ux%u&text=%s', $width, $height, urlencode(sfConfig::get('app_sf_image_pool_placeholdit_text', ' '))); } else { // If offsite then should have cached placeholder too - check whether created as crop too if ($class::IS_REMOTE && !empty($cache_options[$off_site_index])) { $is_crop = 'crop' == $method; $crop = sfImagePoolCropTable::getInstance()->findCrop(sfImagePoolImage::DEFAULT_FILENAME, $width, $height, $is_crop, $class::CROP_IDENTIFIER); if ($crop) { $absolute = false; $offsite = true; } } $url = url_for(sprintf('@image?width=%s&height=%s&filename=%s&method=%s', $width, $height, sfImagePoolImage::DEFAULT_FILENAME, $method), $absolute); } } else { return false; } // No image, no placeholder } else { $url = url_for(sprintf('@image?width=%s&height=%s&filename=%s&method=%s', $width, $height, $image['filename'], $method), $absolute); } // Do we want to remove the controller filename? It's good to have this option independent of the global Symfony // setting as we may be generating URLs to insert into the db and which should not have any controller referenced. if (!sfConfig::get('app_sf_image_pool_use_script_name', false) || $offsite) { $url = preg_replace('%\\w+(_dev)?\\.php/%', '', $url); } // If offsite - then replace local image-pool folder with offsite URL (naming convention for offsite should mirror // folder structure for local) if ($offsite) { $url = str_replace(sfImagePoolPluginConfiguration::getBaseUrl(), $cache_options[$off_site_index], $url); } return $url; }
/** * Get the path to the original file * @return string */ public function getPathToOriginalFile() { return implode(DIRECTORY_SEPARATOR, array(sfImagePoolPluginConfiguration::getBaseDir(), $this['filename'])); }