Skip to content

Commit

Permalink
pkp/pkp-lib#9315 Disallow SVGs
Browse files Browse the repository at this point in the history
  • Loading branch information
asmecher committed Feb 1, 2025
1 parent ad6b507 commit d3028e4
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions classes/publication/Repository.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @file classes/publication/Repository.php
*
Expand Down Expand Up @@ -439,40 +440,36 @@ public function makeThumbnail($filePath, $thumbFileName, $maxWidth, $maxHeight)
break;
case 'webp': $cover = imagecreatefromwebp($filePath);
break;
case 'svg': $cover = copy($filePath, $thumbFilePath);
break;
}
if (!isset($cover)) {
throw new Exception('Can not build thumbnail because the file was not found or the file extension was not recognized.');
}

if ($pathParts['extension'] != 'svg') {
// Calculate the scaling ratio for each dimension.
$originalSizeArray = getimagesize($filePath);
$xRatio = min(1, $maxWidth / $originalSizeArray[0]);
$yRatio = min(1, $maxHeight / $originalSizeArray[1]);

// Choose the smallest ratio and create the target.
$ratio = min($xRatio, $yRatio);

$thumbWidth = round($ratio * $originalSizeArray[0]);
$thumbHeight = round($ratio * $originalSizeArray[1]);
$thumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresampled($thumb, $cover, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $originalSizeArray[0], $originalSizeArray[1]);

switch ($pathParts['extension']) {
case 'jpg': imagejpeg($thumb, $pathParts['dirname'] . '/' . $thumbFileName);
break;
case 'png': imagepng($thumb, $pathParts['dirname'] . '/' . $thumbFileName);
break;
case 'gif': imagegif($thumb, $pathParts['dirname'] . '/' . $thumbFileName);
break;
case 'webp': imagewebp($thumb, $thumbFilePath);
break;
}
// Calculate the scaling ratio for each dimension.
$originalSizeArray = getimagesize($filePath);
$xRatio = min(1, $maxWidth / $originalSizeArray[0]);
$yRatio = min(1, $maxHeight / $originalSizeArray[1]);

imagedestroy($thumb);
// Choose the smallest ratio and create the target.
$ratio = min($xRatio, $yRatio);

$thumbWidth = round($ratio * $originalSizeArray[0]);
$thumbHeight = round($ratio * $originalSizeArray[1]);
$thumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresampled($thumb, $cover, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $originalSizeArray[0], $originalSizeArray[1]);

switch ($pathParts['extension']) {
case 'jpg': imagejpeg($thumb, $pathParts['dirname'] . '/' . $thumbFileName);
break;
case 'png': imagepng($thumb, $pathParts['dirname'] . '/' . $thumbFileName);
break;
case 'gif': imagegif($thumb, $pathParts['dirname'] . '/' . $thumbFileName);
break;
case 'webp': imagewebp($thumb, $thumbFilePath);
break;
}

imagedestroy($thumb);
}

/**
Expand Down

0 comments on commit d3028e4

Please sign in to comment.