Skip to content

Commit c8e8b51

Browse files
g4b0g4b0
authored andcommitted
API: used call_user_func_array in order to have an arbitrary number of parameter for getFormattedImage (fixes #1752)
1 parent ae09301 commit c8e8b51

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

filesystem/GD.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,6 @@ public static function color_web2gd($image, $webColor) {
351351
*/
352352
public function paddedResize($width, $height, $backgroundColor = "FFFFFF") {
353353
if(!$this->gd) return;
354-
355354
$width = round($width);
356355
$height = round($height);
357356

model/Image.php

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,10 @@ public function generateStripThumbnail(Image_Backend $backend) {
346346
* @param integer $height The height to size to
347347
* @return Image
348348
*/
349-
public function PaddedImage($width, $height) {
349+
public function PaddedImage($width, $height, $backgroundColor=null) {
350350
return $this->isSize($width, $height)
351351
? $this
352-
: $this->getFormattedImage('PaddedImage', $width, $height);
352+
: $this->getFormattedImage('PaddedImage', $width, $height, $backgroundColor);
353353
}
354354

355355
/**
@@ -360,8 +360,8 @@ public function PaddedImage($width, $height) {
360360
* @param integer $height The height to size to
361361
* @return Image_Backend
362362
*/
363-
public function generatePaddedImage(Image_Backend $backend, $width, $height) {
364-
return $backend->paddedResize($width, $height);
363+
public function generatePaddedImage(Image_Backend $backend, $width, $height, $backgroundColor=null) {
364+
return $backend->paddedResize($width, $height, $backgroundColor);
365365
}
366366

367367
/**
@@ -399,17 +399,20 @@ public function isHeight($height) {
399399
* Return an image object representing the image in the given format.
400400
* This image will be generated using generateFormattedImage().
401401
* The generated image is cached, to flush the cache append ?flush=1 to your URL.
402+
*
403+
* Just pass the correct number of parameters expected by the working function
404+
*
402405
* @param string $format The name of the format.
403-
* @param string $arg1 An argument to pass to the generate function.
404-
* @param string $arg2 A second argument to pass to the generate function.
405406
* @return Image_Cached
406407
*/
407-
public function getFormattedImage($format, $arg1 = null, $arg2 = null) {
408+
public function getFormattedImage($format) {
409+
$args = func_get_args();
410+
408411
if($this->ID && $this->Filename && Director::fileExists($this->Filename)) {
409-
$cacheFile = $this->cacheFilename($format, $arg1, $arg2);
410-
412+
$cacheFile = call_user_func_array(array($this, "cacheFilename"), $args);
413+
411414
if(!file_exists(Director::baseFolder()."/".$cacheFile) || isset($_GET['flush'])) {
412-
$this->generateFormattedImage($format, $arg1, $arg2);
415+
call_user_func_array(array($this, "generateFormattedImage"), $args);
413416
}
414417

415418
$cached = new Image_Cached($cacheFile);
@@ -422,14 +425,14 @@ public function getFormattedImage($format, $arg1 = null, $arg2 = null) {
422425
/**
423426
* Return the filename for the cached image, given it's format name and arguments.
424427
* @param string $format The format name.
425-
* @param string $arg1 The first argument passed to the generate function.
426-
* @param string $arg2 The second argument passed to the generate function.
427428
* @return string
428429
*/
429-
public function cacheFilename($format, $arg1 = null, $arg2 = null) {
430+
public function cacheFilename($format) {
431+
$args = func_get_args();
432+
array_shift($args);
430433
$folder = $this->ParentID ? $this->Parent()->Filename : ASSETS_DIR . "/";
431434

432-
$format = $format.$arg1.$arg2;
435+
$format = $format.implode('', $args);
433436

434437
return $folder . "_resampled/$format-" . $this->Name;
435438
}
@@ -440,11 +443,11 @@ public function cacheFilename($format, $arg1 = null, $arg2 = null) {
440443
* using the specific 'generate' method for the specified format.
441444
*
442445
* @param string $format Name of the format to generate.
443-
* @param string $arg1 Argument to pass to the generate method.
444-
* @param string $arg2 A second argument to pass to the generate method.
445446
*/
446-
public function generateFormattedImage($format, $arg1 = null, $arg2 = null) {
447-
$cacheFile = $this->cacheFilename($format, $arg1, $arg2);
447+
public function generateFormattedImage($format) {
448+
$args = func_get_args();
449+
450+
$cacheFile = call_user_func_array(array($this, "cacheFilename"), $args);
448451

449452
$backend = Injector::inst()->createWithArgs(self::$backend, array(
450453
Director::baseFolder()."/" . $this->Filename
@@ -454,7 +457,11 @@ public function generateFormattedImage($format, $arg1 = null, $arg2 = null) {
454457

455458
$generateFunc = "generate$format";
456459
if($this->hasMethod($generateFunc)){
457-
$backend = $this->$generateFunc($backend, $arg1, $arg2);
460+
461+
array_shift($args);
462+
array_unshift($args, $backend);
463+
464+
$backend = call_user_func_array(array($this, $generateFunc), $args);
458465
if($backend){
459466
$backend->writeTo(Director::baseFolder()."/" . $cacheFile);
460467
}

0 commit comments

Comments
 (0)