图像调整大小功能减少了尺寸但增加了文件大小

时间:2015-05-05 06:33:41

标签: php gd

此调整大小功能会增加文件大小(以kb为单位),同时图像的显示从750px减少到400px。怎么可能?

class imageProcessing{
 var $imageSizeX;
 var $imageSizeY;
 var $resizeX;
 var $resizeY;
 var $reduction;
 var $fileName;
 var $msg;
 var $image;
 var $imageType;
 var $myname;
 var $tag;

/**
** Class constructor to initialize the processing
**/
 function imageProcessing($imgName,$myname,$tag,$red=100){ // default thumbnail size is 100
 $this->reduction=$red;
 if(is_file($imgName)){
 if(file_exists($imgName)){
 $this->fileName=$imgName;
 $this->getSize();
 $this->setSize();
 //print $this->imageSizeX."==>".$this->resizeX."<br>";
 //print $this->imageSizeY."==>".$this->resizeY."<br>";
 $this->resizeIt();
 $this->saveImage("webcam/" . $myname . "/".$myname . "_" . $tag . ".jpg"); // to thumbnail
 //$this->saveImage($imgName); // to resized actual image
 }else{
 $this->errorState(0);
 }
 }else{
 $this->errorState(2);
 }
 }

/**
** Function to set target size for the final image
** This size is based on the aspect ratio of the actual image to maintain the image clarity
**/
 function setSize(){
 if($this->imageSizeY<=$this->imageSizeX){
 $ratio=$this->imageSizeY/$this->imageSizeX;
 $this->resizeX=$this->reduction;
 $this->resizeY=round($ratio*$this->reduction);
 }else{
 $ratio=$this->imageSizeX/$this->imageSizeY;
 $this->resizeY=$this->reduction;
 $this->resizeX=round($ratio*$this->reduction);
 }
 }

/**
** Function to identify the image and set the image quality for JPEG images
**/
 function saveImage($imgName){
 switch ($this->imageType){
 case "gif":
 imagegif($this->image,$imgName);
 break;
 case "jpg":
 imagejpeg($this->image,$imgName,100);
 break;
 case "png":
 imagepng($this->image,$imgName);
 break;
 }
 }

/**
**Function for fetching the parameters of the image. Identifying image type.
**/
 function getSize(){
 $imgParams=getimagesize($this->fileName);
 $this->imageSizeX=$imgParams[0];
 $this->imageSizeY=$imgParams[1];

 switch($imgParams[2]){
 case 1:
 $this->imageType="gif";
 $this->image = imagecreatefromgif($this->fileName);
 break;

 case 2:
 $this->imageType="jpg";
 $this->image = imagecreatefromjpeg($this->fileName);
 break;

 case 3:
 $this->imageType="png";
 $this->image = imagecreatefrompng($this->fileName);
 break;

 default:
 $this->errorState(1);
 }

 }

/**
**Function for resizing the image once the parameters are recorded.
**Creating true color template for the new image to maintain the quality of the image
**/
 function resizeIt(){

 if($this->imageSizeX<=$this->resizeX){
 $this->resizeX=$this->imageSizeX;
 }

 if($this->imageSizeY<=$this->resizeY){
 $this->resizeY=$this->imageSizeY;
 }
 $copy=imagecreatetruecolor($this->resizeX,$this->resizeY);
 if($copy){
 if(imagecopyresampled($copy,$this->image,0,0,0,0,$this->resizeX,$this->resizeY,$this->imageSizeX,
    $this->imageSizeY)){
 if(imagedestroy($this->image)){
 $this->image=$copy;
 }else{
 $this->errorState(6);
 }
 }else{
 $this->errorState(4);
 }
 }else{
 $this->errorState(5);
 }
 }

/**
**Function for error and displaying error messages.
**/
 function errorState($err){

 switch ($err){
 case 0:
 $this->msg="File Not Found.";
 break;
 case 1:
 $this->msg="Invalid File Type.";
 break;
 case 2:
 $this->msg="Invalid Input.";
 break;
 case 3:
 $this->msg="Could Not Save.";
 break;
 case 4:
 $this->msg="Could not Create Image.";
 break;
 case 5:
 $this->msg="Could not Create Blank File.";
 break;
 case 6:
 $this->msg="Unable to Destroy Old File.";
 break;
 }

 print $this->msg;
 exit();
 }
}

1 个答案:

答案 0 :(得分:2)

我想这是因为jpeg使用压缩。 当您将jpeg图像转换为PHP图像对象时,它将解压缩jpeg图像,然后当您将其保存为文件时,它将以75的压缩率保存它(从0到100) 0是最低的文件大小。)

有关更多信息,请参阅: http://php.net/manual/fr/function.imagejpeg.php