function image_resize($from, $tw,$th){ $temp = array(1=>'gif', 2=>'jpeg', 3=>'png'); list($fw, $fh, $tmp) = getimagesize($from); if(!$temp[$tmp]){ return false; } $tmp = $temp[$tmp]; $infunc = "imagecreatefrom$tmp"; $outfunc = "image$tmp"; $fimg = $infunc($from); if($fw/$tw > $fh/$th){ $th = $tw*($fh/$fw); }else{ $tw = $th*($fw/$fh); } $timg = imagecreatetruecolor($tw, $th); imagecopyresampled($timg, $fimg, 0,0, 0,0, $tw,$th, $fw,$fh); $to = C('UPLOAD_PATH').C('UPLOAD_SAVE_DIR_RULE').'a_'.time().'_'.mt_rand(100,999).'.'.end(explode('.',$from)); $r = $outfunc($timg, $to); if($r){ unlink($from); return $to; }else{ return false; } } function cut_img($from,$zero=[0,0],$cutSize=[300,300]){ $temp = array(1=>'gif', 2=>'jpeg', 3=>'png'); list($fw, $fh, $tmp) = getimagesize($from); if(!$temp[$tmp]){ return false; } $tmp = $temp[$tmp]; $infunc = "imagecreatefrom$tmp"; $outfunc = "image$tmp"; $timg = $infunc($from); $cutImg=imagecreatetruecolor($cutSize[0], $cutSize[1]); imagecopy($cutImg,$timg,0,0,$zero[0], $zero[1],$cutSize[0], $cutSize[1]); $to = C('UPLOAD_PATH').C('UPLOAD_SAVE_DIR_RULE').'a_'.time().'_'.mt_rand(100,999).'.'.end(explode('.',$from)); $r = $outfunc($cutImg, $to); if($r){ unlink($from); return str_replace('../','/',$to); }else{ return false; } }
|