Press "Enter" to skip to content

PHP_图片类型转换和尺寸压缩

if($this->category == 16 && $suffix != 'gif' && ($this->force == 2)) {
                $this->file->move($destDir, $fileName);
                $filepathsrc = $destDir . $fileName;
                switch($suffix) {
                    case 'png':
                        $image = imagecreatefrompng($filepathsrc);
                        break;
                    case 'jpg':
                        $image = imagecreatefromjpeg($filepathsrc);
                        break;
                    case 'webp':
                        $image = imagecreatefromwebp($filepathsrc);
                        break;
                }

                list($width, $height) = getimagesize($filepathsrc);
                $fname = substr($filepathsrc,0, strrpos($filepathsrc, '.'));
                $newfilename = $fname . '.gif';

                if($width > self::$MAXWLIMIT || $height>self::$MAXWLIMIT) {
                    if($width > $height) {
                        $k = 1;
                    }else{
                        $k = 2;
                    }
                    $newwidth = self::$MAXWLIMIT;
                    $newheight = self::$MAXWLIMIT;
                    if($k == 1){
                        $newheight = round($height/$width  * $newwidth);
                    }else{
                        $newwidth =  round($width/$height  * $newheight);
                    }



                }else{
                    $newwidth = $width;
                    $newheight = $height;
                }
                $newImage = imagecreatetruecolor($newwidth, $newheight);
                $color = imagecolorallocatealpha($newImage,255,255,255, 127);
                imagecolortransparent($newImage, $color);
                imagefill($newImage,0,0,$color);
                // 在新的画布上复制调整后的图像
                imagecopyresampled($newImage, $image, 0, 0, 0, 0,$newwidth, $newheight, $width, $height);
                $res = imagegif($newImage,  $newfilename);
                if ($res) {
                    $filePath = $newfilename;
                    $extgif = 'gif';
                }
                imagedestroy($newImage);
                imagedestroy($image);

            }
发表评论