BaseFrontendClass.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace app\models\base;
  3. use yii\db\ActiveRecord;
  4. use yii\helpers\Url;
  5. use app\models\Person;
  6. use app\models\base\Tags;
  7. use app\models\base\Story;
  8. use app\models\base\NewsTopic;
  9. class BaseFrontendClass extends ActiveRecord
  10. {
  11. public static function getDb()
  12. {
  13. return \Yii::$app->db;
  14. }
  15. public function getUrl($full = false):string
  16. {
  17. $base = Url::base('https');
  18. $page = \Yii::$app->request->get('page');
  19. $addpage = $page?'?page='.$page:'';
  20. if( $this instanceof NewsTopic || $this instanceof Story || $this instanceof Tags ){
  21. $alias = trim( $this->url );
  22. }else if($this instanceof News){
  23. $alias = trim( $this->alias );
  24. return ($full ? $base : "")."/news/".$alias."-".$this->id;
  25. }else if( $this instanceof Person ){
  26. $alias = trim( $this->alias );
  27. return ($full ? $base : "")."/person/".$alias;
  28. }else{
  29. return false;
  30. }
  31. if( $this instanceof Tags) return ($full ? $base : "")."/tags/".$alias.$addpage;
  32. return ($full ? $base : "")."/news/".$alias.$addpage;
  33. }
  34. public function generatePreview( $ifile, $ofile, $w, $h, $param = array())
  35. {
  36. extract( $param );
  37. $addp = '';
  38. if( strrpos($ofile, '.webp') == strlen($ofile) - strlen('.webp') )
  39. {
  40. $addp = " -background '#FFFFFF' -alpha off -strip Plane -quality 88 -define webp:method=6 "; //-interlace
  41. }else{
  42. $addp = " -quality 95";
  43. }
  44. if(isset($sharpen))
  45. {
  46. $addp = $addp." -sharpen ".$sharpen;
  47. }
  48. list($width, $height) = getimagesize($ifile);
  49. // echo "convert -define jpeg:size=".$w."x".$h."+".$x."+".$y." $ifile -crop ".$w."x".$h."+".$x."+".$y." -thumbnail ".$ww."x".$hh."^ -gravity center $ofile";
  50. umask(0113);
  51. exec("convert -define jpeg:size=".$width."x".$height."+0+0 $ifile -thumbnail ".$w."x".$h."^ -gravity center -background '#FFFFFF' $addp $ofile" );
  52. @chmod($ofile, 0664);
  53. }
  54. public function generateImageCrop( $ifile, $ofile, $ww, $hh, $param )
  55. {
  56. // to do add rotate, etc
  57. extract( $param );
  58. list($width, $height) = getimagesize($ifile);
  59. $addp = '';
  60. if( strrpos($ofile, '.webp') == strlen($ofile) - strlen('.webp') )
  61. {
  62. $addp = " -background '#FFFFFF' -alpha off -strip Plane -quality 88 -define webp:method=6 "; //-interlace
  63. }
  64. if(isset($sharpen))
  65. {
  66. $addp = $addp." -sharpen ".$sharpen;
  67. }
  68. umask(0113);
  69. // echo "convert -define jpeg:size=".$w."x".$h."+".$x."+".$y." $ifile -crop ".$w."x".$h."+".$x."+".$y." -thumbnail ".$ww."x".$hh."^ -gravity center $ofile";
  70. exec("convert -define jpeg:size=".$width."x".$height."+0+0 $ifile -crop ".$w."x".$h."+".$x."+".$y." -thumbnail ".$ww."x".$hh."^ $addp -gravity center $ofile" ); //> /dev/null 2>&1 & > /dev/null & в фоне
  71. // chmod($ofile, 0664);
  72. }
  73. /**
  74. * {@inheritdoc}
  75. * @return status
  76. */
  77. public function clearNewsSTG($id)
  78. {
  79. $status = $this->db->createCommand()->delete(self::tableName(), [
  80. 'news_id' => $id
  81. ])->execute();
  82. return $status;
  83. }
  84. }