src/Entity/Media.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  5. use Symfony\Component\HttpFoundation\File\File as HttpFile;
  6. /**
  7.  * @ORM\Entity(repositoryClass="App\Repository\MediaRepository")
  8.  * @Vich\Uploadable
  9.  */
  10. class Media
  11. {
  12.     /**
  13.      * @ORM\Id()
  14.      * @ORM\GeneratedValue()
  15.      * @ORM\Column(type="bigint")
  16.      */
  17.     private $id;
  18.     
  19.     /**
  20.      * @ORM\Version @ORM\Column(type="integer")
  21.      */
  22.     private $version;
  23.     /**
  24.      * @ORM\Column(type="string", length=255)
  25.      */
  26.     private $title;
  27.     /**
  28.      * @ORM\Column(type="text")
  29.      */
  30.     private $description;
  31.     
  32.     /**
  33.      * @ORM\Column(type="string", length=125)
  34.      */
  35.     private $alt_tag;
  36.     /**
  37.      * @ORM\Column(type="string", length=16)
  38.      */
  39.     private $extension;
  40.     /**
  41.      * @Vich\UploadableField(mapping="media_files", fileNameProperty="name")
  42.      * @var File
  43.      */
  44.     private $data;
  45.     
  46.     /**
  47.      * @ORM\Column(type="string", length=255)
  48.      */
  49.     private $name;
  50.     
  51.     /**
  52.      * @ORM\Column(type="string", length=50)
  53.      */
  54.     private $type;
  55.     /**
  56.      * @ORM\Column(type="integer")
  57.      */
  58.     private $status;
  59.     /**
  60.      * @ORM\Column(type="datetime")
  61.      */
  62.     private $modified_at;
  63.     /**
  64.      * @ORM\Column(type="datetime")
  65.      */
  66.     private $created_at;
  67.     /**
  68.      * @ORM\Column(type="bigint")
  69.      */
  70.     private $old_id;
  71.     /**
  72.      * @ O R M\Column(type="string", length=255)
  73.      * /
  74.     private $sub_directory;
  75.     */
  76.     
  77.     public function __construct ()
  78.     {
  79.         $this->title "";
  80.         $this->alt_tag "";
  81.         $this->extension "";
  82.         $this->name "";
  83.         $this->type "";
  84.         $this->status "1";
  85.         $this->description "";
  86.         $this->status 1;
  87.         $this->old_id 0;
  88.         
  89.         // $this->sub_directory = "";
  90.         
  91.         $this->modified_at = new \DateTime("now");
  92.         $this->created_at = new \DateTime("now");
  93.     }
  94.     public function getId()
  95.     {
  96.         return $this->id;
  97.     }
  98.     
  99.     public function getVersion() {
  100.         return $this->version;
  101.     }
  102.     
  103.     public function getTitle(): ?string
  104.     {
  105.         return $this->title;
  106.     }
  107.     public function setTitle(string $title null): self
  108.     {
  109.         $this->title $title $title "";
  110.         return $this;
  111.     }
  112.     public function getDescription(): ?string
  113.     {
  114.         return $this->description;
  115.     }
  116.     public function setDescription(string $description null): self
  117.     {
  118.         $this->description $description $description "";
  119.         return $this;
  120.     }
  121.     
  122.     public function getAltTag(): ?string
  123.     {
  124.         return $this->alt_tag;
  125.     }
  126.     public function setAltTag(string $alt_tag null): self
  127.     {
  128.         $this->alt_tag $alt_tag $alt_tag "";
  129.         return $this;
  130.     }
  131.     
  132.     public function getAltTagText(): ?string
  133.     {
  134.         return $this->alt_tag $this->alt_tag $this->title;
  135.     }
  136.     public function getExtension(): ?string
  137.     {
  138.         return $this->extension;
  139.     }
  140.     public function setExtension(string $extension null): self
  141.     {
  142.         $this->extension $extension $extension "";
  143.         return $this;
  144.     }
  145.     public function getData(): ?HttpFile
  146.     {
  147.         return $this->data;
  148.     }
  149.     public function setData(HttpFile $data null): self
  150.     {
  151.         $this->data $data;
  152.         
  153.         if ($data) {
  154.             $this->extension $data->guessExtension();
  155.             // $this->extension = $data->getExtension();
  156.             $this->type $data->getMimeType();
  157.             $this->modified_at = new \DateTime("now");
  158.             
  159.             $nameArray explode('/'$data);
  160.             $nameArray array_slice($nameArray, -33true);
  161.             $this->name implode('/'$nameArray);
  162.             
  163.             if ($this->type == 'image/heif' || $this->type == 'application/octet-stream') {
  164.                 //return null;
  165.             }
  166.             
  167.             //Exif Rotation Fix
  168.             try {
  169.                 $file $data;
  170.                 if (is_file($file) && exif_imagetype($file)) {
  171.             
  172.                     $type exif_imagetype($file);
  173.                     switch($type) {
  174.                         case 1:
  175.                             $image imagecreatefromgif($file);
  176.                             break;
  177.                         case 2:
  178.                             $image imagecreatefromjpeg($file);
  179.                             break;
  180.                         case 3:
  181.                             $image imagecreatefrompng($file);
  182.                             break;
  183.                         default:
  184.                             $image 0;
  185.                     }
  186.                     
  187.                     if($image) {
  188.                         $exif exif_read_data($file);
  189.                         
  190.                         if (!empty($exif['Orientation'])) {
  191.                             
  192.                             switch ($exif['Orientation']) {
  193.                                 case 3:
  194.                                     $image imagerotate($image1800);
  195.                                     break;
  196.                                     
  197.                                 case 6:
  198.                                     $image imagerotate($image, -900);
  199.                                     break;
  200.                                     
  201.                                 case 8:
  202.                                     $image imagerotate($image900);
  203.                                     break;
  204.                             }
  205.                             
  206.                             switch($type) {
  207.                                 case 1:
  208.                                     imagegif($image$this->name);
  209.                                     break;
  210.                                 
  211.                                 case 2:
  212.                                     imagejpeg($image$this->name);
  213.                                     break;
  214.                                 
  215.                                 case 3:
  216.                                     imagepng($image$this->name);
  217.                                     break;
  218.                             }
  219.                         }
  220.                     }
  221.                 }
  222.             } catch (\Exception $e) {
  223.                 //usually it's erroring on the exif_read_data function if the file is not supported
  224.                 //do nothing, the image should be fine
  225.                 
  226.                 
  227.             }
  228.             
  229.         }
  230.         return $this;
  231.     }
  232.     
  233.     public function removeFile ()
  234.     {
  235.         if ($this->getName()) {
  236.             $file dirname(dirname(dirname(__FILE__)))."/public/uploads/media/" $this->getName();
  237.             if (is_file($file)) {
  238.                 unlink ($file);
  239.                 $this->name null;
  240.                 $this->data null;
  241.             }
  242.         }
  243.         return $this;
  244.     }
  245.     public function getName(): ?string
  246.     {
  247.         /*
  248.         if ($this->name && $this->sub_directory && 
  249.             strpos($this->name, $this->sub_directory) === false
  250.         ) {
  251.             return $this->sub_directory.$this->name;
  252.         }
  253.         */
  254.         return $this->name;
  255.     }
  256.     public function setName(string $name null): self
  257.     {
  258.         $this->name $name $name null;
  259.         return $this;
  260.     }
  261.     
  262.     public function getURL ()
  263.     {
  264.         if(!$this->getName()) {
  265.             return "";
  266.         }
  267.         
  268.         return "/uploads/media/{$this->name}";
  269.     }
  270.     
  271.     public function getAbsoluteURL ()
  272.     {
  273.         if(!$this->getName()) {
  274.             return "";
  275.         }
  276.         
  277.         return "https://" $_SERVER['SERVER_NAME'] . "/uploads/media/{$this->name}";
  278.     }
  279.     
  280.     //Todo: clean up getImageTag and getVideoTag
  281.     
  282.     public function getImageTag(string $class ""string $style ""string $item_id ""string $url ""$lazy_load true)
  283.     {
  284.         // for clicks:
  285.         // need to include link as well, but that's stored in the media group item e.g. <a href="{{ item.getExternalUrl() }}" target="_blank">
  286.         // so make a function in media group items to get the tag and call this function
  287.         
  288.         // for impressions:
  289.         // insert record into database table
  290.         
  291.         if(!$this->getURL()) {
  292.             return "";
  293.         }
  294.         
  295.         if($this->getType() == "video/mp4") {
  296.             return "<video controls style='max-width:100%;'>
  297.                       <source src='{$this->getURL()}' type='video/mp4'>
  298.                     Your browser does not support the video tag.
  299.                     </video>";
  300.         }
  301.         
  302.         $return "
  303.         <img src='{$this->getURL()}'
  304.             alt='{$this->getAltTagText()}'
  305.             title='{$this->getAltTagText()}'
  306.             class='{$class}'
  307.             style='{$style} ' ";
  308.         if($lazy_load) {
  309.             $return .= " loading='lazy' ";
  310.         }
  311.         if($item_id) {
  312.             $return .= "data-mitem-id='".$item_id."'";
  313.         }
  314.         if($url) {
  315.             $return .= "data-url='".$url."'";
  316.         }
  317.         $return .= "/>";
  318.         return $return;
  319.         
  320.     }
  321.     
  322.     public function getVideoTag(
  323.         string $class "",
  324.         string $style "",
  325.         string $item_id "",
  326.         string $url "",
  327.         $lazy_load true,
  328.         $autoplay false,
  329.         $controls true,
  330.         $muted false,
  331.         $thumbnailUrl ""
  332.     ) {
  333.         // for clicks:
  334.         // need to include link as well, but that's stored in the media group item e.g. <a href="{{ item.getExternalUrl() }}" target="_blank">
  335.         // so make a function in media group items to get the tag and call this function
  336.         
  337.         // for impressions:
  338.         // insert record into database table
  339.         
  340.         if(!$this->getURL()) {
  341.             return "";
  342.         }
  343.         
  344.         if(in_array($this->getType(), ["video/mp4""application/octe"])) {
  345.             $controlsText $controls "controls" "";
  346.             $autoplayText $autoplay "autoplay" "";
  347.             $mutedText $muted "muted" "";
  348.             //$thumbnailText = "";
  349.             $thumbnailText "poster='{$thumbnailUrl}'";
  350.             $hoverPlay false;
  351.             $hoverPlayText $hoverPlay "onmouseover='this.play();' onmouseout='this.pause();this.currentTime=0;'" "";
  352.             return "<video {$controlsText} {$autoplayText} {$mutedText} {$thumbnailText} {$hoverPlayText} class='{$class}' style='max-width:100%; {$style}' data-video-mid='{$this->getId()}'>
  353.                       <source src='{$this->getURL()}' type='video/mp4'>
  354.                     Your browser does not support the video tag.
  355.                     </video>";
  356.         }
  357.         
  358.         $return "
  359.         <img src='{$this->getURL()}'
  360.             alt='{$this->getAltTagText()}'
  361.             title='{$this->getAltTagText()}'
  362.             class='{$class}'
  363.             style='{$style} ' ";
  364.         if($lazy_load) {
  365.             $return .= " loading='lazy' ";
  366.         }
  367.         if($item_id) {
  368.             $return .= "data-mitem-id='".$item_id."'";
  369.         }
  370.         if($url) {
  371.             $return .= "data-url='".$url."'";
  372.         }
  373.         $return .= "/>";
  374.         return $return;
  375.         
  376.     }
  377.     public function getModifiedAt(): ?\DateTimeInterface
  378.     {
  379.         return $this->modifiedAt;
  380.     }
  381.     public function setModifiedAt(\DateTimeInterface $modified_at): self
  382.     {
  383.         $this->modifiedAt $modified_at;
  384.         return $this;
  385.     }
  386.     public function getCreatedAt(): ?\DateTimeInterface
  387.     {
  388.         return $this->createdAt ?? new \DateTime("now");
  389.     }
  390.     public function setCreatedAt(\DateTimeInterface $created_at): self
  391.     {
  392.         $this->createdAt $created_at;
  393.         return $this;
  394.     }
  395.     
  396.     public function getType ()
  397.     {
  398.         return $this->type;
  399.     }
  400.     
  401.     public function setType (string $type null): self
  402.     {
  403.         $this->type $type $type "";
  404.         
  405.         return $this;
  406.     }
  407.     public function getStatus(): ?int
  408.     {
  409.         return $this->status;
  410.     }
  411.     public function setStatus(int $status): self
  412.     {
  413.         $this->status $status;
  414.         return $this;
  415.     }
  416.     
  417.     public function __toString ()
  418.     {
  419.         return "{$this->title} : {$this->type}";
  420.     }
  421.     public function getOldId(): ?int
  422.     {
  423.         return $this->old_id;
  424.     }
  425.     public function setOldId(int $old_id): self
  426.     {
  427.         $this->old_id $old_id;
  428.         return $this;
  429.     }
  430.     
  431.     public function isImage()
  432.     {
  433.         return (strpos($this->getType(), "image") !== false);
  434.     }
  435.     
  436.     /*
  437.     public function getSubDirectory(): ?string
  438.     {
  439.         return $this->sub_directory;
  440.     }
  441.     public function setSubDirectory(string $sub_directory): self
  442.     {
  443.         $this->sub_directory = $sub_directory;
  444.         return $this;
  445.     }
  446.     */
  447. }