FS-Mod/FSMod/SpecialUserImage.php
2021-11-12 19:38:16 +01:00

99 lines
2.4 KiB
PHP

<?php
/**
* Special:StuBU, a special page that implements a StuBu generator form.
* Based on the pdfforms typo3 plugin of Felix B. Holzke.
*
* @file
* @ingroup Extensions
* @author Björn Kinscher
* @copyright © 2009 Björn Kinscher
* @license GNU General Public Licence 2.0 or later
*/
if( !defined( 'MEDIAWIKI' ) ) {
echo( "not a valid entry point.\n" );
die( 1 );
}
/**
* Provides the stubu form
* @ingroup SpecialPage
*/
class SpecialUserImage extends SpecialPage {
/**
* Constructor
*/
public function __construct() {
parent::__construct( 'UserImage' );
}
static function resizeImage($originalImage)
{
list($width, $height) = getimagesize($originalImage);
$new_width = 122;
$new_height = round((122 * $height) / $width);
$imageResized = imagecreatetruecolor($new_width, $new_height);
$imageTmp = imagecreatefromjpeg ($originalImage);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
return $imageResized;
}
static function getCachedImageName($name, $default = null) {
$filename = $default ?? dirname(__FILE__) . '/noimage.jpg';
if ( !preg_match('/^[a-z\.\-]{2,15}$/', $name) ){
return $filename;
}
$data = FSModLdap::perform_search("(uid=$name)");
$image = array_key_exists('jpegphoto', $data[0]) ? $data[0]['jpegphoto'][0] : null;
if ( $image ) {
$filename = dirname(__FILE__). "/cache/" . crc32($image) . ".jpg";
if ( !file_exists($filename) ) {
//not in cache, create cached file
file_put_contents($filename, $image);
$img = self::resizeImage($filename);
imagejpeg($img, $filename, 100);
}
}
return $filename;
}
/**
* Main execution function
*
* @param $par Mixed: Parameters passed to the page
*/
public function execute( $par ) {
$this->checkPermissions();
$out = $this->getOutput();
$request = $this->getRequest();
$name = $request->getVal( 'name' );
if ( null !== $name ) {
$out->disable();
$file = self::getCachedImageName($name);
header('Content-Type: image/jpeg');
readfile($file);
} else {
wfDebug( __METHOD__ . ": form\n" );
$out->addHTML(
"This page shows the corresponding Member Photo when the the url parameter 'name' is set!"
);
}
}
}