Common functions

b0ne^
/srv/http/Posts: 22Administrator in PHP

U ovom topicu cu kaciti neke funkcije koje cesto koristim, mozda nekome bude korisno. PHP verzija na kojoj je testirano: 7.0.14

HTTPS provera i currentURL

    <?php
        /**
         * Generate a full link for the current page
         *
         * @return string Current URL
         */
        protected function currentUrl(): string {
            if($this->https()) {
                return ((strpos($_SERVER['HTTP_HOST'], "https://") === false) ? ("https://") : ("")).$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
            }

            return ((strpos($_SERVER['HTTP_HOST'], "http://") === false) ? ("http://") : ("")).$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        }
        /**
         * Check whether website is accessed over HTTPS
         *
         * @return bool
         */
        protected function https(): bool {
            return (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == "on");
        }

Variable escape

    <?php
        /**
         * Escape HTML characters UTF-8 compatible
         *
         * @param string $string
         *
         * @return string
         */
        protected function escape(string $string): string {
            return htmlspecialchars($string, ENT_COMPAT | ENT_HTML5, "UTF-8", false);
        }

Provera da li variable sadrzi samo brojeve

    <?php
        /**
         * Check if variable contains only numbers
         *
         * @param mixed $int
         *
         * @return bool
         */
        protected function isInt($int): bool {
            return preg_match("/^[0-9]+$/", $int);
        }

Random string generisanje

    <?php
        /**
         * Generate random string
         *
         * @param int $length
         *
         * @return string
         */
        protected function strRandom(int $length): string {
            $characters = "abcdefghijklmnopqrstuvwxyz0123456789";
            $result = "";

            for($i = 0; $i < $length; $i++) {
                $character = $characters[rand(0, strlen($characters)-1)];

                if(ctype_alpha($character) && rand() & 1) {
                    $character = strtoupper($character);
                }

                $result .= $character;
            }

            return $result;
        }

If you don't know who I am, then maybe your best course would be to tread lightly.

Comments

  • 1 Comment sorted by Votes Date Added
  • b0ne^
    /srv/http/Posts: 22Administrator edited January 8

    PHP verzija na kojoj je testirano: 7.0.14

    Provera ispravnosti slike

    <?php
        /**
         * @param string $image_name
         * @param string $image_tmp
         * @param string $image_type
         *
         * @return bool
         */
        function check(string $image_name, string $image_tmp, string $image_type): bool {
            $image_extension = substr($image_name, strrpos($image_name, ".")+1);
            $mimeTypes = ["image/jpeg", "image/png", "image/gif"];
            $extensions = ["jpg", "jpeg", "png", "gif"];
    
            return (getimagesize($image_tmp) && in_array($image_type, $mimeTypes) && in_array($image_extension, $extensions));
        }
    

    Dodavanje watermarka na sliku

    <?php
        /**
         * Add watermark to image
         *
         * @param string $image Image path
         * @param string $watermark Watermark path
         * @param array $options
         *      $options = [
         *          'offset_x'  => (int) Number of pixels to horizontaly offset
         *          'offset_y'  => (int) Number of pixels to vertically offset
         *          'position'  => (string) Where to place the watermark
         *      ]
         *
         * @throws Exception if source image doesn't exists
         * @throws Exception if watermark image doesn't exists
         * @throws Exception if some of the options are invalid
         * @return bool
         */
        function watermark(string $image, string $watermark, array $options = ["offset_x" => 5, "offset_y" => 5, "position" => "bottom-right"]): bool {
            $watermark_positions = ["top-left", "top-center", "top-right", "center-left", "center-center", "center-right", "bottom-left", "bottom-center", "bottom-right"];
    
            if(!file_exists($image)) {
                throw new \Exception("File ".$image." doesn't exists");
            }
            if(!file_exists($watermark)) {
                throw new \Exception("File ".$watermark." doesn't exists");
            }
    
            $image_size = getimagesize($image);
            $watermark_size = getimagesize($watermark);
    
            $options['offset_x'] = $options['offset_x'] ?? 0;
            $options['offset_y'] = $options['offset_y'] ?? 0;
            $options['position'] = $options['position'] ?? "bottom-right";
    
            if(!in_array($options['position'], $watermark_positions)) {
                throw new \Exception("Invalid watermark position");
            }
    
            switch($image_size[2]) {
                case "1":
                    $im = imagecreatefromgif($image);
                break;
                case "2":
                    $im = imagecreatefromjpeg($image);
                break;
                case "3":
                    $im = imagecreatefrompng($image);
                break;
            }
            switch($watermark_size[2]) {
                case "1":
                    $watermark = imagecreatefromgif($watermark);
                break;
                case "2":
                    $watermark = imagecreatefromjpeg($watermark);
                break;
                case "3":
                    $watermark = imagecreatefrompng($watermark);
                break;
            }
    
            $watermark_width = (($image_size[0]-$options['offset_x']) / 4);
    
            if($watermark_width > $watermark_size[0]) {
                $watermark_width = $watermark_size[0];
            }
    
            $watermark_height = ($watermark_width/$watermark_size[0])*$watermark_size[1];
    
            if($watermark_height > $image_size[1]) {
                $watermark_height = (($image_size[1]-$options['offset_y']) / 4);
                $watermark_width = ($watermark_height/$watermark_size[1])*$watermark_size[0];
            } else if($watermark_height > $watermark_size[1]) {
                $watermark_height = $watermark_size[1];
            }
    
            switch($options['position']) {
                case "top-left":
                    $position_x = $options['offset_x'];
                    $position_y = $options['offset_y'];
                break;
                case "top-center":
                    $position_x = ceil(($image_size[0]/2)+$options['offset_x']-($watermark_width/2));
                    $position_y = $options['offset_y'];
                break;
                case "top-right":
                    $position_x = ceil($image_size[0]-$options['offset_x']-$watermark_width);
                    $position_y = $options['offset_y'];
                break;
                case "center-left":
                    $position_x = $options['offset_x'];
                    $position_y = ceil(($image_size[1]/2)+$options['offset_y']-($watermark_height/2));
                break;
                case "center-center":
                    $position_x = ceil(($image_size[0]/2)+$options['offset_x']-($watermark_width/2));
                    $position_y = ceil(($image_size[1]/2)+$options['offset_y']-($watermark_height/2));
                break;
                case "center-right":
                    $position_x = ceil($image_size[0]-$options['offset_x']-$watermark_width);
                    $position_y = ceil(($image_size[1]/2)+$options['offset_y']-($watermark_height/2));
                break;
                case "bottom-left":
                    $position_x = $options['offset_x'];
                    $position_y = ceil($image_size[1]-$watermark_height-$options['offset_y']);
                break;
                case "bottom-center":
                    $position_x = ceil(($image_size[0]/2)+$options['offset_x']-($watermark_width/2));
                    $position_y = ceil($image_size[1]-$watermark_height-$options['offset_y']);
                break;
                case "bottom-right":
                    $position_x = ceil($image_size[0]-$options['offset_x']-$watermark_width);
                    $position_y = ceil($image_size[1]-$watermark_height-$options['offset_y']);
                break;
            }
    
            $watermark_resized = imagecreatetruecolor($watermark_width, $watermark_height);
            imagealphablending($watermark_resized, false);
            imagesavealpha($watermark_resized, true);
            imagecopyresampled($watermark_resized, $watermark, 0, 0, 0, 0, $watermark_width, $watermark_height, $watermark_size[0], $watermark_size[1]);
            imagecopy($im, $watermark_resized, $position_x, $position_y, 0, 0, $watermark_width, $watermark_height);
    
            $save = false;
    
            switch($image_size[2]) {
                case "1":
                    $save = imagegif($im, $image);
                break;
                case "2":
                    $save = imagejpeg($im, $image, 100);
                break;
                case "3":
                    $save = imagepng($im, $image);
                break;
            }
    
            return $save;
        }
    

    If you don't know who I am, then maybe your best course would be to tread lightly.

Sign In or Register to comment.

Welcome

It looks like you're new here. If you want to get involved, click one of these buttons!

Discussions

In this Discussion