<?php
abstract class img
{
public static function getcoverpreview($ID,$bookpath,$comicpath)
{
global $_SESSION;
if(DB::Setting("ShowCovers",$_SESSION['UID'])!=1)
{
return false;
}
$fullbookpath = $bookpath . "/$ID.jpg";
if(!file_exists($fullbookpath))
{
$pagelist = img::pagelist($comicpath);
if(isset($pagelist[0]->name))
{
$firstpage = $pagelist[0]['name'];
}
else
{
foreach($pagelist as $key=>$value)
{
$firstpage = $value;
break;
}
}
try{
$fullbookpath = img::extractfrombook($comicpath,$firstpage,$bookpath,"$ID.jpg");
$image = img::coverresize($fullbookpath,100);
imagejpeg($image,$fullbookpath);
}
catch(Exception $ex)
{
return '<img src=\'img/nocover.png\' width = \'100px\'>';
}
}
return "<img src='covers/$ID.jpg' width='100px' />";
}
/**
* Resizes an image if width of image is bigger than the maximum width
* @return array the imageinfo of the resized image
* @param $filename String The path where the image is located
* @param $maxwidth String[optional] The maximum width the image is allowed to be
*/
public static function resize($filename,$maxwidth="1024")
{
$inputfunctions = array('image/jpeg'=>'imagecreatefromjpeg',
'image/png'=>'imagecreatefrompng',
'image/gif'=>'imagecreatefromgif');
$outputfunctions = array('image/jpeg'=>'imagejpeg','image/png'=>'imagepng','image/gif'=>'imagegif');
$imageinfo = getimagesize($filename);
$img = $inputfunctions[$imageinfo['mime']]($filename);
$currentwidth = $imageinfo[0];
$currentheight = $imageinfo[1];
//$mimetype = image_type_to_mime_type(exif_imagetype($file));
if($currentwidth < $maxwidth)
{
return $img;
}
//$img = $inputfunctions[$imageinfo['mime']]($filename);
$newwidth = $maxwidth;
$newheight = ($currentheight/$currentwidth)*$newwidth;
$newimage = imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($newimage,$img,0,0,0,0,$newwidth,$newheight,$currentwidth,$currentheight);
return $newimage;
}
public static function coverresize($filename,$maxwidth="100")
{
$inputfunctions = array('image/jpeg'=>'imagecreatefromjpeg',
'image/png'=>'imagecreatefrompng',
'image/gif'=>'imagecreatefromgif');
$outputfunctions = array('image/jpeg'=>'imagejpeg','image/png'=>'imagepng','image/gif'=>'imagegif');
$imageinfo = getimagesize($filename);
$currentwidth = $imageinfo[0];
$currentheight = $imageinfo[1];
$img = $inputfunctions[$imageinfo['mime']]($filename);
$newwidth = $maxwidth;
$newheight = $maxheight;
$newheight = ($currentheight/($currentwidth))*$newwidth;
if($currentwidth / $currentheight > 0.68)
{
//accomodate for crop
$hwidth = $currentheight *0.68;
$newheight = ($currentheight/$hwidth)*($newwidth);
$newimage = imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($newimage,$img,0,0,$currentwidth-$hwidth,0,$newwidth,$newheight,$hwidth,$currentheight);
}
else
{
$newimage = imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($newimage,$img,0,0,0,0,$newwidth,$newheight,$currentwidth,$currentheight);
}
return $newimage;
}
public static function pagelist($filename)
{
if ((preg_match('/cbr$/i',$filename)) || (preg_match('/rar$/i',$filename)))
{
$rar_file = rar_open($filepath.$filename);
$list = rar_list($rar_file);
sort($list);
$new = array();
foreach ($list as $file)
{
if (($file->unpacked_size > 0) && preg_match('/jpg|gif|png/i',$file->name))
{
$new[$file->name] = $file->name;
//unset($list->$file);
}
}
return $new;
}
elseif ((preg_match('/cbz$/i',$filename))||(preg_match('/zip$/i',$filename)))
{
$zip = new ZipArchive();
$zip->open($filename) or die("cannot open $filename!\n");
if($zip->numFiles != 0)
{
$filelist = array();
for ($i = 0; $i < $zip->numFiles; $i++) {
$entry = $zip->statIndex($i);
if (preg_match('/(jpg|png|gif)$/i',$entry['name']))
{
$filelist[$entry['name']] = $i;
}
}
ksort($filelist);
$zip->close();
return $filelist;
}
}
return array();
}
public static function extractfrombook($comicname,$pagename,$extracttopath,$extracttofile = NULL)
{
if($extracttofile == NULL)
{
$extracttofile = $pagename;
}
$extractto = $extracttopath . '/' . rawurlencode($extracttofile);
if ((preg_match('/cbr$/i',$comicname))||(preg_match('/rar$/i',$comicname)))
{
$rar_file = $pagename;
if(!rar_open($comicname))
{
throw new Exception();
}
$rar_file = rar_open($comicname);
$entry = rar_entry_get($rar_file,$pagename);
$entry->extract(false,$extractto);
rar_close($rar_file);
}
elseif ((preg_match('/cbz$/i',$comicname))||(preg_match('/zip$/i',$comicname)))
{
$index = $pagename;
$zip = new ZipArchive();
if ($zip->open($comicname) === TRUE)
{
$entry = $zip->statIndex($index);
file_put_contents($extractto,$zip->getFromIndex($index));
$zip->close();
}
}
else
{
return false;
}
return $extractto;
}
}
?>