php 将图片保存为不同规格的图片
2012-12-20 19:46:10 来源:WEB开发网核心提示:2.get_spec_img()调用图片类,然后再用下面的方法保存不同规格的图片并返回图片连接//获取相应规格的图片地址//gen=0:保持比例缩放,不剪裁,如高为0,php 将图片保存为不同规格的图片(2),则保证宽度按比例缩放 gen=1:保证长宽,剪裁function get_spec_image($img_p
2.get_spec_img()调用图片类,然后再用下面的方法保存不同规格的图片并返回图片连接
//获取相应规格的图片地址 //gen=0:保持比例缩放,不剪裁,如高为0,则保证宽度按比例缩放 gen=1:保证长宽,剪裁 function get_spec_image($img_path,$width=0,$height=0,$gen=0,$is_preview=true) { if($width==0) $new_path = $img_path; else { $img_name = substr($img_path,0,-4); $img_ext = substr($img_path,-3); if($is_preview) $new_path = $img_name."_".$width."x".$height.".jpg"; else $new_path = $img_name."o_".$width."x".$height.".jpg"; if(!file_exists($new_path)) { require_once "imagecls.php"; $imagec = new imagecls(); $thumb = $imagec->thumb($img_path,$width,$height,$gen,true,"",$is_preview); if(app_conf("PUBLIC_DOMAIN_ROOT")!='') { $paths = pathinfo($new_path); $path = str_replace("./","",$paths['dirname']); $filename = $paths['basename']; $pathwithoupublic = str_replace("public/","",$path); $file_data = @file_get_contents($path.$file); $img = @imagecreatefromstring($file_data); if($img!==false) { $save_path = "public/".$path; if(!is_dir($save_path)) { @mk_dir($save_path); } @file_put_contents($save_path.$name,$file_data); } } } } return $new_path; }
3.使用方法:
//im:将店铺图片保存为3种规格:小图:48x48,中图120x120,大图200x200 $small_url=get_spec_image($data['image'],48,48,0); $<span id="result_box" class="short_text" lang="en"><span>middle_url</span></span>=get_spec_image($data['image'],120,120,0); $big_url=get_spec_image($data['image'],200,200,0);
更多精彩
赞助商链接