名站导航为爱好php程序的朋友们提供php相关的教程知识。
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通道信息www.mzdh.net $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); ?>
好了关于php程序的知识就说到这里希望可以帮助需要的朋友。,微信大屏幕