Перейти к контенту
  • 0

перевод кода на php 7


kinlok

Вопрос

ребят я тут навечок и php тока недавна начал изучать памагите пажалуста перевести этат код на php 7

if (!defined('MOZG')) {
    die("Hacking attempt!");
}

class thumbnail
{
    var $img;
    var $watermark_image_light;
    var $watermark_image_dark;
    
    function thumbnail($imgfile)
    {
        //detect image format
        
        $info = @getimagesize($imgfile);
        
        if ($info[2] == 2) {
            $this->img['format'] = "JPEG";
            $this->img['src']    = @imagecreatefromjpeg($imgfile);
        } elseif ($info[2] == 3) {
            $this->img['format'] = "PNG";
            $this->img['src']    = @imagecreatefrompng($imgfile);
        } elseif ($info[2] == 1) {
            $this->img['format'] = "GIF";
            $this->img['src']    = @imagecreatefromgif($imgfile);
        } else {
            echo "Not Supported File! Thumbnails can only be made from .jpg, gif and .png images!";
            @unlink($imgfile);
            exit();
        }
        
        if (!$this->img['src']) {
            echo "Not Supported File! Thumbnails can only be made from .jpg, gif and .png images!";
            @unlink($imgfile);
            exit();
            
        }
        
        $this->img['lebar']        = @imagesx($this->img['src']);
        $this->img['tinggi']       = @imagesy($this->img['src']);
        $this->img['lebar_thumb']  = $this->img['lebar'];
        $this->img['tinggi_thumb'] = $this->img['tinggi'];
        //default quality jpeg
        $this->img['quality']      = 90;
        
    }
    
    function size_auto($size = 100, $site = 0, $jqCrop = 0)
    {
        
        $size = explode("x", $size);
        
        if ($jqCrop) {
            
            return $this->jqCrop(intval($size[0]), intval($size[1]), $jqCrop);
            
        } else if (count($size) == 2) {
            $size[0] = intval($size[0]);
            $size[1] = intval($size[1]);
            return $this->crop(intval($size[0]), intval($size[1]));
            
        } else {
            $size[0] = intval($size[0]);
            return $this->scale(intval($size[0]), $site);
            
        }
        
    }
    
    function crop($nw, $nh)
    {
        
        $w = $this->img['lebar'];
        $h = $this->img['tinggi'];
        
        if ($w <= $nw AND $h <= $nh) {
            $this->img['lebar_thumb']  = $w;
            $this->img['tinggi_thumb'] = $h;
            return 0;
        }
        
        $nw = min($nw, $w);
        $nh = min($nh, $h);
        
        $size_ratio = max($nw / $w, $nh / $h);
        
        $src_w = ceil($nw / $size_ratio);
        $src_h = ceil($nh / $size_ratio);
        
        $sx = floor(($w - $src_w) / 2);
        $sy = floor(($h - $src_h) / 2);
        
        $this->img['des'] = imagecreatetruecolor($nw, $nh);
        
        if ($this->img['format'] == "PNG") {
            imagealphablending($this->img['des'], false);
            imagesavealpha($this->img['des'], true);
        }
        
        imagecopyresampled($this->img['des'], $this->img['src'], 0, 0, $sx, 0, $nw, $nh, $src_w, $src_h);
        
        $this->img['src'] = $this->img['des'];
        return 1;
    }
    
    function jqCrop($nw, $nh, $cropData)
    {
        $cropDataExp = explode('|', $cropData);
        $left        = $cropDataExp[0];
        $top         = $cropDataExp[1];
        
        if (!$left OR $left <= 0)
            $left = 0;
        if (!$top OR $top <= 0)
            $top = 0;
        
        if ($nw < 100)
            $nw = 100;
        if ($nh < 100)
            $nh = 100;
        
        $w = $this->img['lebar'];
        $h = $this->img['tinggi'];
        
        if ($w <= $nw AND $h <= $nh) {
            $this->img['lebar_thumb']  = $w;
            $this->img['tinggi_thumb'] = $h;
            return 0;
        }
        
        $nw = min($nw, $w);
        $nh = min($nh, $h);
        
        $size_ratio = max($nw / $w, $nh / $h);
        
        $src_w = ceil($nw / $size_ratio);
        $src_h = ceil($nh / $size_ratio);
        
        $this->img['des'] = imagecreatetruecolor($nw, $nh);
        
        if ($this->img['format'] == "PNG") {
            imagealphablending($this->img['des'], false);
            imagesavealpha($this->img['des'], true);
        }
        
        imagecopyresampled($this->img['des'], $this->img['src'], 0, 0, $left, $top, $nw, $nh, $nw, $nh);
        
        $this->img['src'] = $this->img['des'];
        
        return 1;
    }
    
    function scale($size = 100, $site = 0)
    {
        
        $site = intval($site);
        
        if ($this->img['lebar'] <= $size and $this->img['tinggi'] <= $size) {
            $this->img['lebar_thumb']  = $this->img['lebar'];
            $this->img['tinggi_thumb'] = $this->img['tinggi'];
            return 0;
        }
        
        switch ($site) {
            
            case "1":
                if ($this->img['lebar'] <= $size) {
                    $this->img['lebar_thumb']  = $this->img['lebar'];
                    $this->img['tinggi_thumb'] = $this->img['tinggi'];
                    return 0;
                } else {
                    $this->img['lebar_thumb']  = $size;
                    $this->img['tinggi_thumb'] = ($this->img['lebar_thumb'] / $this->img['lebar']) * $this->img['tinggi'];
                }
                
                break;
            
            case "2":
                if ($this->img['tinggi'] <= $size) {
                    $this->img['lebar_thumb']  = $this->img['lebar'];
                    $this->img['tinggi_thumb'] = $this->img['tinggi'];
                    return 0;
                } else {
                    $this->img['tinggi_thumb'] = $size;
                    $this->img['lebar_thumb']  = ($this->img['tinggi_thumb'] / $this->img['tinggi']) * $this->img['lebar'];
                }
                
                break;
            
            default:
                
                if ($this->img['lebar'] >= $this->img['tinggi']) {
                    $this->img['lebar_thumb']  = $size;
                    $this->img['tinggi_thumb'] = ($this->img['lebar_thumb'] / $this->img['lebar']) * $this->img['tinggi'];
                    
                } else {
                    
                    $this->img['tinggi_thumb'] = $size;
                    $this->img['lebar_thumb']  = ($this->img['tinggi_thumb'] / $this->img['tinggi']) * $this->img['lebar'];
                    
                }
                
                break;
        }
        
        if ($this->img['lebar_thumb'] < 1)
            $this->img['lebar_thumb'] = 1;
        if ($this->img['tinggi_thumb'] < 1)
            $this->img['tinggi_thumb'] = 1;
        
        $this->img['des'] = imagecreatetruecolor($this->img['lebar_thumb'], $this->img['tinggi_thumb']);
        
        if ($this->img['format'] == "PNG") {
            imagealphablending($this->img['des'], false);
            imagesavealpha($this->img['des'], true);
        }
        
        @imagecopyresampled($this->img['des'], $this->img['src'], 0, 0, 0, 0, $this->img['lebar_thumb'], $this->img['tinggi_thumb'], $this->img['lebar'], $this->img['tinggi']);
        
        $this->img['src'] = $this->img['des'];
        return 1;
        
    }
    
    function jpeg_quality($quality = 90)
    {
        //jpeg quality
        $this->img['quality'] = $quality;
    }
    
function insert_watermark($min_image) {
		$image_width = imagesx( $this->img['src'] );
		$image_height = imagesy( $this->img['src'] );
		
		if($image_width > 210 AND $image_height > 60){
		$this->watermark_image_light = ROOT_DIR . '/templates/Default/images/watermark_light.png';
		$this->watermark_image_dark = ROOT_DIR . '/templates/Default/images/watermark_dark.png';
		$margin = 7;
		}else{
		$this->watermark_image_light = ROOT_DIR . '/templates/Default/images/wm_min_l.png';
		$this->watermark_image_dark = ROOT_DIR . '/templates/Default/images/wm_min_d.png';
		$margin = 8;
		}
		
		
		list ( $watermark_width, $watermark_height ) = getimagesize( $this->watermark_image_light );
		
			$watermark_x = $image_width - $margin - $watermark_width;
			$watermark_y = $image_height - $margin - $watermark_height;
		
		
		$watermark_x2 = $watermark_x + $watermark_width;
		$watermark_y2 = $watermark_y + $watermark_height;
		
		if( $watermark_x < 0 OR $watermark_y < 0 OR $watermark_x2 > $image_width OR $watermark_y2 > $image_height OR $image_width < $min_image OR $image_height < $min_image ) {
			return;
		}
		
		$test = imagecreatetruecolor( 1, 1 );
		imagecopyresampled( $test, $this->img['src'], 0, 0, $watermark_x, $watermark_y, 1, 1, $watermark_width, $watermark_height );
		$rgb = imagecolorat( $test, 0, 0 );
		
		$r = ($rgb >> 16) & 0xFF;
		$g = ($rgb >> 8) & 0xFF;
		$b = $rgb & 0xFF;
		
		$max = min( $r, $g, $b );
		$min = max( $r, $g, $b );
		$lightness = ( double ) (($max + $min) / 510.0);
		imagedestroy( $test );
		
		$watermark_image = ($lightness < 0.5) ? $this->watermark_image_light : $this->watermark_image_dark;
		
		$watermark = imagecreatefrompng( $watermark_image );

		imagealphablending( $watermark, TRUE );

		if( $this->img['format'] == "PNG") {
			imagealphablending( $this->img['src'], TRUE );
			$temp_img = imagecreatetruecolor( $image_width, $image_height );
			imagealphablending ( $temp_img , false );
			imagesavealpha ( $temp_img , true );
			imagecopy( $temp_img, $this->img['src'], 0, 0, 0, 0, $image_width, $image_height );
			imagecopy( $temp_img, $watermark, $watermark_x, $watermark_y, 0, 0, $watermark_width, $watermark_height );
			imagecopy( $this->img['src'], $temp_img, 0, 0, 0, 0, $image_width, $image_height );
			imagedestroy( $temp_img );
		
		} elseif($this->img['format'] == "GIF") { 

			$temp_img = imagecreatetruecolor( $image_width, $image_height );

			$transparent_index=imagecolortransparent($this->img['src']);

			if($transparent_index!==-1){
				$transparent_color=imagecolorsforindex($this->img['src'], $transparent_index);
			 
				$transparent_destination_index=imagecolorallocate($temp_img, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
				imagecolortransparent($temp_img, $transparent_destination_index);
			 
				imagefill($temp_img, 0, 0, $transparent_destination_index);
			}

			imagecopy( $temp_img, $this->img['src'], 0, 0, 0, 0, $image_width, $image_height );
			imagecopy( $temp_img, $watermark, $watermark_x, $watermark_y, 0, 0, $watermark_width, $watermark_height );
			imagecopy( $this->img['src'], $temp_img, 0, 0, 0, 0, $image_width, $image_height );
			imagedestroy( $temp_img );

		} else {

			imagecopy( $this->img['src'], $watermark, $watermark_x, $watermark_y, 0, 0, $watermark_width, $watermark_height );

		}
	
		imagedestroy( $watermark );
	
	}
	
	function save($save = "") {
		
		if( $this->img['format'] == "JPG" || $this->img['format'] == "JPEG" ) {
			//JPEG
			 $this->insert_watermark('50');
			imagejpeg( $this->img['src'], $save, $this->img['quality'] );
		} elseif( $this->img['format'] == "PNG" ) {
			//PNG
			$this->insert_watermark('50');
			imagealphablending( $this->img['src'], false);
			imagesavealpha( $this->img['src'], true);
			imagepng( $this->img['src'], $save );
		} elseif( $this->img['format'] == "GIF" ) {
			//GIF
			$this->insert_watermark('50');
			imagegif( $this->img['src'], $save );
		}
		
		imagedestroy( $this->img['src'] );
	}
	
	function show() {
		if( $this->img['format'] == "JPG" || $this->img['format'] == "JPEG" ) {
			//JPEG
			imageJPEG( $this->img['src'], "", $this->img['quality'] );
		} elseif( $this->img['format'] == "PNG" ) {
			//PNG
			imagePNG( $this->img['src'] );
		} elseif( $this->img['format'] == "GIF" ) {
			//GIF
			imageGIF( $this->img['src'] );
		}
		
		imagedestroy( $this->img['src'] );
	}

}
?>
Ссылка на комментарий
Поделиться на других сайтах

Рекомендуемые сообщения

Пока что нет ответов на этот вопрос

Присоединиться к обсуждению

Вы можете ответить сейчас, а зарегистрироваться позже. Если у вас уже есть аккаунт, войдите, чтобы ответить от своего имени.

Гость
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Ответить на вопрос...

×   Вы вставили отформатированный текст.   Удалить форматирование

  Допустимо не более 75 смайлов.

×   Ваша ссылка была автоматически заменена на медиа-контент.   Отображать как ссылку

×   Ваши публикации восстановлены.   Очистить редактор

×   Вы не можете вставить изображения напрямую. Загрузите или вставьте изображения по ссылке.

Зарузка...
×
×
  • Создать...

Важная информация

Находясь на нашем сайте, вы соглашаетесь на использование файлов cookie, а также с нашим положением о конфиденциальности Политика конфиденциальности и пользовательским соглашением Условия использования.