Basic class add two new methods

correctImageOrientation: fixes the orientation of a JPEG image with
the exif Orientation header set

uuidv4: creates a uuid v4 string
This commit is contained in:
Clemens Schwaighofer
2019-10-16 18:58:19 +09:00
parent ca073c1b56
commit dfb2a93fbd

View File

@@ -1956,6 +1956,56 @@ class Basic
return $return_data;
}
/**
* reads the rotation info of an file and rotates it to be correctly upright
* this is done because not all software honers the exit Orientation flag
* @param string $filename path + filename to rotate. This file must be writeable
* @return void
*/
public function correctImageOrientation($filename): void
{
if (function_exists('exif_read_data') && is_writeable($filename)) {
list($inc_width, $inc_height, $type) = getimagesize($filename);
$exif = exif_read_data($filename);
$orientation = null;
$img = null;
if ($exif && isset($exif['Orientation'])) {
$orientation = $exif['Orientation'];
}
if ($orientation != 1) {
$this->debug('IMAGE FILE ROTATE', 'Need to rotate image ['.$filename.'] from: '.$orientation);
if ($type == 2) {
// load image to include
$img = imagecreatefromjpeg($filename);
} elseif ($type == 3) {
$img = imagecreatefrompng($filename);
}
$deg = 0;
// 1 top, 6: left, 8: right, 3: bottom
switch ($orientation) {
case 3:
$deg = 180;
break;
case 6:
$deg = -90;
break;
case 8:
$deg = 90;
break;
}
if ($deg && $img !== null) {
$img = imagerotate($img, $deg, 0);
}
// then rewrite the rotated image back to the disk as $filename
if ($type == 2 && $img !== null) {
imagejpeg($img, $filename);
} elseif ($type == 3 && $img !== null) {
imagepng($img, $filename);
}
} // only if we need to rotate
} // function exists & file is writeable, else do nothing
}
/**
* test if a string can be safely convert between encodings. mostly utf8 to shift jis
* the default compare has a possibility of failure, especially with windows
@@ -2117,6 +2167,35 @@ class Basic
return false;
}
/**
* creates psuedo random uuid v4
* Code take from class here:
* https://www.php.net/manual/en/function.uniqid.php#94959
* @return string pseudo random uuid v4
*/
public static function uuidv4(): string
{
return sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
// 16 bits for "time_mid"
mt_rand(0, 0xffff),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand(0, 0x0fff) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand(0, 0x3fff) | 0x8000,
// 48 bits for "node"
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0xffff)
);
}
// [!!! DEPRECATED !!!]
// ALL crypt* methids are DEPRECATED and SHALL NOT BE USED
// use the new password* instead