加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php中的图像压缩

发布时间:2020-12-13 16:44:15 所属栏目:PHP教程 来源:网络整理
导读:我正在使用以下类压缩图像.. ?phpclass SimpleImage {var $image;var $image_type;function load($filename) { $image_info = getimagesize($filename); $this-image_type = $image_info[2]; if( $this-image_type == IMAGETYPE_JPEG ) { $this-image = imag
我正在使用以下类压缩图像..

<?php
class SimpleImage {

var $image;
var $image_type;

function load($filename) {

  $image_info = getimagesize($filename);
  $this->image_type = $image_info[2];
  if( $this->image_type == IMAGETYPE_JPEG ) {

     $this->image = imagecreatefromjpeg($filename);
  } elseif( $this->image_type == IMAGETYPE_GIF ) {

     $this->image = imagecreatefromgif($filename);
  } elseif( $this->image_type == IMAGETYPE_PNG ) {

     $this->image = imagecreatefrompng($filename);
  }
 }
 function save($filename,$image_type=IMAGETYPE_JPEG,$compression=75,$permissions=null){

  if( $image_type == IMAGETYPE_JPEG ) {
     imagejpeg($this->image,$filename,$compression);
  } elseif( $image_type == IMAGETYPE_GIF ) {

     imagegif($this->image,$filename);
  } elseif( $image_type == IMAGETYPE_PNG ) {

     imagepng($this->image,$filename);
  }
  if( $permissions != null) {

     chmod($filename,$permissions);
  }
}
function output($image_type=IMAGETYPE_JPEG) {

  if( $image_type == IMAGETYPE_JPEG ) {
     imagejpeg($this->image);
  } elseif( $image_type == IMAGETYPE_GIF ) {

     imagegif($this->image);
  } elseif( $image_type == IMAGETYPE_PNG ) {

     imagepng($this->image);
  }
}
function getWidth() {

  return imagesx($this->image);
}
 function getHeight() {

  return imagesy($this->image);
}
function resizeToHeight($height) {

  $ratio = $height / $this->getHeight();
  $width = $this->getWidth() * $ratio;
  $this->resize($width,$height);
}
function resizeToWidth($width) {
  $ratio = $width / $this->getWidth();
  $height = $this->getheight() * $ratio;
  $this->resize($width,$height);
}
function scale($scale) {
  $width = $this->getWidth() * $scale/100;
  $height = $this->getheight() * $scale/100;
  $this->resize($width,$height);
}
function resize($width,$height) {
  $new_image = imagecreatetruecolor($width,$height);
   imagecopyresampled($new_image,$this->image,$width,$height,$this->getWidth(),$this->getHeight());
  $this->image = $new_image;
}      

}

?>

我的PHP代码看起来像..

include_once(ROOT.'/library/SimpleImage.php')
$image = new SimpleImage();
$image->load($image_name);
$image->resizeToWidth(100);
$image->save($image_name);

现在的问题是我的图像被压缩了,但它们的原始颜色组合丢失了……
不知道该怎么办.. ??

请建议另一个类进行图像压缩.

解决方法

这是我很久以前编写的一个函数,它不是真正优化的,但它应该可以解决问题.适用于各种mime类型,并允许您将修改后的图像保存到新位置.如果您需要更少的功能,可以轻松简化它.

public function resizePicture($path,$new_path,$new_width,$new_height,$proportion = false) {
    $size = getimagesize($path);
    $x = 0;
    $y = 0;

    switch($size['mime']) {
        case 'image/jpeg':
            $picture = imagecreatefromjpeg($path);
        break;
        case 'image/png':
            $picture = imagecreatefrompng($path);
        break;
        case 'image/gif':
            $picture = imagecreatefromgif($path);
        break;
        default:
            return false;
        break;
    }

    $width = $size[0];
    $height = $size[1];

    $frame = imagecreatetruecolor($new_width,$new_height);
    if($size['mime'] == 'image/jpeg') {
        $bg = imagecolorallocate($frame,255,255);
        imagefill($frame,$bg);
    } else if($size['mime'] == 'image/gif' or $size['mime'] == 'image/png') {
        imagealphablending($picture,false);
        imagesavealpha($picture,true);
        imagealphablending($frame,false);
        imagesavealpha($frame,true);
    }

    if($width < $new_width and $height < $new_height) {
        $x = ($new_width - $width) / 2;
        $y = ($new_height - $height) / 2;
        imagecopy($frame,$picture,$x,$y,$height);
    } else {
        if($proportion and $width != $height) {
            if($width > $height) {
                $old_height = $new_height;
                $new_height = $height * $new_width / $width;
                $y = abs($old_height - $new_height) / 2;
            } else {
                $old_width = $new_width;
                $new_width = $width * $new_height / $height;
                $x = abs($old_width - $new_width) / 2;
            }
        }
        imagecopyresampled($frame,$height);
    }

    switch($size['mime']) {
        case 'image/jpeg':
            imagejpeg($frame,85);
        break;
        case 'image/png':
            imagepng($frame,8);
        break;
        case 'image/gif':
            imagegif($frame,$new_path);
        break;
        default:
            return false;
        break;
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读