- //创建缩略图
- function ResizeImage($im,$maxwidth,$maxheight,$name,$sldir,$type){
- $width = imagesx($im);
- $height = imagesy($im);
- if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
- if($maxwidth && $width > $maxwidth){
- $widthratio = $maxwidth/$width;
- $RESIZEWIDTH=true;
- }
- if($maxheight && $height > $maxheight){
- $heightratio = $maxheight/$height;
- $RESIZEHEIGHT=true;
- }
- if($RESIZEWIDTH && $RESIZEHEIGHT){
- if($widthratio < $heightratio){
- $ratio = $widthratio;
- }else{
- $ratio = $heightratio;
- }
- }elseif($RESIZEWIDTH){
- $ratio = $widthratio;
- }elseif($RESIZEHEIGHT){
- $ratio = $heightratio;
- }
- $newwidth = $width * $ratio;
- $newheight = $height * $ratio;
- if(function_exists("imagecopyresampled")){
- $newim = imagecreatetruecolor($newwidth, $newheight);
- imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
- }else{
- $newim = imagecreate($newwidth, $newheight);
- imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
- }
- ImageJpeg ($newim,$sldir.$name.".".$type);
- ImageDestroy ($newim);
- }else{
- ImageJpeg ($im,$sldir.$name.".".$type);
- }
- }
- function create_small_img($new_name,$width,$height,$file_dir,$file_name,$file_tmpname,$file_type){
- file_exists($file_dir) or mkdir($file_dir,0777);
- $imgtype = end(explode(".",$file_name));
- $new_imgname = $new_name.".".$imgtype;
- if($file_name){
- if($file_type == "image/pjpeg"){
- $im = imagecreatefromjpeg($file_tmpname);
- }elseif($file_type == "image/x-png"){
- $im = imagecreatefrompng($file_tmpname);
- }elseif($file_type == "image/gif"){
- $im = imagecreatefromgif($file_tmpname);
- }
- if($im){
- if(file_exists($file_dir.$new_imgname)){
- unlink($file_dir.$new_imgname);
- }
- ResizeImage($im,$width,$height,$new_name,$file_dir,$imgtype);
- ImageDestroy ($im);
- }
- }
- }
- //创建缩略图
- //用法
- create_small_img($store_img_new_name,$width,$height,$small_img_dir,$upload_img_name,$upload_img_temp,$upload_img_type);
PHP创建图片缩略图函数
最新推荐文章于 2024-08-10 10:59:41 发布