php放大缩小图片

<?php
 
    class image{

        private $file;
        private $image;
        private $info;

        public function __construct($file){
            if(file_exists($file)){
                $info = getimagesize($file);
                $this->info = array(
                    'width'  => $info[0],
                    'height' => $info[1],
                    'bits'   => $info['bits'],
                    'mime'   => $info['mime']
                );
                $this->image = $this->create($file);
            }else{
                exit("coulde not load image");
            }
        }

        private function create($image){

            $mime = $this->info['mime'];

            if($mime == 'image/gif'){
               return  imagecreatefromgif($image);
            }elseif($mime == 'image/png'){
               return  imagecreatefrompng($image);
            }elseif($mime == 'image/jpeg'){
                return imagecreatefromjpeg($image);
            }
        }


        public function resize($width=0,$height=0,$default=''){

            if (!$this->info['width'] || !$this->info['height']) {
                return;
            }

            $xpos = 0;
            $ypos = 0;
            $scale = 1;

            $scale_w = $width / $this->info['width'];
            $scale_h = $height / $this->info['height'];

            print_r($this->info);

            if ($default == 'w') {
                $scale = $scale_w;
            } elseif ($default == 'h'){
                $scale = $scale_h;
            } else {
                $scale = min($scale_w, $scale_h);
            }

            if ($scale == 1 && $scale_h == $scale_w && $this->info['mime'] != 'image/png') {
                return;
            }

            $new_width = (int)($this->info['width'] * $scale);

            $new_height = (int)($this->info['height'] * $scale);

            $xpos = (int)(($width - $new_width) / 2);

            $ypos = (int)(($height - $new_height) / 2);

            $image_old = $this->image;
            $this->image = imagecreatetruecolor($width, $height);//建立的是一幅大小为 x和 y的黑色图像(默认为黑色)

            if (isset($this->info['mime']) && $this->info['mime'] == 'image/png') {
                imagealphablending($this->image, false);//设定图像的混色模式
                imagesavealpha($this->image, true);//设置标记以在保存PNG图像时保存完整的alpha通道信息
                $background = imagecolorallocatealpha($this->image, 255, 255, 255, 127);//为一幅图像分配颜色+alpha
                imagecolortransparent($this->image, $background);//将某个颜色定义为透明色
            } else {
                $background = imagecolorallocate($this->image, 255, 255, 255);//为一幅图像分配颜色
            }

            imagefilledrectangle($this->image, 0, 0, $width, $height, $background);//画一个矩形并填充

            imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->info['width'], $this->info['height']);//重采样复制部分图像并调整大小
            imagedestroy($image_old);

            $this->info['width']  = $width;
            $this->info['height'] = $height;


        }
        public function save($file,$quality = 90){

            $info = pathinfo($file);

            $extension = strtolower($info['extension']);

            if (is_resource($this->image)) {
                if ($extension == 'jpeg' || $extension == 'jpg') {
                    imagejpeg($this->image, $file, $quality);
                } elseif($extension == 'png') {
                    imagepng($this->image, $file);
                } elseif($extension == 'gif') {
                    imagegif($this->image, $file);
                }

                imagedestroy($this->image);
            }

        }

    }

    $root = $_SERVER['DOCUMENT_ROOT'].'/oimg/xts.jpg';
    $newroot = $_SERVER['DOCUMENT_ROOT'].'/nimg/xts.jpg';
    $image = new image($root);
    $image->resize(50,10);
    $image->save($newroot);
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值