mirror of
https://github.com/standardebooks/web.git
synced 2025-07-12 09:32:24 -04:00
Add cover art database
Co-authored-by: Job Curtis <job.curtis@gmail.com> Co-authored-by: Alex Cabal <alex@standardebooks.org>
This commit is contained in:
parent
74f747df76
commit
6a5c05511a
92 changed files with 3174 additions and 146 deletions
94
lib/Image.php
Normal file
94
lib/Image.php
Normal file
|
@ -0,0 +1,94 @@
|
|||
<?
|
||||
use function Safe\exec;
|
||||
use function Safe\imagecopyresampled;
|
||||
use function Safe\imagecreatetruecolor;
|
||||
use function Safe\imagejpeg;
|
||||
use function Safe\getimagesize;
|
||||
use function Safe\unlink;
|
||||
|
||||
class Image{
|
||||
public $Path;
|
||||
public $MimeType;
|
||||
|
||||
public function __construct(string $path){
|
||||
$this->Path = $path;
|
||||
$this->MimeType = ImageMimeType::FromFile($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return resource
|
||||
* @throws \Safe\Exceptions\ImageException
|
||||
* @throws Exceptions\InvalidImageUploadException
|
||||
*/
|
||||
private function GetImageHandle(){
|
||||
switch($this->MimeType){
|
||||
case ImageMimeType::JPG:
|
||||
$handle = \Safe\imagecreatefromjpeg($this->Path);
|
||||
break;
|
||||
case ImageMimeType::BMP:
|
||||
$handle = \Safe\imagecreatefrombmp($this->Path);
|
||||
break;
|
||||
case ImageMimeType::PNG:
|
||||
$handle = \Safe\imagecreatefrompng($this->Path);
|
||||
break;
|
||||
case ImageMimeType::TIFF:
|
||||
$handle = $this->GetImageHandleFromTiff();
|
||||
break;
|
||||
default:
|
||||
throw new \Exceptions\InvalidImageUploadException();
|
||||
}
|
||||
|
||||
return $handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return resource
|
||||
* @throws \Exceptions\InvalidImageUploadException
|
||||
*/
|
||||
private function GetImageHandleFromTiff(){
|
||||
$tempFilename = sys_get_temp_dir() . '/se-' . pathinfo($this->Path)['filename'] . '.jpg';
|
||||
|
||||
try{
|
||||
exec('convert '. escapeshellarg($this->Path) . ' ' . escapeshellarg($tempFilename), $shellOutput, $resultCode);
|
||||
|
||||
if($resultCode !== 0){
|
||||
throw new Exceptions\InvalidImageUploadException('Failed to convert TIFF to JPEG');
|
||||
}
|
||||
|
||||
$handle = \Safe\imagecreatefromjpeg($tempFilename);
|
||||
}
|
||||
finally{
|
||||
try{
|
||||
unlink($tempFilename);
|
||||
}
|
||||
catch(Exception){
|
||||
// Pass if file doesn't exist
|
||||
}
|
||||
}
|
||||
|
||||
return $handle;
|
||||
}
|
||||
|
||||
public function Resize(string $destImagePath, int $width, int $height): void{
|
||||
$imageDimensions = getimagesize($this->Path);
|
||||
|
||||
$imageWidth = $imageDimensions[0];
|
||||
$imageHeight = $imageDimensions[1];
|
||||
|
||||
if($imageHeight > $imageWidth){
|
||||
$destinationHeight = $height;
|
||||
$destinationWidth = intval($destinationHeight * ($imageWidth / $imageHeight));
|
||||
}
|
||||
else{
|
||||
$destinationWidth = $width;
|
||||
$destinationHeight = intval($destinationWidth * ($imageHeight / $imageWidth));
|
||||
}
|
||||
|
||||
$srcImageHandle = $this->GetImageHandle();
|
||||
$thumbImageHandle = imagecreatetruecolor($destinationWidth, $destinationHeight);
|
||||
|
||||
imagecopyresampled($thumbImageHandle, $srcImageHandle, 0, 0, 0, 0, $destinationWidth, $destinationHeight, $imageWidth, $imageHeight);
|
||||
|
||||
imagejpeg($thumbImageHandle, $destImagePath);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue