/** * Author : smallchicken * Time : 2009年6月8日16:46:05 * Last Time: 2010年5月5日 10:24:30 * mode 1 : 强制裁剪,生成图片严格按照需要,不足放大,超过裁剪,图片始终铺满 * mode 2 : 和1类似,但不足的时候 不放大 会产生补白,可以用png消除。 * mode 3 : 只缩放,不裁剪,保留全部图片信息,会产生补白, * mode 4 : 只缩放,不裁剪,保留全部图片信息,此时的参数只是限制了生成的图片的最大宽高,不产生补白 * mode 5 : 生成的图比例严格按照需要的比例,宽和高不超过给定的参数。 * 默认补白为白色,如果要使补白成透明像素,请使用SaveAlpha()方法代替SaveImage()方法 * * 调用方法: * * $ic=new ImageCrop('old.jpg','afterCrop.jpg'); * $ic->Crop(120,80,2); * $ic->SaveImage(); * //$ic->SaveAlpha();将补白变成透明像素保存 * $ic->destory(); * * */ function make_thumb($src, $dst, $width, $height, $mode) { $ic = new ImageCrop($src, $dst); $ic->Crop($width, $height, $mode); $ic->SaveImage(); $ic->destory(); }
/** * 生成并输出图片 * * @access public * @param mixed $src * @param mixed $dst * @param mixed $width * @param mixed $height * @param mixed $mode * @return void */ public function make_crop_thumb($src, $dst, $width, $height, $mode) { $ic = new ImageCrop($src, $dst); $ic->Crop($width, $height, $mode); list($width, $height, $type) = getimagesize($src); if ($type === IMAGETYPE_PNG) { $ic->SaveAlpha(); } else { $ic->SaveImage(); } $ic->destory(); }
//给画布分配颜色 imagefill($this->dImage, 0, 0, $bg); //给图像用颜色进行填充 imagecolortransparent($this->dImage, $bg); //背景定义成透明色 $ratio_w = 1.0 * $dst_width / $this->src_width; //横向缩放的比例 $ratio_h = 1.0 * $dst_height / $this->src_height; //纵向缩放的比例 //var_dump($this); //不进行缩放,直接对图像进行裁剪 $ratio = 1.0; $tmp_w = (int) ($dst_width / $ratio); $tmp_h = (int) ($dst_height / $ratio); $tmp_img = imagecreatetruecolor($dst_width, $dst_height); //创建暂时保存的画布 imagecopy($tmp_img, $this->sImage, 0, 0, $dst_x, $dst_y, $dst_width, $dst_height); //拷贝出图像的一部分,进行裁切 imagecopyresampled($this->dImage, $tmp_img, 0, 0, 0, 0, $dst_width, $dst_height, $tmp_w, $tmp_h); //把暂时缓存的图片,放到目标文件里面 imagedestroy($tmp_img); } } $pathToFile = "E:/www2/GitHub/php_lib_code_center/qq_touxiang.jpg"; $targetFile = "E:/www2/GitHub/php_lib_code_center/" . time() . ".jpg"; $ic = new ImageCrop($pathToFile, $targetFile); //$ic->Cut(40,30,120,130); $ic->Crop(100, 100, 3, $targetFile); $ic->SaveImage(); //$ic->SaveAlpha();将补白变成透明像素保存 $ic->destory();