/** * DOCUMENT ME * @param mixed $value * @param mixed $imagePreview * @return mixed */ public function getPreviewUrl($value, $imagePreview = array()) { list($exists, $persistid, $extension) = $this->getExistsPersistidAndExtension($value); // hasOption just verifies that the option is valid, it doesn't check what, // if anything, was passed. Thanks to Lucjan Wilczewski $defaultPreview = $this->hasOption('default-preview') ? $this->getOption('default-preview') : false; if ($exists) { $defaultPreview = false; } if ($exists || $defaultPreview) { // Note change of key $urlStem = sfConfig::get('app_aPersistentFileUpload_preview_url', '/uploads/uploaded_image_preview'); $urlStem = sfContext::getInstance()->getRequest()->getRelativeUrlRoot() . $urlStem; // This is the corresponding directory path. You have to override one // if you override the other. You override this one by setting // app_aToolkit_upload_uploaded_image_preview_dir $dir = aFiles::getUploadFolder("uploaded_image_preview"); // While we're here age off stale previews aValidatorFilePersistent::removeOldFiles($dir); if ($exists) { $info = aValidatorFilePersistent::getFileInfo($persistid); $source = $info['tmp_name']; } else { $source = $defaultPreview; } $info = aImageConverter::getInfo($source, array('format-only' => true)); $previewable = false; if ($info && in_array($info['format'], array('jpg', 'png', 'gif'))) { $previewable = true; $info = aImageConverter::getInfo($source); } if ($previewable) { $iwidth = $info['width']; $iheight = $info['height']; // This is safe - based on sniffed file contents and not a user supplied extension $format = $info['format']; $dimensions = aDimensions::constrain($iwidth, $iheight, $format, $imagePreview); // A simple filename reveals less $imagename = "{$persistid}.{$format}"; $url = "{$urlStem}/{$imagename}"; $output = "{$dir}/{$imagename}"; if (isset($info['newfile']) && $info['newfile'] || !file_exists($output)) { if ($imagePreview['resizeType'] === 'c') { $method = 'cropOriginal'; } else { $method = 'scaleToFit'; } sfContext::getInstance()->getLogger()->info("YY calling converter method {$method} width " . $dimensions['width'] . ' height ' . $dimensions['height']); aImageConverter::$method($source, $output, $dimensions['width'], $dimensions['height']); sfContext::getInstance()->getLogger()->info("YY after converter"); } } else { // Don't try to provide an icon alternative to the preview here, // it's better to do that at the project and/or apostrophePlugin level // where we can style it better... the less we fake templating inside // a widget the better. See getFormat $url = false; } return $url; } return false; }
/** * @param string $name The element name * @param string $value The value displayed in this widget * (i.e. the browser-side filename submitted * on a previous partially successful * validation of this form) * @param array $attributes An array of HTML attributes to be merged with the default HTML attributes * @param array $errors An array of errors for the field * * @return string An HTML tag string * * @see sfWidgetForm */ public function render($name, $value = null, $attributes = array(), $errors = array()) { $exists = false; if (isset($value['persistid']) && strlen($value['persistid'])) { $persistid = $value['persistid']; $info = aValidatorFilePersistent::getFileInfo($persistid); if ($info) { $exists = true; } } else { // One implementation, not two (to inevitably drift apart) $persistid = aGuid::generate(); } $result = ''; // hasOption just verifies that the option is valid, it doesn't check what, // if anything, was passed. Thanks to Lucjan Wilczewski $preview = $this->hasOption('image-preview') ? $this->getOption('image-preview') : false; $defaultPreview = $this->hasOption('default-preview') ? $this->getOption('default-preview') : false; if ($exists) { $defaultPreview = false; } if ($exists || $defaultPreview) { if ($preview) { // Note change of key $urlStem = sfConfig::get('app_aPersistentFileUpload_preview_url', '/uploads/uploaded_image_preview'); // This is the corresponding directory path. You have to override one // if you override the other. You override this one by setting // app_aToolkit_upload_uploaded_image_preview_dir $dir = aFiles::getUploadFolder("uploaded_image_preview"); // While we're here age off stale previews aValidatorFilePersistent::removeOldFiles($dir); $imagePreview = $this->getOption('image-preview'); if ($exists) { $source = $info['tmp_name']; } else { $source = $defaultPreview; } $info = aImageConverter::getInfo($source); if ($info) { $iwidth = $info['width']; $height = $info['height']; // This is safe - based on sniffed file contents and not a user supplied extension $format = $info['format']; list($iwidth, $iheight) = getimagesize($source); $dimensions = aDimensions::constrain($iwidth, $iheight, $format, $imagePreview); // A simple filename reveals less $imagename = "{$persistid}.{$format}"; $url = "{$urlStem}/{$imagename}"; $output = "{$dir}/{$imagename}"; if (isset($info['newfile']) && $info['newfile'] || !file_exists($output)) { if ($imagePreview['resizeType'] === 'c') { $method = 'cropOriginal'; } else { $method = 'scaleToFit'; } sfContext::getInstance()->getLogger()->info("YY calling converter method {$method} width " . $dimensions['width'] . ' height ' . $dimensions['height']); aImageConverter::$method($source, $output, $dimensions['width'], $dimensions['height']); sfContext::getInstance()->getLogger()->info("YY after converter"); } } if (isset($imagePreview['markup'])) { $markup = $imagePreview['markup']; } else { $markup = '<img src="%s" />'; } $result .= sprintf($markup, $url); } $result .= $this->getOption('existing-html'); } return $result . $this->renderTag('input', array_merge(array('type' => $this->getOption('type'), 'name' => $name . '[newfile]'), $attributes)) . $this->renderTag('input', array('type' => 'hidden', 'name' => $name . '[persistid]', 'value' => $persistid)); }