php生成缩略图 class ResizeImage { //图片类型 private $type; //实际宽度 private $width; //实际高度 private $height; //改变后的宽度 private $resize_width; //改变后的高度 private $resize_height; //是否裁图 private $cut; //源图象 private $srcimg; //目标图象地址 private $dstimg; //临时创建的图象 private $im; function construct($imgPath, $width, $height, $isCut, $savePath) { // $type; //实际宽度 // private $width; //实际高度 // private $height; //改变后的宽度 // private $resize_width; //改变后的高度 // private $resize_height; //是否裁图 // private $cut; //源图象 // private $srcimg; //目标图象地址 // private $dstimg; //临时创建的图象 // private $im; $this->srcimg = $imgPath; $this->resize_width = $width; $this->resize_height = $height; $this->cut = $isCut; //图片的类型 $this->type = strtolower(substr(strrchr($this->srcimg,"."),1)); //获取图像后缀名 //strrchr()查找字符在指定字符串中从左面开始的最后一次出现的位置,如果成功,返回该字符以及其后面的字符strrchr($this->srcimg,".")后缀名(加点) //substr(string,start,length)函数返回字符串的一部分。substr(strrchr($this->srcimg,"."),1)返回后缀名(不加点) //strtolower() 函数把字符串转换为小写 $this->initi_img(); //目标图象地址 $this -> dst_img($savePath); //-- $this->width = imagesx($this->im); $this->height = imagesy($this->im); //生成图象 $this->newimg(); ImageDestroy ($this->im); } private function newimg() { //改变后的图象的比例 $resize_ratio = ($this->resize_width)/($this->resize_height); //实际图象的比例 $ratio = ($this->width)/($this->height); if($this->cut) { //裁图 $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);//imagecreatetruecolor(int $width , int $height ) — 新建一个真彩色图像 if($this->type=="png") { imagefill($newimg, 0, 0, imagecolorallocatealpha($newimg, 0, 0, 0, 127));//imagecolorallocatealpha( resource $image , int $red , int $green , int $blue , int $alpha) // — 为一幅图像分配颜色 + alpha } if($ratio>=$resize_ratio) { //高度优先 imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height); } else { //宽度优先 imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio)); } } else { //不裁图 if($ratio>=$resize_ratio) { $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio); if($this->type=="png") { imagefill($newimg, 0, 0, imagecolorallocatealpha($newimg, 0, 0, 0, 127)); } imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height); } else { $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height); if($this->type=="png") { imagefill($newimg, 0, 0, imagecolorallocatealpha($newimg, 0, 0, 0, 127)); } imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height); } } if($this->type=="png") { imagesavealpha($newimg, true); imagepng ($newimg,$this->dstimg); } else { imagejpeg ($newimg,$this->dstimg); } } //初始化图象 private function initi_img(){ if($this->type=="jpg") {$this->im = imagecreatefromjpeg($this->srcimg); }//imagecreatefromjpeg(string $filename) — 由文件或 URL 创建一个新图象。 if($this->type=="gif") { $this->im = imagecreatefromgif($this->srcimg); } if($this->type=="png") { $this->im = imagecreatefrompng($this->srcimg); } } //图象目标地址 private function dst_img($dstpath) { $full_length = strlen($this->srcimg);//strlen() 函数返回字符串的长度。 $type_length = strlen($this->type); $name_length = $full_length-$type_length; $name= substr($this->srcimg,0,$name_length-1); $this->dstimg = $dstpath; } }
$filesuolue=new ResizeImage(); $imgPath='images/'.$path; $savepath=substr($path,5); $savepathm=substr($savepath,-4);//后缀名 $savepathk=substr($savepath,0,-4);//图片名 $savepathn='images/card/'.$savepathk.'_suo'.$savepathm; array_push($newpath,$savepathn); $filesuolue->construct($imgPath, 150, 130, true, $savepathn);
symfony生成json文件
//生成json $root=dirname($this->get('kernel')->getRootDir()); $json_path=$root."/web/card.json"; if(!file_exists($json_path)||count(file($json_path))==0){ $fp=fopen($json_path,"w"); $init_data=array( 'cardpath'=>$newpath ); $json_str =json_encode($init_data, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES); fwrite($fp,var_export($json_str,true)); }else{ $array_item=file($json_path); $this->get('logger')->info('ppp',[$array_item]); if($array_item[0]){ if(substr( $array_item[0], 0, 1 ) == "'") { //去掉$array[0]文件中第一个'和最后一个字符' $array_item[0] = substr($array_item[0],1,-1); } $array_tmpitem=json_decode($array_item[0],true); $num_sz = count($array_tmpitem); for($i=0;$i<count($newpath);$i++){ array_push($array_tmpitem['cardpath'],$newpath[$i]); } $json_str=json_encode($array_tmpitem, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES); $fpitem=fopen($json_path,'w'); fwrite($fpitem,var_export($json_str,true)); fclose($fpitem); } }