使用PHP调整IMG大小-最小分辨率

使用PHP调整IMG大小-最小分辨率,php,image,upload,resize,Php,Image,Upload,Resize,我使用下面的代码来调整上传后的图像大小。。 请帮助我,告诉我如何改变它,以调整图像大小,但保持最低分辨率。现在上传图片调整到宽度或高度500px,但至少需要1200px define("HAR_AUTO_NAME",1); Class RESIZEIMAGE { var $imgFile=""; var $imgWidth=0; var $imgHeight=0; var $imgType=""; var $imgAttr=""; var $typ

我使用下面的代码来调整上传后的图像大小。。 请帮助我,告诉我如何改变它,以调整图像大小,但保持最低分辨率。现在上传图片调整到宽度或高度500px,但至少需要1200px

define("HAR_AUTO_NAME",1);
Class RESIZEIMAGE
{
    var $imgFile="";
    var $imgWidth=0;
    var $imgHeight=0;
    var $imgType="";
    var $imgAttr="";
    var $type=NULL;
    var $_img=NULL;
    var $_error="";

    /**
     * Constructor
     *
     * @param [String $imgFile] Image File Name
     * @return RESIZEIMAGE (Class Object)
     */

    function RESIZEIMAGE($imgFile="")
    {
        if (!function_exists("imagecreate"))
        {
            $this->_error="Error: GD Library is not available.";
            return false;
        }

        $this->type=Array(1 => 'GIF', 2 => 'JPG', 3 => 'PNG', 4 => 'SWF', 5 => 'PSD', 6 => 'BMP', 7 => 'TIFF', 8 => 'TIFF', 9 => 'JPC', 10 => 'JP2', 11 => 'JPX', 12 => 'JB2', 13 => 'SWC', 14 => 'IFF', 15 => 'WBMP', 16 => 'XBM');
        if(!empty($imgFile))
            $this->setImage($imgFile);
    }
    /**
     * Error occured while resizing the image.
     *
     * @return String
     */
    function error()
    {
        return $this->_error;
    }

    /**
     * Set image file name
     *
     * @param String $imgFile
     * @return void
     */
    function setImage($imgFile)
    {
        $this->imgFile=$imgFile;
        return $this->_createImage();
    }
    /**
     *
     * @return void
     */
    function close()
    {
        return @imagedestroy($this->_img);
    }
    /**
     * Resize a image to given width and height and keep it's current width and height ratio
     *
     * @param Number $imgwidth
     * @param Numnber $imgheight
     * @param String $newfile
     */
    function resize_limitwh($imgwidth,$imgheight,$newfile=NULL)
    {
        $image_per = 100;
        list($width, $height, $type, $attr) = @getimagesize($this->imgFile);
        if($width != $imgwidth && $imgwidth > 0)
            $image_per = (double)(($imgwidth * 100) / $width);

        if(floor(($height * $image_per)/100)!=$imgheight && $imgheight > 0)
            $image_per = (double)(($imgheight * 100) / $height);

        $this->resize_percentage($image_per,$newfile);

    }
    function resize_maxsize($maxsize,$newfile=NULL)
    {
        $image_per = 100;
        list($width, $height, $type, $attr) = @getimagesize($this->imgFile);

        if($width>$height && $maxsize>0){
            $image_per = (double)(($maxsize/$width)*100);
        }
        if($width<$height && $maxsize>0){
            $image_per = (double)(($maxsize/$height)*100);
        }

        $this->resize_percentage($image_per,$newfile);

    }
    /**
     * Resize an image to given percentage.
     *
     * @param Number $percent
     * @param String $newfile
     * @return Boolean
     */
    function resize_percentage($percent=100,$newfile=NULL)
    {
        $newWidth=($this->imgWidth*$percent)/100;
        $newHeight=($this->imgHeight*$percent)/100;
        return $this->resize($newWidth,$newHeight,$newfile);
    }

    function resize_scale($maxwidth, $maxheight, $newfile=NULL)
    {
      list($width, $height, $type, $attr) = @getimagesize($this->imgFile);

      if ($width < $maxwidth &&
          $height < $maxheight)
      {
        return $this->resize($width, $height, $newfile);
      }

      $oldprop = round($width / $height, 2);
  $newprop = round($maxwidth / $maxheight, 2);
  $preserveAspectx = round($width / $maxwidth, 2);
  $preserveAspecty = round($height / $maxheight, 2);

  if ($preserveAspectx < $preserveAspecty)
  {
    $newwidth  = $width/($height/$maxheight);
    $newheight = $maxheight;
  }
  else
  {
    $newwidth  = $maxwidth;
    $newheight = $height/($width/$maxwidth);
  }

  return $this->resize($newwidth,$newheight,$newfile);
    }
    /**
     * Resize an image to given X and Y percentage.
     *
     * @param Number $xpercent
     * @param Number $ypercent
     * @param String $newfile
     * @return Boolean
     */
    function resize_xypercentage($xpercent=100,$ypercent=100,$newfile=NULL)
    {
        $newWidth=($this->imgWidth*$xpercent)/100;
        $newHeight=($this->imgHeight*$ypercent)/100;
        return $this->resize($newWidth,$newHeight,$newfile);
    }

    /**
     * Resize an image to given width and height
     *
     * @param Number $width
     * @param Number $height
     * @param String $newfile
     * @return Boolean
     */
    function resize($width,$height,$newfile=NULL)
    {
        if(empty($this->imgFile))
        {
            $this->_error="File name is not initialised.";
            return false;
        }
        if($this->imgWidth<=0 || $this->imgHeight<=0)
        {
            $this->_error="Could not resize given image";
            return false;
        }
        if($width<=0)
            $width=$this->imgWidth;
        if($height<=0)
            $height=$this->imgHeight;

        return $this->_resize($width,$height,$newfile);
    }

    /**
     * Get the image attributes
     * @access Private
     *
     */
    function _getImageInfo()
    {
        @list($this->imgWidth,$this->imgHeight,$type,$this->imgAttr)=@getimagesize($this->imgFile);
        $this->imgType=$this->type[$type];
    }

    /**
     * Create the image resource
     * @access Private
     * @return Boolean
     */
    function _createImage()
    {
        $this->_getImageInfo();
        if($this->imgType=='GIF')
        {
            $this->_img=@imagecreatefromgif($this->imgFile);
        }
        elseif($this->imgType=='JPG')
        {
            $this->_img=@imagecreatefromjpeg($this->imgFile);
        }
        elseif($this->imgType=='PNG')
        {
            $this->_img=@imagecreatefrompng($this->imgFile);
        }
        if(!$this->_img || !@is_resource($this->_img))
        {
            $this->_error="Error loading ".$this->imgFile;
            return false;
        }
        return true;
    }

    /**
     * Function is used to resize the image
     *
     * @access Private
     * @param Number $width
     * @param Number $height
     * @param String $newfile
     * @return Boolean
     */
    function _resize($width,$height,$newfile=NULL)
    {
        if (!function_exists("imagecreate"))
        {
            $this->_error="Error: GD Library is not available.";
            return false;
        }

        $newimg=@imagecreatetruecolor($width,$height);
        //imagecolortransparent( $newimg, imagecolorat( $newimg, 0, 0 ) );

        if($this->imgType=='GIF' || $this->imgType=='PNG')
        {
            /** Code to keep transparency of image **/
            $colorcount = imagecolorstotal($this->_img);
            if ($colorcount == 0) $colorcount = 256;
            imagetruecolortopalette($newimg,true,$colorcount);
            imagepalettecopy($newimg,$this->_img);
            $transparentcolor = imagecolortransparent($this->_img);
            imagefill($newimg,0,0,$transparentcolor);
            imagecolortransparent($newimg,$transparentcolor);
        }

        @imagecopyresampled ( $newimg, $this->_img, 0,0,0,0, $width, $height, $this->imgWidth,$this->imgHeight);



        if($newfile===HAR_AUTO_NAME)
        {
            if(@preg_match("/\..*+$/",@basename($this->imgFile),$matches))
                $newfile=@substr_replace($this->imgFile,"_har",-@strlen($matches[0]),0);
        }
        elseif(!empty($newfile))
        {
            if(!@preg_match("/\..*+$/",@basename($newfile)))
            {
                if(@preg_match("/\..*+$/",@basename($this->imgFile),$matches))
                   $newfile=$newfile.$matches[0];
            }
        }

        if($this->imgType=='GIF')
        {
            if(!empty($newfile))
                @imagegif($newimg,$newfile);
            else
            {
                @header("Content-type: image/gif");
                @imagegif($newimg);
            }
        }
        elseif($this->imgType=='JPG')
        {
            if(!empty($newfile))
                @imagejpeg($newimg,$newfile);
            else
            {
                @header("Content-type: image/jpeg");
                @imagejpeg($newimg);
            }
        }
        elseif($this->imgType=='PNG')
        {
            if(!empty($newfile))
                @imagepng($newimg,$newfile);
            else
            {
                @header("Content-type: image/png");
                @imagepng($newimg);
            }
        }
        @imagedestroy($newimg);
    }
}
define(“HAR\u AUTO\u NAME”,1);
类大小图像
{
var$imgFile=“”;
var$imgWidth=0;
var$imgHeight=0;
var$imgType=“”;
var$imgAttr=“”;
var$type=NULL;
var$\u img=NULL;
var$_error=“”;
/**
*建造师
*
*@param[String$imgFile]图像文件名
*@return RESIZEIMAGE(类对象)
*/
函数RESIZEIMAGE($imgFile=”“)
{
如果(!函数_存在(“imagecreate”))
{
$this->\u error=“错误:GD库不可用。”;
返回false;
}
$this->type=Array(1=>'GIF',2=>'JPG',3=>'PNG',4=>'SWF',5=>'PSD',6=>'BMP',7=>'TIFF',8=>'TIFF',9=>'JPC',10=>'JP2',11=>'JPX',12=>'JB2',13=>'SWC',14=>'IFF',15=>'WBMP',16=>'XBM等);
如果(!空($imgFile))
$this->setImage($imgFile);
}
/**
*调整图像大小时出错。
*
*@返回字符串
*/
函数错误()
{
返回$this->\u错误;
}
/**
*设置图像文件名
*
*@param String$imgFile
*@返回无效
*/
函数setImage($imgFile)
{
$this->imgFile=$imgFile;
返回$this->_createImage();
}
/**
*
*@返回无效
*/
函数关闭()
{
return@imagedestroy($this->\u img);
}
/**
*将图像调整为给定的宽度和高度,并保持其当前的宽度和高度比
*
*@param Number$imgwidth
*@param number$imgheight
*@param String$newfile
*/
函数resize_limitwh($imgwidth,$imgheight,$newfile=NULL)
{
$image_/=100;
列表($width、$height、$type、$attr)=@getimagesize($this->imgFile);
如果($width!=$imgwidth&&$imgwidth>0)
$image_per=(双精度)($imgwidth*100)/$width);
如果(地板(($height*$image_per)/100)!=$imgheight&&$imgheight>0)
$image_per=(双倍)($imghight*100)/$height);
$this->resize_percentage($image_per,$newfile);
}
函数resize\u maxsize($maxsize,$newfile=NULL)
{
$image_/=100;
列表($width、$height、$type、$attr)=@getimagesize($this->imgFile);
如果($width>$height&$maxsize>0){
$image_per=(双精度)($maxsize/$width)*100);
}
如果($0){
$image_per=(双倍)($maxsize/$height)*100);
}
$this->resize_percentage($image_per,$newfile);
}
/**
*将图像大小调整为给定的百分比。
*
*@param Number$percent
*@param String$newfile
*@返回布尔值
*/
函数调整大小\u百分比($percent=100,$newfile=NULL)
{
$newWidth=($this->imgWidth*$percent)/100;
$newHeight=($this->imgHeight*$percent)/100;
返回$this->resize($newWidth、$newHeight、$newfile);
}
函数resize\u scale($maxwidth、$maxheight、$newfile=NULL)
{
列表($width、$height、$type、$attr)=@getimagesize($this->imgFile);
如果($width<$maxwidth&&
$height<$maxheight)
{
返回$this->resize($width,$height,$newfile);
}
$oldprop=圆形($width/$height,2);
$newprop=round($maxwidth/$maxheight,2);
$preserveApectX=圆形($width/$maxwidth,2);
$preserveApecty=圆形($height/$maxheight,2);
如果($preserveApectX<$preserveApectY)
{
$newwidth=$width/($height/$maxheight);
$newheight=$maxheight;
}
其他的
{
$newwidth=$maxwidth;
$newheight=$height/($width/$maxwidth);
}
返回$this->resize($newwidth、$newheight、$newfile);
}
/**
*将图像大小调整为给定的X和Y百分比。
*
*@param Number$xpercent
*@param Number$ypercent
*@param String$newfile
*@返回布尔值
*/
函数大小调整百分比($xpercent=100,$ypercent=100,$newfile=NULL)
{
$newWidth=($this->imgWidth*$xpercent)/100;
$newHeight=($this->imgHeight*$ypercent)/100;
返回$this->resize($newWidth、$newHeight、$newfile);
}
/**
*将图像调整为给定的宽度和高度
*
*@param Number$width
*@param Number$height
*@param String$newfile
*@返回布尔值
*/
函数resize($width,$height,$newfile=NULL)
{
if(空($this->imgFile))
{
$this->\u error=“文件名未初始化。”;
返回false;
}
如果($this->imgWidthimgHeight\u error=“无法调整给定图像的大小”;
返回false;
}
如果($widthmgwidth;
如果($heightimghight;
返回$this->\u resize($width、$height、$newfile);
}
/**
*获取图像属性
*@access-Private
*
*/
函数_getImageInfo()
{
@list($this->imgWidth,$this->imgHeight,$type,$this->imgAttr)=@getimagesize($this->imgFile);
$this->imgType=$this->type[$type];
}
/**
*创建图像资源
*@access-Private
*@返回布尔值
*/
函数_createImage()
{
$this->_getImageInfo();
如果($this->imgType=='GIF')
{
$this->\u img=@imagecreatefromformgif($this->imgFile);
}
elseif($this->imgType=='JPG')
{
$this->\u img=@imagecreatefromjpeg($this->imgFile);
}
elseif($this->imgType=='PNG')
{
$this->_img=@imagecreatefrompng($this->imgFile);
}
如果(!$this->\u img | |!@is_资源($this->\u img))
{
$this->\u error=“错误加载”。$this->imgFile;
返回false;
}
返回true;
}
/**
*函数用于调整图像的大小
*
*@access-Private
*@param Number$width