3 use wcf\system\exception\SystemException
;
4 use wcf\system\image\ImageHandler
;
7 * Contains image-related functions.
10 * @copyright 2001-2019 WoltLab GmbH
11 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
12 * @package WoltLabSuite\Core\Util
14 final class ImageUtil
{
19 protected static $imageExtensions = ['jpeg', 'jpg', 'png', 'gif'];
22 * Checks the content of an image for bad sections, e.g. the use of javascript
23 * and returns false if any bad stuff was found.
28 public static function checkImageContent($file) {
30 $content = file_get_contents($file);
32 // remove some characters
33 $content = strtolower(preg_replace('/[^a-z0-9<\(]+/i', '', $content));
34 $content = str_replace('description', '', $content);
36 // search for javascript
37 if (strpos($content, 'script') !== false ||
strpos($content, 'javascript') !== false ||
strpos($content, 'expression(') !== false) return false;
43 * Checks whether a given file is a valid image.
45 * @param string $location
46 * @param string|null $filename
47 * @param bool $handleSvgAsValidImage flag, whether a svg file is handled as image
50 public static function isImage($location, $filename = null, $handleSvgAsValidImage = false) {
51 if ($filename === null) {
52 $filename = basename($location);
55 if (@getimagesize
($location) !== false) {
56 $extension = pathinfo($filename, PATHINFO_EXTENSION
);
58 if (in_array(mb_strtolower($extension), self
::$imageExtensions)) {
62 else if ($handleSvgAsValidImage) {
63 if (in_array(FileUtil
::getMimeType($location), ['image/svg', 'image/svg+xml']) && mb_strtolower(pathinfo($filename, PATHINFO_EXTENSION
)) === 'svg') {
72 * Return the file extension for an image with the given mime type.
74 * @param string $mimeType
76 * @see http://www.php.net/manual/en/function.image-type-to-mime-type.php
78 public static function getExtensionByMimeType($mimeType) {
86 case 'application/x-shockwave-flash':
91 case 'image/x-ms-bmp':
101 * Enforces dimensions for given image.
103 * @param string $filename
104 * @param integer $maxWidth
105 * @param integer $maxHeight
106 * @param boolean $obtainDimensions
107 * @return string new filename if file was changed, otherwise old filename
110 public static function enforceDimensions($filename, $maxWidth, $maxHeight, $obtainDimensions = true) {
111 $imageData = getimagesize($filename);
112 if ($imageData[0] > $maxWidth ||
$imageData[1] > $maxHeight) {
113 $adapter = ImageHandler
::getInstance()->getAdapter();
114 $adapter->loadFile($filename);
115 $filename = FileUtil
::getTemporaryFilename();
116 $thumbnail = $adapter->createThumbnail($maxWidth, $maxHeight, $obtainDimensions);
117 $adapter->writeImage($thumbnail, $filename);
124 * Rotates the given image based on the orientation stored in the exif data.
126 * @param string $filename
127 * @return string new filename if file was changed, otherwise old filename
130 public static function fixOrientation($filename) {
132 $exifData = ExifUtil
::getExifData($filename);
133 if (!empty($exifData)) {
134 $orientation = ExifUtil
::getOrientation($exifData);
135 if ($orientation != ExifUtil
::ORIENTATION_ORIGINAL
) {
136 $adapter = ImageHandler
::getInstance()->getAdapter();
137 $adapter->loadFile($filename);
140 switch ($orientation) {
141 case ExifUtil
::ORIENTATION_180_ROTATE
:
142 $newImage = $adapter->rotate(180);
145 case ExifUtil
::ORIENTATION_90_ROTATE
:
146 $newImage = $adapter->rotate(90);
149 case ExifUtil
::ORIENTATION_270_ROTATE
:
150 $newImage = $adapter->rotate(270);
153 case ExifUtil
::ORIENTATION_HORIZONTAL_FLIP
:
154 case ExifUtil
::ORIENTATION_VERTICAL_FLIP
:
155 case ExifUtil
::ORIENTATION_VERTICAL_FLIP_270_ROTATE
:
156 case ExifUtil
::ORIENTATION_HORIZONTAL_FLIP_270_ROTATE
:
161 if ($newImage !== null) {
162 if ($newImage instanceof \Imagick
) {
163 $newImage->setImageOrientation(\Imagick
::ORIENTATION_TOPLEFT
);
166 $adapter->load($newImage, $adapter->getType());
169 $newFilename = FileUtil
::getTemporaryFilename();
170 $adapter->writeImage($newFilename);
171 $filename = $newFilename;
175 catch (SystemException
$e) {}
181 * Forbid creation of ImageUtil objects.
183 private function __construct() {