Ignore:
Timestamp:
Apr 14, 2019, 2:31:40 PM (5 years ago)
Author:
roby
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/client/modules/Elezioni/grafici/jpgraph_imgtrans.php

    r265 r267  
    11<?php
    22//=======================================================================
    3 // File:        JPGRAPH_IMGTRANS.PHP
    4 // Description: Extension for JpGraph to do some simple img transformations
    5 // Created:     2003-09-06
    6 // Ver:         $Id: jpgraph_imgtrans.php 1106 2009-02-22 20:16:35Z ljp $
     3// File:        JPGRAPH_IMGTRANS.PHP
     4// Description: Extension for JpGraph to do some simple img transformations
     5// Created:     2003-09-06
     6// Ver:         $Id: jpgraph_imgtrans.php 781 2006-10-08 08:07:47Z ljp $
    77//
    8 // Copyright (c) Asial Corporation. All rights reserved.
     8// Copyright (c) Aditus Consulting. All rights reserved.
    99//========================================================================
    1010
    1111//------------------------------------------------------------------------
    1212// Class ImgTrans
    13 // Perform some simple image transformations.
     13// Perform some simple image transformations. 
    1414//------------------------------------------------------------------------
    1515class ImgTrans {
    1616    private $gdImg=null;
    1717
    18     function __construct($aGdImg) {
    19         // Constructor
    20         $this->gdImg = $aGdImg;
    21     }
    22 
    23     // --------------------------------------------------------------------
    24     // _TransVert3D() and _TransHor3D() are helper methods to
    25     // Skew3D().
     18    function ImgTrans($aGdImg) {
     19        // Constructor
     20        $this->gdImg = $aGdImg;
     21    }
     22
     23    // --------------------------------------------------------------------
     24    // _TransVert3D() and _TransHor3D() are helper methods to 
     25    // Skew3D(). 
    2626    // --------------------------------------------------------------------
    2727    function _TransVert3D($aGdImg,$aHorizon=100,$aSkewDist=120,$aDir=SKEW3D_DOWN,$aMinSize=true,$aFillColor='#FFFFFF',$aQuality=false,$aBorder=false,$aHorizonPos=0.5) {
    2828
    2929
    30         // Parameter check
    31     if( $aHorizonPos < 0 || $aHorizonPos > 1.0 ) {
    32         JpGraphError::RaiseL(9001);
    33         //("Value for image transformation out of bounds.\nVanishing point on horizon must be specified as a value between 0 and 1.");
    34     }
    35 
    36     $w = imagesx($aGdImg);
    37     $h = imagesy($aGdImg);
    38 
    39     // Create new image
    40     $ww = $w;
    41     if( $aMinSize )
    42     $hh = ceil($h * $aHorizon / ($aSkewDist+$h));
    43     else
    44     $hh = $h;
    45 
    46     $newgdh = imagecreatetruecolor($ww,$hh);
    47     $crgb = new RGB( $newgdh );
    48     $fillColor = $crgb->Allocate($aFillColor);
    49     imagefilledrectangle($newgdh,0,0,$ww-1,$hh-1,$fillColor);
    50 
    51     if( $aBorder ) {
    52         $colidx = $crgb->Allocate($aBorder);
    53         imagerectangle($newgdh,0,0,$ww-1,$hh-1,$colidx);
    54     }
    55 
    56     $mid = round($w * $aHorizonPos);
    57 
    58     $last=$h;
    59     for($y=0; $y < $h; ++$y) {
    60 
    61         $yp = $h-$y-1;
    62         $yt = floor($yp * $aHorizon / ($aSkewDist + $yp));
    63 
    64         if( !$aQuality ) {
    65             if( $last <= $yt ) continue ;
    66             $last = $yt;
    67         }
    68 
    69         for($x=0; $x < $w; ++$x) {
    70             $xt = ($x-$mid) * $aSkewDist / ($aSkewDist + $yp);
    71             if( $aDir == SKEW3D_UP )
    72             $rgb = imagecolorat($aGdImg,$x,$h-$y-1);
    73             else
    74             $rgb = imagecolorat($aGdImg,$x,$y);
    75             $r = ($rgb >> 16) & 0xFF;
    76             $g = ($rgb >> 8) & 0xFF;
    77             $b = $rgb & 0xFF;
    78             $colidx = imagecolorallocate($newgdh,$r,$g,$b);
    79             $xt = round($xt+$mid);
    80             if( $aDir == SKEW3D_UP ) {
    81                 $syt = $yt;
    82             }
    83             else {
    84                 $syt = $hh-$yt-1;
    85             }
    86 
    87             if( !empty($set[$yt]) ) {
    88                 $nrgb = imagecolorat($newgdh,$xt,$syt);
    89                 $nr = ($nrgb >> 16) & 0xFF;
    90                 $ng = ($nrgb >> 8) & 0xFF;
    91                 $nb = $nrgb & 0xFF;
    92                 $colidx = imagecolorallocate($newgdh,floor(($r+$nr)/2),
    93                 floor(($g+$ng)/2),floor(($b+$nb)/2));
    94             }
    95 
    96             imagesetpixel($newgdh,$xt,$syt,$colidx);
    97         }
    98 
    99         $set[$yt] = true;
    100     }
    101 
    102     return $newgdh;
    103     }
    104 
    105     // --------------------------------------------------------------------
    106     // _TransVert3D() and _TransHor3D() are helper methods to
    107     // Skew3D().
     30        // Parameter check
     31        if( $aHorizonPos < 0 || $aHorizonPos > 1.0 ) {
     32            JpGraphError::RaiseL(9001);
     33//("Value for image transformation out of bounds.\nVanishing point on horizon must be specified as a value between 0 and 1.");
     34        }
     35
     36        $w = imagesx($aGdImg);
     37        $h = imagesy($aGdImg);
     38
     39        // Create new image
     40        $ww = $w;
     41        if( $aMinSize )
     42            $hh = ceil($h * $aHorizon / ($aSkewDist+$h));
     43        else
     44            $hh = $h;
     45       
     46        $newgdh = imagecreatetruecolor($ww,$hh);
     47        $crgb = new RGB( $newgdh );
     48        $fillColor = $crgb->Allocate($aFillColor);
     49        imagefilledrectangle($newgdh,0,0,$ww-1,$hh-1,$fillColor);
     50
     51        if( $aBorder ) {
     52            $colidx = $crgb->Allocate($aBorder);
     53            imagerectangle($newgdh,0,0,$ww-1,$hh-1,$colidx);
     54        }
     55
     56        $mid = round($w * $aHorizonPos);
     57   
     58        $last=$h;
     59        for($y=0; $y < $h; ++$y) {     
     60
     61            $yp = $h-$y-1;
     62            $yt = floor($yp * $aHorizon / ($aSkewDist + $yp));     
     63
     64            if( !$aQuality ) {
     65                if( $last <= $yt ) continue ;
     66                $last = $yt;
     67            }
     68
     69            for($x=0; $x < $w; ++$x) {     
     70                $xt = ($x-$mid) * $aSkewDist / ($aSkewDist + $yp);
     71                if( $aDir == SKEW3D_UP )
     72                    $rgb = imagecolorat($aGdImg,$x,$h-$y-1);
     73                else
     74                    $rgb = imagecolorat($aGdImg,$x,$y);
     75                $r = ($rgb >> 16) & 0xFF;
     76                $g = ($rgb >> 8) & 0xFF;
     77                $b = $rgb & 0xFF;   
     78                $colidx = imagecolorallocate($newgdh,$r,$g,$b);
     79                $xt = round($xt+$mid);
     80                if( $aDir == SKEW3D_UP ) {
     81                    $syt = $yt;
     82                }
     83                else {
     84                    $syt = $hh-$yt-1;
     85                }
     86
     87                if( !empty($set[$yt]) ) {
     88                    $nrgb = imagecolorat($newgdh,$xt,$syt);
     89                    $nr = ($nrgb >> 16) & 0xFF;
     90                    $ng = ($nrgb >> 8) & 0xFF;
     91                    $nb = $nrgb & 0xFF;   
     92                    $colidx = imagecolorallocate($newgdh,floor(($r+$nr)/2),
     93                                                 floor(($g+$ng)/2),floor(($b+$nb)/2)); 
     94                }       
     95
     96                imagesetpixel($newgdh,$xt,$syt,$colidx);       
     97            }
     98
     99            $set[$yt] = true;   
     100        }
     101
     102        return $newgdh;
     103    }
     104
     105    // --------------------------------------------------------------------
     106    // _TransVert3D() and _TransHor3D() are helper methods to 
     107    // Skew3D(). 
    108108    // --------------------------------------------------------------------
    109109    function _TransHor3D($aGdImg,$aHorizon=100,$aSkewDist=120,$aDir=SKEW3D_LEFT,$aMinSize=true,$aFillColor='#FFFFFF',$aQuality=false,$aBorder=false,$aHorizonPos=0.5) {
    110110
    111         $w = imagesx($aGdImg);
    112         $h = imagesy($aGdImg);
    113 
    114         // Create new image
    115         $hh = $h;
    116         if( $aMinSize )
    117         $ww = ceil($w * $aHorizon / ($aSkewDist+$w));
    118         else
    119         $ww = $w;
    120 
    121         $newgdh = imagecreatetruecolor($ww,$hh);
    122         $crgb = new RGB( $newgdh );
    123         $fillColor = $crgb->Allocate($aFillColor);
    124         imagefilledrectangle($newgdh,0,0,$ww-1,$hh-1,$fillColor);
    125 
    126         if( $aBorder ) {
    127             $colidx = $crgb->Allocate($aBorder);
    128             imagerectangle($newgdh,0,0,$ww-1,$hh-1,$colidx);
    129         }
    130 
    131         $mid = round($h * $aHorizonPos);
    132 
    133         $last = -1;
    134         for($x=0; $x < $w-1; ++$x) {
    135             $xt = floor($x * $aHorizon / ($aSkewDist + $x));
    136             if( !$aQuality ) {
    137                 if( $last >= $xt ) continue ;
    138                 $last = $xt;
    139             }
    140 
    141             for($y=0; $y < $h; ++$y) {
    142                 $yp = $h-$y-1;
    143                 $yt = ($yp-$mid) * $aSkewDist / ($aSkewDist + $x);
    144 
    145                 if( $aDir == SKEW3D_RIGHT )
    146                 $rgb = imagecolorat($aGdImg,$w-$x-1,$y);
    147                 else
    148                 $rgb = imagecolorat($aGdImg,$x,$y);
    149                 $r = ($rgb >> 16) & 0xFF;
    150                 $g = ($rgb >> 8) & 0xFF;
    151                 $b = $rgb & 0xFF;
    152                 $colidx = imagecolorallocate($newgdh,$r,$g,$b);
    153                 $yt = floor($hh-$yt-$mid-1);
    154                 if( $aDir == SKEW3D_RIGHT ) {
    155                     $sxt = $ww-$xt-1;
    156                 }
    157                 else
    158                 $sxt = $xt ;
    159 
    160                 if( !empty($set[$xt]) ) {
    161                     $nrgb = imagecolorat($newgdh,$sxt,$yt);
    162                     $nr = ($nrgb >> 16) & 0xFF;
    163                     $ng = ($nrgb >> 8) & 0xFF;
    164                     $nb = $nrgb & 0xFF;
    165                     $colidx = imagecolorallocate($newgdh,floor(($r+$nr)/2),
    166                     floor(($g+$ng)/2),floor(($b+$nb)/2));
    167                 }
    168                 imagesetpixel($newgdh,$sxt,$yt,$colidx);
    169             }
    170 
    171             $set[$xt] = true;
    172         }
    173 
    174         return $newgdh;
     111        $w = imagesx($aGdImg);
     112        $h = imagesy($aGdImg);
     113
     114        // Create new image
     115        $hh = $h;
     116        if( $aMinSize )
     117            $ww = ceil($w * $aHorizon / ($aSkewDist+$w));
     118        else
     119            $ww = $w;
     120       
     121        $newgdh = imagecreatetruecolor($ww,$hh);
     122        $crgb = new RGB( $newgdh );
     123        $fillColor = $crgb->Allocate($aFillColor);
     124        imagefilledrectangle($newgdh,0,0,$ww-1,$hh-1,$fillColor);
     125
     126        if( $aBorder ) {
     127            $colidx = $crgb->Allocate($aBorder);
     128            imagerectangle($newgdh,0,0,$ww-1,$hh-1,$colidx);
     129        }
     130
     131        $mid = round($h * $aHorizonPos);
     132
     133        $last = -1;
     134        for($x=0; $x < $w-1; ++$x) {       
     135            $xt = floor($x * $aHorizon / ($aSkewDist + $x));
     136            if( !$aQuality ) {
     137                if( $last >= $xt ) continue ;
     138                $last = $xt;
     139            }
     140
     141            for($y=0; $y < $h; ++$y) { 
     142                $yp = $h-$y-1;
     143                $yt = ($yp-$mid) * $aSkewDist / ($aSkewDist + $x);
     144
     145                if( $aDir == SKEW3D_RIGHT )
     146                    $rgb = imagecolorat($aGdImg,$w-$x-1,$y);
     147                else
     148                    $rgb = imagecolorat($aGdImg,$x,$y);
     149                $r = ($rgb >> 16) & 0xFF;
     150                $g = ($rgb >> 8) & 0xFF;
     151                $b = $rgb & 0xFF;   
     152                $colidx = imagecolorallocate($newgdh,$r,$g,$b);
     153                $yt = floor($hh-$yt-$mid-1);
     154                if( $aDir == SKEW3D_RIGHT ) {
     155                    $sxt = $ww-$xt-1;
     156                }
     157                else
     158                    $sxt = $xt ;
     159
     160                if( !empty($set[$xt]) ) {
     161                    $nrgb = imagecolorat($newgdh,$sxt,$yt);
     162                    $nr = ($nrgb >> 16) & 0xFF;
     163                    $ng = ($nrgb >> 8) & 0xFF;
     164                    $nb = $nrgb & 0xFF;   
     165                    $colidx = imagecolorallocate($newgdh,floor(($r+$nr)/2),
     166                                                 floor(($g+$ng)/2),floor(($b+$nb)/2)); 
     167                }
     168                imagesetpixel($newgdh,$sxt,$yt,$colidx);       
     169            }
     170
     171            $set[$xt] = true;
     172        }
     173
     174        return $newgdh;
    175175    }
    176176
     
    180180    // of the image. The transformation is specified by giving the height
    181181    // of the artificial horizon and specifying a "skew" factor which
    182     // is the distance on the horizon line between the point of
     182    // is the distance on the horizon line between the point of 
    183183    // convergence and perspective line.
    184184    //
     
    188188    // Parameters:
    189189    // * $aGdImg, GD handle to the image to be transformed
    190     // * $aHorizon, Distance to the horizon
     190    // * $aHorizon, Distance to the horizon 
    191191    // * $aSkewDist, Distance from the horizon point of convergence
    192     //   on the horizon line to the perspective points. A larger
     192    //   on the horizon line to the perspective points. A larger 
    193193    //   value will fore-shorten the image more
    194     // * $aDir, parameter specifies type of convergence. This of this
     194    // * $aDir, parameter specifies type of convergence. This of this 
    195195    //   as the walls in a room you are looking at. This specifies if the
    196196    //   image should be applied on the left,right,top or bottom walls.
     
    202202    //   high quality will have a dramatic effect on the time it takes
    203203    //   to transform an image.
    204     // * $aBorder, if set to anything besides false this will draw a
     204    // * $aBorder, if set to anything besides false this will draw a 
    205205    //   a border of the speciied color around the image
    206206    // --------------------------------------------------------------------
    207207    function Skew3D($aHorizon=120,$aSkewDist=150,$aDir=SKEW3D_DOWN,$aHiQuality=false,$aMinSize=true,$aFillColor='#FFFFFF',$aBorder=false) {
    208         return $this->_Skew3D($this->gdImg,$aHorizon,$aSkewDist,$aDir,$aHiQuality,
    209         $aMinSize,$aFillColor,$aBorder);
     208        return $this->_Skew3D($this->gdImg,$aHorizon,$aSkewDist,$aDir,$aHiQuality,
     209                              $aMinSize,$aFillColor,$aBorder);
    210210    }
    211211
    212212    function _Skew3D($aGdImg,$aHorizon=120,$aSkewDist=150,$aDir=SKEW3D_DOWN,$aHiQuality=false,$aMinSize=true,$aFillColor='#FFFFFF',$aBorder=false) {
    213         if( $aDir == SKEW3D_DOWN || $aDir == SKEW3D_UP )
    214         return $this->_TransVert3D($aGdImg,$aHorizon,$aSkewDist,$aDir,$aMinSize,$aFillColor,$aHiQuality,$aBorder);
    215         else
    216         return $this->_TransHor3D($aGdImg,$aHorizon,$aSkewDist,$aDir,$aMinSize,$aFillColor,$aHiQuality,$aBorder);
    217 
    218     }
    219 
     213        if( $aDir == SKEW3D_DOWN || $aDir == SKEW3D_UP )
     214            return $this->_TransVert3D($aGdImg,$aHorizon,$aSkewDist,$aDir,$aMinSize,$aFillColor,$aHiQuality,$aBorder);
     215        else
     216            return $this->_TransHor3D($aGdImg,$aHorizon,$aSkewDist,$aDir,$aMinSize,$aFillColor,$aHiQuality,$aBorder);
     217
     218    }
     219   
    220220}
    221221
Note: See TracChangeset for help on using the changeset viewer.