Initial import
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace app\models;
|
||||
|
||||
use app\services\IdServer;
|
||||
use yii\db\Exception;
|
||||
|
||||
/**
|
||||
* Class RegisterObjectFiles
|
||||
* @package app\models
|
||||
* @property $file_url
|
||||
* @property $streaming_url
|
||||
* @property $extension
|
||||
* @property $size
|
||||
* @property $file_ref_num
|
||||
* @property $object_id
|
||||
* @property $video_thumbnail
|
||||
* @property $video_title
|
||||
* @property $file_content_type
|
||||
* @property $video_duration
|
||||
*/
|
||||
class RegisterObjectFiles extends _Base
|
||||
{
|
||||
public function getObject()
|
||||
{
|
||||
return $this->hasOne(RegisterObjects::class, ['id' => 'object_id']);
|
||||
}
|
||||
|
||||
public function videoThumbnail()
|
||||
{
|
||||
if ($this->video_thumbnail)
|
||||
return IdServer::getVideoThumb($this->id);
|
||||
return '/_public/images/empty.png';
|
||||
}
|
||||
|
||||
public function getResizedImg($direct = false, $wm = false)
|
||||
{
|
||||
// if ($direct)
|
||||
//return IdServer::getImgToResize($this->id);
|
||||
$file = $_SERVER['DOCUMENT_ROOT'] . '/_files/thumbnails/' . $this->id . '.jpg';
|
||||
if (!file_exists($file)) {
|
||||
$delivery = $this->file_url; //IdServer::getImgToResize($this->id);
|
||||
//$headers = @get_headers($delivery);
|
||||
//if ($headers && strpos($headers[0], '200')) {
|
||||
$this->compressImage($delivery, $file, 70, $wm);
|
||||
//x}
|
||||
}
|
||||
return \Yii::$app->params['portal'] . '/_files/thumbnails/' . $this->id . '.jpg';
|
||||
}
|
||||
|
||||
public function getCopyImg($object_dir)
|
||||
{
|
||||
$inputFile = $this->file_url;
|
||||
$outputFile = $object_dir . '/' . $this->id . '.jpg';
|
||||
if (!file_exists($outputFile))
|
||||
$this->copyImage($inputFile, $outputFile);
|
||||
return $outputFile;
|
||||
}
|
||||
|
||||
private function compressImage($tempPath, $originalPath, $imageQuality, $waterMark = false)
|
||||
{
|
||||
// Get image info
|
||||
$imgInfo = getimagesize($tempPath);
|
||||
$mime = $imgInfo['mime'];
|
||||
|
||||
// Create a new image from file
|
||||
switch ($mime) {
|
||||
case 'image/jpeg':
|
||||
$image = imagecreatefromjpeg($tempPath);
|
||||
break;
|
||||
case 'image/png':
|
||||
$image = imagecreatefrompng($tempPath);
|
||||
break;
|
||||
case 'image/gif':
|
||||
$image = imagecreatefromgif($tempPath);
|
||||
break;
|
||||
default:
|
||||
$image = imagecreatefromjpeg($tempPath);
|
||||
}
|
||||
|
||||
$imgResized = imagescale($image, 350);
|
||||
|
||||
if ($waterMark) {
|
||||
$stamp = imagecreatefrompng($_SERVER['DOCUMENT_ROOT'] . '/_public/images/watermark.png');
|
||||
imagecopy($imgResized, $stamp, 75, 0, 0, 0, 200, 200);
|
||||
}
|
||||
|
||||
// Save image
|
||||
imagejpeg($imgResized, $originalPath, $imageQuality);
|
||||
imagedestroy($imgResized);
|
||||
// Return compressed image
|
||||
return $originalPath;
|
||||
}
|
||||
|
||||
private function copyImage($tempPath, $originalPath)
|
||||
{
|
||||
// Get image info
|
||||
$imgInfo = getimagesize($tempPath);
|
||||
$mime = $imgInfo['mime'];
|
||||
|
||||
// Create a new image from file
|
||||
switch ($mime) {
|
||||
case 'image/jpeg':
|
||||
$image = imagecreatefromjpeg($tempPath);
|
||||
break;
|
||||
case 'image/png':
|
||||
$image = imagecreatefrompng($tempPath);
|
||||
break;
|
||||
case 'image/gif':
|
||||
$image = imagecreatefromgif($tempPath);
|
||||
break;
|
||||
default:
|
||||
$image = imagecreatefromjpeg($tempPath);
|
||||
}
|
||||
|
||||
|
||||
// Save image
|
||||
imagejpeg($image, $originalPath, 100);
|
||||
imagedestroy($image);
|
||||
// Return compressed image
|
||||
return $originalPath;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user