Skip to content
Snippets Groups Projects
Commit bcc789f7 authored by Ghislain Loas's avatar Ghislain Loas
Browse files

hotline #76959 fix file-manager thumbnailing

parent 3c29de92
Branches
Tags
2 merge requests!2746Hotline,!2727Hotline#76959 vignettes manquantes dans kiosque notices
Pipeline #4558 failed with stage
in 54 minutes and 21 seconds
- ticket #76959 : Vignettes manquantes dans kiosque notices
\ No newline at end of file
- ticket #76959 : Explorateur de fichier : correction du vignettage d'images.
......@@ -43,7 +43,6 @@ class Class_FileWriter {
public function mkdir($directory_name) {
return mkdir($directory_name,0777,true);
}
......@@ -56,6 +55,8 @@ class Class_FileWriter {
return move_uploaded_file($source,$destination);
}
}
?>
\ No newline at end of file
public function unlink($path) {
return unlink($path);
}
}
\ No newline at end of file
......@@ -21,7 +21,10 @@
class Class_Notice_Thumbnail_ResizeImage {
use Trait_StaticFileWriter, Trait_Translator;
protected static $_default_image_factory;
protected $_image;
public static function setDefaultImageFactory($factory) {
......@@ -36,27 +39,60 @@ class Class_Notice_Thumbnail_ResizeImage {
}
public function resize($source, $width, $height, $into = '', $unlink = false, $bestfit = true) {
public function __construct($image) {
$this->_image = $image;
}
public function resize($width, $height, $unlink = false, $bestfit = true) {
if (!$this->_image || !$this->_image->isImage())
return new Class_Entity(['Url' => '']);
$pattern = 'temp/thumbnails/file-manager/%sx%s';
$thumb_path_pattern = sprintf($pattern, $width, $height);
$thumb_path = str_replace($this->_image->getParentPath(),
$thumb_path_pattern,
$this->_image->getPath());
$thumb_parent_path = explode('/', $thumb_path);
array_pop($thumb_parent_path);
$thumb_parent_path = implode('/', $thumb_parent_path);
$thumb_url = Class_Url::absolute(sprintf($pattern . '/%s',
$width,
$height,
$this->_image->getName()));
if (@$this->getFileWriter()->fileExists($thumb_path))
return new Class_Entity(['Url' => $thumb_url,
'Alt' => $this->_('Vignette de l\'image "%s"', $this->_image->getName())]);
if (!@$this->getFileWriter()->fileExists($thumb_parent_path))
@$this->getFileWriter()->mkdir($thumb_parent_path, 0755, true);
try {
$image = static::getImageFactory($source);
$image = static::getImageFactory($this->_image->getPath());
$image->scaleImage($width, $height, $bestfit);
$image->setImageBackgroundColor('none');
$image->setImageCompressionQuality(Class_AdminVar::get('IMAGICK_IMAGE_COMPRESSION_QUALITY'));
$image->setSamplingFactors(explode(';', Class_AdminVar::get('IMAGICK_SAMPLING_FACTORS')));
$into = $into
? $into
: $source;
$into = $thumb_path;
if($unlink)
@unlink($into);
@$this->getFileWriter()->unlink($into);
$image->writeImage($into);
$image->destroy();
} catch(Exception $e) {
return false;
return new Class_Entity(['Url' => $thumb_url,
'Title' => $this->_('Erreur lors du vignettage de l\'image "%s". %s', $this->_image->getName(),
$e->getMessage())]);
}
return true;
return new Class_Entity(['Url' => $thumb_url,
'Alt' => $this->_('Vignette de l\'image "%s"', $this->_image->getName())]);
}
}
\ No newline at end of file
......@@ -95,7 +95,7 @@ class Class_Url {
return false;
if('/' === substr($string, 0, 1))
return true;
return false;
return 'http' === substr($string, 0, 4);
}
......
......@@ -21,39 +21,14 @@
class ZendAfi_View_Helper_Thumbnail extends ZendAfi_View_Helper_BaseHelper {
use Trait_StaticFileWriter;
public function thumbnail($instance, $width = 160, $height = 150) {
if(!$instance->isImage())
return '';
$resized_image = (new Class_Notice_Thumbnail_ResizeImage($instance))
->resize($width, $height);
try {
$thumb_path_pattern = sprintf('temp/thumbnails/file-manager/%sx%s', $width, $height);
$thumb_path = str_replace($instance->getParentPath(),
$thumb_path_pattern,
$instance->getPath());
$thumb_parent_path = explode('/', $thumb_path);
array_pop($thumb_parent_path);
$thumb_parent_path = implode('/', $thumb_parent_path);
$thumb_url = Class_Url::absolute(str_replace($instance->getParentPath(),
$thumb_path_pattern,
$instance->getId()));
if($this->getFileWriter()->fileExists($thumb_path))
return $this->view->tagImg($thumb_url);
if(!$this->getFileWriter()->fileExists($thumb_parent_path))
$this->getFileWriter()->mkdir($thumb_parent_path, 0755, true);
if(!(new Class_Notice_Thumbnail_ResizeImage)->resize($instance->getPath(), $width, $height, $thumb_path))
return '';
return $this->view->tagImg($thumb_url);
} catch (Exception $e) {
return '';
}
return ($url = $resized_image->getUrl())
? $this->view->tagImg($url, ['title' => $resized_image->getTitle(),
'alt' => $resized_image->getAlt()])
: '';
}
}
?>
\ No newline at end of file
}
\ No newline at end of file
<?php
/**
* Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class ZendAfi_View_HelperThumbnailImageTest extends ViewHelperTestCase {
public function setUp() {
parent::setUp();
$this->_helper = new ZendAfi_View_Helper_Thumbnail();
$this->_helper->setView($this->view);
Class_FileManager::beOpenBar();
$image = Class_FileManager::find(__DIR__ . '/image.png');
$this->_html = $this->_helper->thumbnail($image);
}
/** @test */
public function htmlShouldContainsTagImgWithTempUrl() {
$this->assertXpath($this->_html, '//img[contains(@src, "/temp/thumbnails/file-manager/160x150/image.png")]', $this->_html);
}
}
\ No newline at end of file
tests/library/ZendAfi/View/Helper/image.png

764 B

0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment