acdsc - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of acdsc.php

  1. <?php
  2.  
  3. class ScreenShot
  4. {
  5.     const URL = 'https://mini.s-shot.ru/';
  6.     private $fileName;
  7.     private $sizeW;
  8.     private $sizeH;
  9.     private $extension;
  10.     private $url;
  11.     private $content;
  12.  
  13.     private function __construct($fileName, $url, $sizeW, $sizeH, $extension)
  14.     {
  15.         $this->fileName = $fileName;
  16.         $this->url = $url;
  17.         $this->sizeW = $sizeW;
  18.         $this->sizeH = $sizeH;
  19.         $this->extension = $extension;
  20.     }
  21.  
  22.     private function validate()
  23.     {
  24.         if (empty($this->fileName) || !is_string($this->fileName) || mb_strlen($this->fileName) < 1) {
  25.             return false;
  26.         }
  27.         if (empty($this->url) || !is_string($this->url) || mb_strlen($this->url) < 3) {
  28.             return false;
  29.         }
  30.         if (!is_int($this->sizeW) || $this->sizeW < 1 || $this->sizeW > 8192) {
  31.             return false;
  32.         }
  33.         if (!is_int($this->sizeH) || $this->sizeH < 0) {
  34.             return false;
  35.         }
  36.         if ($this->extension !== 'jpeg' && $this->extension !== 'png') {
  37.             return false;
  38.         }
  39.         return true;
  40.     }
  41.  
  42.     public static function make($fileName, $url, $sizeW, $sizeH = 0, $extension = 'jpeg')
  43.     {
  44.         $instance = new self($fileName, $url, $sizeW, $sizeH, $extension);
  45.         if ($instance->validate()) {
  46.             return $instance;
  47.         }
  48.         throw new \Exception('Error, check parameters', 0001);
  49.     }
  50.  
  51.     private function getUrl()
  52.     {
  53.         $url = self::URL;
  54.         $url .= "/{$this->sizeW}x{$this->sizeH}/{$this->extension}/{$this->sizeW}/{$this->fileName}";
  55.         $url .= "/?{$this->url}";
  56.         return $url;
  57.     }
  58.  
  59.     private function getScreenShot()
  60.     {
  61.         $this->content = file_get_contents($this->getUrl());
  62.     }
  63.  
  64.     public function save($fileFullPath)
  65.     {
  66.         if (empty($this->content)) {
  67.             $this->getScreenShot();
  68.         }
  69.         if (!file_exists($fileFullPath)) {
  70.             file_put_contents($fileFullPath, $this->content);
  71.             return true;
  72.         }
  73.         return false;
  74.     }
  75.  
  76.     public function show()
  77.     {
  78.         if (empty($this->content)) {
  79.             $this->getScreenShot();
  80.         }
  81.         echo '*' . $this->content . '*';
  82.     }
  83. }
  84.  
  85. ScreenShot::make('test', 'https://www.youtube.com/', 2560)->show();
File Description
  • acdsc
  • PHP Code
  • 15 Dec-2018
  • 2.23 Kb
You can Share it: