<?php

if ( ! @include_once( './mainfile.php' ) ) { exit( '!! INCLUDE ERROR. ACCESS DENIED !!' ); }
error_reporting( _E_VALUE );


/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | myPHPNuke Version 188_8 RC2                                          |
// +----------------------------------------------------------------------+
// | License: GNU/GPL - http://www.gnu.org/copyleft/gpl.html              |
// +----------------------------------------------------------------------+
// | Support: http://myphpnuke.com/                                       |
// +----------------------------------------------------------------------+
// | Author: myPHPNuke Dev Team                                           |
// +----------------------------------------------------------------------+
// | Copyright: © 2001-2004 myPHPNuke Dev Team                            |
// +----------------------------------------------------------------------+
// $Id: imageFunctions.php,v 1.6.2.2 2004/04/18 12:47:18 ajvwaay Exp $

function RatioResizeImg( $src_file, $dest_file, $newWidth )
{
    global $gallerypath, $imageSoftware;

    switch ($imageSoftware)
    {
        case 'GD':
        return RatioResizeImgGD($src_file, $dest_file, $newWidth);
        break;

        case 'ImageMagick':
        return RatioResizeImgImageMagick($src_file, $dest_file, $newWidth);
        break;

        case 'none':
        return $src_file;
        break;

        case 'browser':
        return 'browser';
        break;

        default:
        return $src_file;
        break;
    }
}


// GD Library
function RatioResizeImgGD($src_file, $dest_file, $newWidth)
{
    global $gallerypath;

    // find the image size & type

    // Assume gd version is 1.x.x
    $gd_version = 1;

    // Try to figure out gd version and over write previous value if need be.
    if (function_exists('gd_info'))
    {
        $gd_info = gd_info();
        preg_match('/[\d]/', $gd_info['GD Version'], $version);
        if (isset($version[0]))
        {
            switch( $version[0] )
            {
                case '2':
                    $gd_version = 2;
                    break;

                case '1':
                default:
                    $gd_version = 1;
                    break;
             }
        }
    }

    if (($gd_version == 2 && !function_exists('imagecreatetruecolor')) || ($gd_version == 1 && !function_exists('imagecreate')))
    {
        return $src_file;
    }

    $imginfo = @getimagesize($src_file);

    switch ($imginfo[2])
    {
        case 1:
            $type = IMAGETYPE_GIF;
            break;

        case 2:
            $type = IMAGETYPE_JPEG;
            break;

        case 3:
            $type = IMAGETYPE_PNG;
            break;

//        case 4:
//            $type = IMAGETYPE_SWF;
//            break;

        case 15:
            $type = IMAGETYPE_WBMP;
            break;

        default:
            return $src_file;
            break;
    }

    switch($type)
    {
        case IMAGETYPE_GIF:
            if (!function_exists('imagecreatefromgif'))
            {
                return $src_file;
            }

            $srcImage = @imagecreatefromgif($src_file);
            break;

        case IMAGETYPE_JPEG:
            if (!function_exists('imagecreatefromjpeg'))
            {
                return $src_file;
            }

            $srcImage = @imagecreatefromjpeg($src_file);
            break;

        case IMAGETYPE_PNG:
            if (!function_exists('imagecreatefrompng'))
            {
                return $src_file;
            }

            $srcImage = @imagecreatefrompng($src_file);
            break;

        case IMAGETYPE_WBMP:
            if (!function_exists('imagecreatefromwbmp'))
            {
                return $src_file;
            }

            $srcImage = @imagecreatefromwbmp($src_file);
            break;

	default:
            return $src_file;
    }

    if (isset($srcImage))
    {
        // height/width
        $srcWidth = $imginfo[0];
        $srcHeight = $imginfo[1];
        $ratioWidth = $srcWidth/$newWidth;
        $destWidth = $newWidth;
        $destHeight = $srcHeight / $ratioWidth;

	// resize
        switch( $gd_version )
        {
            case 2 && $type != IMAGETYPE_GIF:
                 $destImage = @imagecreatetruecolor($destWidth, $destHeight);
                 imagecopyresampled($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight);
                 break;

            case 1:
            default:
                $destImage = @imagecreate($destWidth, $destHeight);
                imagecopyresized($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight);
        }

	// create and save final picture

        switch($type)
        {
            case IMAGETYPE_GIF:
                @imagegif($destImage, $dest_file);
                break;

            case IMAGETYPE_JPEG:
                @imagejpeg($destImage, $dest_file);
                break;

            case IMAGETYPE_PNG:
                @imagepng($destImage, $dest_file);
                break;

            case IMAGETYPE_WBMP:
                @imagewbmp($destImage, $dest_file);
                break;
        }

        // free the memory
	@imagedestroy($srcImage);
	@imagedestroy($destImage);

        // chmod the thumbs
        @chmod($dest_file, 0666);

	return $dest_file;
    }
    else
    {
	return $src_file;
    }
}


function RatioResizeImgImageMagick($src_file, $dest_file, $newWidth)
{
    global $gallerypath;
    // find the image size
    $imginfo = @getimagesize($src_file);
    if ($imginfo == NULL)
    {
        return $src_file;
    }

    // height/width
    $srcWidth = $imginfo[0];
    $srcHeight = $imginfo[1];

    $ratioWidth = $srcWidth/$newWidth;
    $ratioHeight = 0;
    $destWidth = $newWidth;
    $destHeight = $srcHeight / $ratioWidth;

    // resize
    exec("convert -quality 80 -antialias -sample \"".$destWidth."x".$destHeight."\" \"$src_file\" \"$dest_file\"");
    return $dest_file;
}

?>
