php 生成压缩图片

思路:将图片先上传到服务器,然后再进行图片压缩

下面提供两种方案:

方案一:使用GD类库,这个默认不支持JPG,需要额外安装,自己检查。(优点不用安装扩展,但处理gif动图无法处理,压缩完就变成静态图片;)

方案二:使用Imagick扩展(优点:可以处理gif动图,缺点需要安装扩展,建议还是这种的,Imagick更加强大。扩展链接:https://pecl.php.net/package/imagick)

方案一代码:

// 压缩图片
    public function resizeImage($file_path, $maxwidth = 480, $maxheight = 480, $name = '')
    {
        try {
            if (!file_exists($file_path)) {
                return false;
            }
            $info = pathinfo($file_path);
            $dirname = $info['dirname'];
            $ext = strtolower($info['extension']);
            if ($name) {
                $filname = $dirname. '/'. $name.'.' . $info['extension'];
            } else {
                $filname = $dirname. '/'. $info['filename'] . 's.' . $info['extension'];
            }
            try {
                if($ext == "jpeg"||$ext == "jpg" )
                {
                    $im = imagecreatefromjpeg($file_path);
                }
                elseif($ext == "png")
                {
                    $im = imagecreatefrompng($file_path);
                }
                elseif($ext == "gif")
                {
                    $im = imagecreatefromgif($file_path);
                }
                elseif($ext == "bmp")
                {
                    $im = imagecreatefrombmp($file_path);
                }
                else//默认jpg
                {
                    $im = imagecreatefromjpeg($file_path);
                }
            } catch (\Exception $exception) { //处理那些自己手动改文件格式的,比如png改成jpg
                try {
                    $im = imagecreatefromjpeg($file_path);
                } catch (\Exception $exception) {
                    try {
                        $im = imagecreatefrompng($file_path);
                    }catch (\Exception $exception) {
                        try {
                            $im = imagecreatefromgif($file_path);
                        } catch (\Exception $exception) {
                            try {
                                $im = imagecreatefrombmp($file_path);
                            } catch (\Exception $exception) {
                            }
                        }
                    }
                }
            }

            //取得当前图片大小
            $width = imagesx($im);
            $height = imagesy($im);
            //生成缩略图的大小
            if(($width > $maxwidth) || ($height > $maxheight))
            {
                $widthratio = $maxwidth/$width;
                $heightratio = $maxheight/$height;

                if($widthratio < $heightratio)
                {
                    $ratio = $widthratio;
                }
                else
                {
                    $ratio = $heightratio;
                }
                $newwidth = $width * $ratio;
                $newheight = $height * $ratio;

                if(function_exists("imagecopyresampled"))
                {
                    $uploaddir_resize = imagecreatetruecolor($newwidth, $newheight);
                    imagecopyresampled($uploaddir_resize, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
                }
                else
                {
                    $uploaddir_resize = imagecreate($newwidth, $newheight);
                    imagecopyresized($uploaddir_resize, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
                }
                if($ext == "jpeg"||$ext == "jpg" ) ImageJpeg ($uploaddir_resize, $filname);
                elseif($ext == "png") ImagePng($uploaddir_resize, $filname);
                elseif($ext == "gif") ImageGif($uploaddir_resize, $filname);
                elseif($ext == "bmp") ImageBmp($uploaddir_resize, $filname);
                else ImageJpeg ($uploaddir_resize, $filname);

                ImageDestroy ($uploaddir_resize);
            }
            else
            {
                if($ext == "jpeg"||$ext == "jpg" ) ImageJpeg ($im, $filname);
                elseif($ext == "png") ImagePng($im, $filname);
                elseif($ext == "gif") ImageGif($im, $filname);
                elseif($ext == "bmp") ImageBmp($im, $filname);
                else ImageJpeg ($im, $filname);
            }
            return $filname;
        } catch (\Exception $exception) {
            return false;
        }
    }

方案二代码:

 public function resizeImage($file_path, $maxwidth = 480, $maxheight = 480, $name = '')
    {
        try {
            if (!file_exists($file_path)) {
                return false;
            }
            $info = pathinfo($file_path);
            $dirname = $info['dirname'];
            $ext = strtolower($info['extension']);
            if ($name) {
                $filname = $dirname. '/'. $name.'.' . $ext;
            } else {
                $filname = $dirname. '/'. $info['filename'] . 's.' . $ext;
            }
            $im = new \Imagick($file_path);
            $width = $newwidth = $im->getImageWidth();
            $height = $newheight = $im->getImageHeight();
            //生成缩略图的大小
            if(($width > $maxwidth) || ($height > $maxheight))
            {
                $widthratio = $maxwidth/$width;
                $heightratio = $maxheight/$height;

                if($widthratio < $heightratio)
                {
                    $ratio = $widthratio;
                }
                else
                {
                    $ratio = $heightratio;
                }
                $newwidth = $width * $ratio;
                $newheight = $height * $ratio;
            }
            foreach ($im as $frame) {
                $frame->thumbnailImage($newwidth, $newheight);
                $frame->setImagePage($newwidth, $newheight, 0, 0);
            }
            $im->writeImages($filname, true);
            return $filname;
        } catch (\Exception $exception) {
            return false;
        }
    }

 

你可能感兴趣的:(php)