PHP图片缩略图处理
也是为了某项目的需要,自己写的一个,纯粹是为了记录下了,以后使用或者修改. 又是一个晚自习...
class images { public function ChangeSize($file,$new_file,$width,$height) { $res = $this->loadImgRes($file); if($res == false) { return false; } $originwidth = imagesx($res); $originheight = imagesy($res); $new_res = imagecreate($width,$height); if(imagecopyresampled($new_res,$res,0,0,0,0,$width,$height,$originwidth,$originheight)) { if($this->saveImg($new_res,$new_file)) { imagedestroy($res); imagedestroy($new_res); return true; } else { return false; } } else { return false; } } public function Thumbnail($file,$new_file,$max_width,$max_height) { $res = $this->loadImgRes($file); $x = imagesx($res); $y = imagesy($res); if($res == false) { return false; } if($x > $max_width || $y > $max_height) { $x_scale = (int)($x / $max_width); $y_scale = (int)($y / $max_height); $scale = $x_scale > $y_scale?$x_scale:$y_scale; $this->ChangeSize($file,$new_file,(int)($x / $scale),(int)($y / $scale)); } return true; } private function loadImgRes(&$file) { if(!file_exists($file)) { return false; } switch(strtolower($this->getFileType($file))) { case "gif": if(function_exists("imagecreatefromgif")) { return imagecreatefromgif($file); } else { return false; } case "png": if(function_exists("imagecreatefrompng")) { return imagecreatefrompng($file); } else { return false; } case "jpeg": if(function_exists("imagecreatefromjpeg")) { return imagecreatefromjpeg($file); } else { return false; } case "jpg": if(function_exists("imagecreatefromjpeg")) { return imagecreatefromjpeg($file); } else { return false; } default: return false; } } private function saveImg(&$res,$newfile) { switch(strtolower($this->getFileType($newfile))) { case "gif": @imagegif($res,$newfile); return true; case "png": @imagepng($res,$newfile); return true; case "jpeg": @imagejpeg($res,$newfile); return true; case "jpg": @imagejpeg($res,$newfile); return true; default: return false; } } private function getFileType($name) { $arr = explode('.',$name); return $arr[count($arr) - 1]; } }
路径一定要处理好,先前没处理好还以为是编码的问题...
$a = new images; $LF = "D:\WebServer\PHPnow\htdocs\u\\1.jpg"; $NF = "D:\WebServer\PHPnow\htdocs\u\\2.jpg"; $a->Thumbnail($LF,$NF,300,300);