/** * g?n?rer le thumb correspondant ? une image. * cete fonction v?rifie si le thumb existe d?j? ou si la source est plus r?cente * si besoin, il est reg?n?r?. * il faut faire passer en parametres options[width] et options[height] * et l'image est automatiquement redimensionner en thumb. * si width = height alors l'image sera tronqu�e et carr� * * @param <string> $image_name : le nom de l'image donc g?n?ralement le $object->getImage(), pas de r?pertoire * @param <string> $folder : le nom du r?pertoire dans uploads o? est stock? l'image : uploads/object/source => $folder = object * @param <array> $options : les parametres ? passer ? l'image: width et height * @param <string> $resize : l'op?ration sur le thumb: "scale" pour garder les proportions, "center" pour tronquer l'image * @param <string> $default : l'image par d?faut si image_name n'existe pas * @return <image_path> */ function doThumb($image_name, $folder, $options = array(), $resize = 'scale', $default = 'default.jpg') { //valeur par d�faut si elles ne sont pas d�finies if (!isset($options['width'])) { $options['width'] = 50; } if (!isset($options['height'])) { $options['height'] = 50; } /*$source_dir = 'uploads/'.$folder.'/source/'; $thumb_dir = 'uploads/'.$folder.'/thumb/';*/ $source_dir = 'uploads/' . $folder . '/'; $thumb_dir = 'uploads/' . $folder . '/thumb/'; //le fichier source $source = $source_dir . $image_name; $exist = sfConfig::get('sf_web_dir') . '/' . $source; if (!is_file($exist)) { $image_name = $default; $source = 'images/' . $image_name; // la valeur par d�faut } $new_name = $options['width'] . 'x' . $options['height'] . '_' . $image_name; $new_img = $thumb_dir . $new_name; // si le thumb n'existe pas ou s'il est plus ancien que le fichier source // alors on reg�n�re le thumb if (!is_file(sfConfig::get('sf_web_dir') . '/' . $new_img) or filemtime($source) > filemtime($new_img)) { $img = new sfImage($source); $img->thumbnail($options['width'], $options['height'], $resize)->saveAs($new_img); } return image_path('/' . $new_img); }
public function updateObject($values = null) { $object = parent::updateObject($values); if ($av = $object->getAvatar('raw')) { $tmp_name = sfConfig::get('sf_upload_dir') . '/avatars/' . $av; if (!file_exists($tmp_name)) { return $object; } // Génération miniature 50x50 $mini = new sfImage($tmp_name); $mini->thumbnail(50, 50, 'inflate'); $mini->setQuality(80); $mini->saveAs(sfConfig::get('sf_upload_dir') . '/avatars/50x50/' . $av); // Génération miniature 32x32 $mini = new sfImage($tmp_name); $mini->thumbnail(32, 32, 'inflate'); $mini->setQuality(80); $mini->saveAs(sfConfig::get('sf_upload_dir') . '/avatars/32x32/' . $av); // Génération miniature 16x16 $mini = new sfImage($tmp_name); $mini->thumbnail(16, 16, 'inflate'); $mini->setQuality(80); $mini->saveAs(sfConfig::get('sf_upload_dir') . '/avatars/16x16/' . $av); // On supprime le fichier original unlink($tmp_name); } return $object; }
public function save(Doctrine_Connection $conn = null) { $slug = Magic::slugify($this->getTitle()); if ($this->isNew()) { $i = 0; do { $i++; $q = Doctrine::getTable('Page')->findOneBySlug($slug); if (!$q) { break; } else { $slug = Magic::slugify($this->getTitle()); $slug .= $i; } } while ($i); $this->setSlug($slug); } elseif ($slug != $this->getSlug()) { $i = 0; do { $i++; $q = Doctrine::getTable('Page')->findOneBySlug($slug); if (!$q) { $this->setSlug($slug); break; } else { if ($slug == $this->getSlug()) { break; } else { $slug = Magic::slugify($this->getTitle()); $slug .= $i; } } } while ($i); } parent::save($conn); // generamos thumbnails $file = sfConfig::get('sf_upload_dir') . '/pictures/' . $this->getPicture(); if (is_file($file)) { $dims = array(array('w' => 950, 'h' => 534), array('w' => 720, 'h' => 405), array('w' => 250, 'h' => 141), array('w' => 60, 'h' => 60)); $size = getimagesize($file); $img = new sfImage($file, $size['mime']); if (!is_dir(sfConfig::get('sf_upload_dir') . '/' . $this->get('id'))) { mkdir(sfConfig::get('sf_upload_dir') . '/' . $this->get('id'), 0777); } foreach ($dims as $dim) { $img->thumbnail($dim['w'], $dim['h']); $img->setQuality(90); $img->saveAs(sfConfig::get('sf_upload_dir') . '/' . $this->get('id') . '/img_' . $this->get('id') . '_' . $dim['w'] . 'x' . $dim['h'] . '.jpg'); } } }
/** * Cache the image based on the size set in thumbnails.yml */ public function cache() { $cache_dir = pathinfo($this->cached_path, PATHINFO_DIRNAME); //create cache dir if it doesn't exist if (!file_exists($cache_dir)) { mkdir($cache_dir, 0777, true); } $size = zsThumbConfigHandler::getSize($this->size); //do image transformations $new_image = new sfImage($this->orig_path); $new_image->thumbnail($size['width'], $size['height'], array_key_exists('method', $size) ? $size['method'] : 'fit'); if (array_key_exists('quality', $size)) { $new_image->setQuality($size['quality']); } $new_image->saveAs($this->cached_path); }
public function storeScreenshot() { $path = sfConfig::get('sf_upload_dir') . '/' . sfConfig::get('app_screenshots_path') . '/' . $this->getPluginId() . '/' . $this->getId(); ForgeToolkit::createRecursiveDirectory($path . '/thumbs/'); $filename = $this->getFilename(); try { $tmp = tempnam(sys_get_temp_dir(), uniqid($this->getUrl() . time())); @copy($this->getUrl(), $tmp); if (@file_get_contents($tmp)) { $image = new sfImage($tmp); $image->saveAs($path . '/' . $filename); $image->thumbnail(sfConfig::get('app_screenshots_' . ($this->isPrimary() ? 'primary' : '') . 'width'), sfConfig::get('app_screenshots_' . ($this->isPrimary() ? 'primary' : '') . 'height'), 'center'); $image->saveAs($path . '/thumbs/' . $filename, 'image/png'); } } catch (sfImageTransformException $e) { $this->delete(); } }
protected function processForm(sfWebRequest $request, sfForm $form) { $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName())); if ($form->isValid()) { $file = $this->form->getValue('image'); $image = $this->form->getObject(); $notice = $image->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.'; try { // Image $filename = 'app_' . sha1($file->getOriginalName() . rand(1, 100)); $extension = str_replace('.', '', $file->getOriginalExtension()); $path = sfConfig::get('sf_upload_dir') . DIRECTORY_SEPARATOR . $filename . '.' . $extension; $file->save($path); // Thumb $img = new sfImage($path, 'image/jpg'); $img->thumbnail(150, 150); $img->saveAs(sfConfig::get('sf_upload_dir') . DIRECTORY_SEPARATOR . $filename . '_thumb' . '.' . $extension); // Save $image->path = $filename . '.' . $extension; $image->type = $extension; $image->name = $request->getPostParameter('picture[name]'); $image->description = $request->getPostParameter('picture[description]'); $image->apartment_id = $request->getPostParameter('picture[apartment_id]'); $image->save(); } catch (Doctrine_Validator_Exception $e) { $errorStack = $form->getObject()->getErrorStack(); $message = get_class($form->getObject()) . ' has ' . count($errorStack) . " field" . (count($errorStack) > 1 ? 's' : null) . " with validation errors: "; foreach ($errorStack as $field => $errors) { $message .= "{$field} (" . implode(", ", $errors) . "), "; } $message = trim($message, ', '); $this->getUser()->setFlash('error', $message); return sfView::SUCCESS; } // $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $picture))); } else { $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false); } }
/** * Saves the uploaded file. * * This method can throw exceptions if there is a problem when saving the file. * * If you don't pass a file name, it will be generated by the generateFilename method. * This will only work if you have passed a path when initializing this instance. * * @param string $file The file path to save the file * @param int $fileMode The octal mode to use for the new file * @param bool $create Indicates that we should make the directory before moving the file * @param int $dirMode The octal mode to use when creating the directory * * @return string The filename without the $this->path prefix * * @throws Exception */ public function save($file = null, $fileMode = 0666, $create = true, $dirMode = 0777) { if (is_null($file)) { $file = $this->generateFilename(); } if ($file[0] != '/' && $file[0] != '\\' && !(strlen($file) > 3 && ctype_alpha($file[0]) && $file[1] == ':' && ($file[2] == '\\' || $file[2] == '/'))) { if (is_null($this->path)) { throw new RuntimeException('You must give a "path" when you give a relative file name.'); } $file = $this->path . DIRECTORY_SEPARATOR . $file; } // get our directory path from the destination filename $directory = dirname($file); if (!is_readable($directory)) { if ($create && !@mkdir($directory, $dirMode, true)) { // failed to create the directory throw new Exception(sprintf('Failed to create file upload directory "%s".', $directory)); } // chmod the directory since it doesn't seem to work on recursive paths chmod($directory, $dirMode); } if (!is_dir($directory)) { // the directory path exists but it's not a directory throw new Exception(sprintf('File upload path "%s" exists, but is not a directory.', $directory)); } if (!is_writable($directory)) { // the directory isn't writable throw new Exception(sprintf('File upload path "%s" is not writable.', $directory)); } $img = new sfImage($this->getTempName(), $this->type); $img->thumbnail(sfConfig::get('app_configuration_max_person_image_width', 300), sfConfig::get('app_configuration_max_person_image_height', 300)); // copy the temp file to the destination file $img->saveAs($file); // chmod our file chmod($file, $fileMode); $this->savedName = $file; return is_null($this->path) ? $file : str_replace($this->path . DIRECTORY_SEPARATOR, '', $file); }
/** * Pour générer une miniature (incluant un watermark) * -------------------------------------------------- * - showThumb_watermark([nom de l'image], [répertoire], [options], [type de resize], [image par defaut], [image du watermark], [position du watermark]) * - [nom de l'image], [répertoire], [options], [type de resize], [image par defaut] : idem ci dessus * - image du watermark : l'image représentant le watermark à placer dans le dossier "/images" * - position du watermark : la position du watermark sur l'image (format : [de haut en bas]-[de gauche a droite]) * - Pour [de haut en bas] : top, middle, bottom * - Pour [de gauche à droite] : left, center, right * - Exemple : middle-center, middle-left, bottom-left... * * @param <string> $image_name : le nom de l’image donc généralement le $object->getImage(), pas de répertoire * @param <string> $folder : le nom du répertoire dans "uploads" où est stocké l’image * @param <array> $options : les options contenus dans un array() : la taille de l’image (width et height) + la classe, l'id, le alt ... de l'image * @param <string> $resize : "scale" pour garder les proportions (deformation de l'image) OU "center" pour tronquer l’image sans la deformer) * @param <string> $default : l’image par défaut si $image_name n’existe pas * @param <string> $watermark : l'image représentant le watermark à placer dans le dossier "/images" * @param <string> $position : la position du watermark sur l'image (format : [de haut en bas]-[de gauche a droite]) : Pour [de haut en bas] : top, middle, bottom / Pour [de gauche à droite] : left, center, right * @return <image_path> */ function doThumb_watermark($image_name, $folder, $options = array(), $resize = 'center', $default = 'default.jpg', $watermark = 'images/watermark.png', $position = 'middle-center') { if (!isset($options['width'])) { $options['width'] = 50; } if (!isset($options['height'])) { $options['height'] = 50; } $source_dir = 'uploads/' . $folder . '/'; $thumb_dir = 'uploads/' . $folder . '/thumb/'; $source = $source_dir . $image_name; $exist = sfConfig::get('sf_web_dir') . '/' . $source; if (!is_file($exist)) { $image_name = $default; $source = 'images/' . $image_name; } $new_name = $options['width'] . 'x' . $options['height'] . '_' . $image_name; $new_img = $thumb_dir . $new_name; // si le thumb n'existe pas ou s'il est plus ancien que le fichier source, alors on regénére le thumb if (!is_file(sfConfig::get('sf_web_dir') . '/' . $new_img) or filemtime($source) > filemtime($new_img)) { $img = new sfImage($source); $img->setQuality(100); $img->thumbnail($options['width'], $options['height'], $resize)->overlay(new sfImage($watermark), $position)->saveAs($new_img); } return image_path('/' . $new_img); }
/** * Generates and saves a thumbnail version of this image * * @param array $options An array of thumbnailing options. * @return boolean The success/failure of the operation */ private function _generateThumbnail($width, $height, $method) { if (!sfSympalConfig::get('assets', 'thumbnails_enabled', false)) { return; } if (!class_exists('sfImage')) { throw new sfException('sfImageTransformPlugin must be installed in order to generate thumbnails.'); } if (file_exists($this->getPath())) { $thumb = new sfImage($this->getPath()); $thumb->thumbnail($width, $height, $method); $destinationDirectory = $this->getThumbnailDirectory($width, $height, $method); if (!file_exists($destinationDirectory)) { // recursively create the directory mkdir($destinationDirectory, 0777, true); chmod($destinationDirectory, 0777); } return $thumb->saveAs($destinationDirectory . '/' . $this->getName()); } else { return false; } }
/** * @TODO * @param $file */ protected function generateThumbnail($source_file, $destination_name, $destination_dir) { if (!class_exists('sfImage')) { throw new sfException('sfImageTransformPlugin must be installed in order to generate thumbnails.'); } $thumb = new sfImage($source_file); $thumb->thumbnail(sfConfig::get('app_sf_media_browser_thumbnails_max_width', 64), sfConfig::get('app_sf_media_browser_thumbnails_max_height', 64)); $destination_dir = $destination_dir . '/' . sfConfig::get('app_sf_media_browser_thumbnails_dir'); if (!file_exists($destination_dir)) { mkdir($destination_dir); chmod($destination_dir, 0777); } return $thumb->saveAs($destination_dir . '/' . $destination_name); }
protected function updateRemoteImage($image, Swift_Message $message, $templateName) { $fileName = dmOs::join(sfConfig::get('sf_web_dir'), 'cache/mail_templates', md5($image->src . $image->width . $image->height) . pathinfo($image->src, PATHINFO_EXTENSION)); if (!file_exists($fileName)) { $imageData = file_get_contents($image->src); if (!$imageData) { $this->logError($templateName, 'remote', 'image', $image->src); $image->src = ''; return $image; } else { $sfImage = new sfImage(); $sfImage->loadString($imageData); $width = $sfImage->getWidth(); $height = $sfImage->getHeight(); if (isset($image->width) && strpos($image->width, '%') === false) { $width = $image->width; } if (isset($image->height) && strpos($image->height, '%') === false) { $width = $image->height; } $sfImage->setQuality(dmConfig::get('image_resize_quality')); $sfImage->thumbnail($width, $height, dmConfig::get('image_resize_method'), null); $fileSystem = $this->serviceContainer->getService('filesystem'); if (!file_exists(dirname($fileName))) { $fileSystem->mkdir(dirname($fileName)); } $sfImage->saveAs($fileName); chmod($fileName, 0777); } } if (dmConfig::get('mail_template_embed_remote_images_as_attachments', true)) { $image->src = $message->embed(Swift_Image::fromPath($fileName)); } else { $image->src = $this->basePath . str_replace(sfConfig::get('sf_web_dir'), '', $fileName); } return $image; }