image rotation

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    image rotation

    hi im trying to create a imagfe rotation script for lavalair



    here is the code for basic image rotation but i need to add the code that selects the avatar from the database and then saves it once its rotated ...i dont know if its possible or not on wap


    PHP Code:
        <?php
    if(!function_exists("imagerotate")) {
        function imagerotate(&$srcImg, $angle, $transparentColor = null) {
            $srcw = imagesx($srcImg);
            $srch = imagesy($srcImg);
            
            if($angle == 0) return $srcImg;
            
            // Convert the angle to radians
            $pi = 3.141592654;
            $theta = $angle * $pi / 180;
            
            // Get the origin (center) of the image
            $originx = $srcw / 2;
            $originy = $srch / 2;
            
            // The pixels array for the new image
            $pixels = array();
            $minx = 0;
            $maxx = 0;
            $miny = 0;
            $maxy = 0;
            $dstw = 0;
            $dsth = 0;
            
            // Loop through every pixel and transform it
            for($x=0;$x<$srcw;$x++) {
                for($y=0;$y<$srch;$y++) {
                    list($x1, $y1) = translateCoordinate($originx, $originy, $x, $y, false);
                    
                    $x2 = $x * cos($theta) - $y * sin($theta);
                    $y2 = $x * sin($theta) + $y * cos($theta);
                    
                    // Store the pixel color
                    $pixels[] = array($x2, $y2, imagecolorat($srcImg, $x, $y));
                    
                    // Check our boundaries
                    if($x2 > $maxx) $maxx = $x2;
                    if($x2 < $minx) $minx = $x2;
                    if($y2 > $maxy) $maxy = $y2;
                    if($y2 < $miny) $miny = $y2;
                }
            }
            
            // Determine the new image size
            $dstw = $maxx - $minx + 1;
            $dsth = $maxy - $miny + 1;
            
            // Create our new image
            $dstImg = imagecreatetruecolor($dstw, $dsth);
            
            // Fill the background with our transparent color
            if($transparentColor == null) $transparentColor = imagecolorallocate($dstImg, 1, 2, 3);
            imagecolortransparent($dstImg, $transparentColor);
            imagefilledrectangle($dstImg, 0, 0, $dstw + 1, $dsth + 1, $transparentColor);
            
            // Get the new origin
            $neworiginx = -$minx;
            $neworiginy = -$miny;
            
            // Fill in the pixels
            foreach($pixels as $data) {
                list($x, $y, $color) = $data;
                list($newx, $newy) = translateCoordinate($neworiginx, $neworiginy, $x, $y);
                imagesetpixel($dstImg, $newx, $newy, $color);
            }
            
            return $dstImg;
        }
        
        /**
         * Translates from mathematical coordinate system to computer coordinate system using
         * origin coordinates from the computer system or visa versa
         *
         * @param int $originx
         * @param int $originy
         * @param int $x
         * @param int $y
         * @param bool $toComp
         * @return array(int $x, int $y)
         */
        function translateCoordinate($originx, $originy, $x, $y, $toComp=true) {
            if($toComp) {
                $newx = $originx + $x;
                $newy = $originy - $y;
            } else {
                $newx = $x - $originx;
                $newy = $originy - $y;
            }
            
            return array($newx, $newy);
        }
    }
    ?>


    im guessing i add this one somewhere lol
    PHP Code:
       $urlImage = "images/image.png"; 
    $urlNewImage = "images/imageNew.png"; 
    
    Last edited by wapfuture; 18.02.10, 16:14.
Working...
X