Skip to content

Commit

Permalink
Unnamespaced \image* functions, added accepts attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesread committed Aug 23, 2023
1 parent 24588c7 commit 631ae2d
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions src/main/php/libAllure/ElementFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class ElementFile extends Element
{
private $imageResource = null;

public $acceptsFilter = "";

public $isImage = true;
public $destinationDir = '/tmp/';
public $destinationFilename = 'unnamed';
Expand Down Expand Up @@ -83,38 +85,38 @@ private function validateImage()
$type = exif_imagetype($this->tempName);

if ($type == IMAGETYPE_JPEG) {
$this->imageResource = imagecreatefromjpeg($this->tempName);
$this->imageResource = \imagecreatefromjpeg($this->tempName);
} elseif ($type == IMAGETYPE_GIF) {
$this->imageResource = imagecreatefromgif($this->tempName);
$this->imageResource = \imagecreatefromgif($this->tempName);
} elseif ($type == IMAGETYPE_PNG) {
$this->imageResource = imagecreatefrompng($this->tempName);
$this->imageResource = \imagecreatefrompng($this->tempName);
} else {
$this->setValidationError("Unsupported file type.");
return;
}

if (imagesx($this->imageResource) > $this->imageMaxW || imagesy($this->imageResource) > $this->imageMaxH) {
if (\imagesx($this->imageResource) > $this->imageMaxW || \imagesy($this->imageResource) > $this->imageMaxH) {
if ($this->autoResize) {
$this->imageResource = self::resizeImage($this->imageResource, $this->imageMaxW, $this->imageMaxH);
} else {
$this->setValidationError('Image too big, images may up to ' . $this->imageMaxW . 'x' . $this->imageMaxH . ' pixels, that was ' . imagesx($this->imageResource) . 'x' . imagesy($this->imageResource) . ' pixels.');
$this->setValidationError('Image too big, images may up to ' . $this->imageMaxW . 'x' . $this->imageMaxH . ' pixels, that was ' . \imagesx($this->imageResource) . 'x' . \imagesy($this->imageResource) . ' pixels.');
}
}
}

public static function resizeImage($srcImage, $finW, $finH)
{
$imageResized = imagecreatetruecolor($finW, $finH);
imagealphablending($srcImage, false);
imageinterlace($srcImage, true);
$imageResized = \imagecreatetruecolor($finW, $finH);
\imagealphablending($srcImage, false);
\imageinterlace($srcImage, true);

imagealphablending($imageResized, false);
imageinterlace($imageResized, true);
\imagealphablending($imageResized, false);
\imageinterlace($imageResized, true);

$srcX = 0;
$srcY = 0;
$srcW = imagesx($srcImage);
$srcH = imagesy($srcImage);
$srcW = \imagesx($srcImage);
$srcH = \imagesy($srcImage);

$offsetX = 0;
$offsetY = 0;
Expand All @@ -132,10 +134,10 @@ public static function resizeImage($srcImage, $finW, $finH)
$offsetY = ($finH - $dstH) / 2;
}

$backgroundColor = imagecolorallocate($imageResized, 255, 255, 255);
imagefill($imageResized, 0, 0, $backgroundColor);
$backgroundColor = \imagecolorallocate($imageResized, 255, 255, 255);
\imagefill($imageResized, 0, 0, $backgroundColor);

imagecopyresampled($imageResized, $srcImage, $offsetX, $offsetY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH);
\imagecopyresampled($imageResized, $srcImage, $offsetX, $offsetY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH);

return $imageResized;
}
Expand All @@ -155,9 +157,8 @@ public function saveJpg()
if (!$this->wasAnythingUploaded()) {
return;
}
echo 'saving to ' . $this->destinationDir . $this->destinationFilename;

imagejpeg($this->imageResource, $this->destinationDir . DIRECTORY_SEPARATOR . $this->destinationFilename);
\imagejpeg($this->imageResource, $this->destinationDir . DIRECTORY_SEPARATOR . $this->destinationFilename);
}

public function savePng()
Expand All @@ -166,13 +167,13 @@ public function savePng()
return;
}

imagealphablending($this->imageResource, true);
imagesavealpha($this->imageResource, true);
imagepng($this->imageResource, $this->destinationDir . DIRECTORY_SEPARATOR . $this->destinationFilename);
\imagealphablending($this->imageResource, true);
\imagesavealpha($this->imageResource, true);
\imagepng($this->imageResource, $this->destinationDir . DIRECTORY_SEPARATOR . $this->destinationFilename);
}

public function render()
{
return sprintf('<input name = "%s" type = "file" />', $this->name);
return sprintf('<input name = "%s" accept = "%s" type = "file" />', $this->name, $this->acceptsFilter);
}
}

0 comments on commit 631ae2d

Please sign in to comment.