php 生成图像的步骤:
imagecreatefromjpeg()//读取原图片文件,解析成图片
ImageCreateTrueColor();//创建一个黑色背景图片
imagecopyresampled();//将原来图片的部分提取出来生成目标图片
imagejpeg();目标图片生成文件。
图片方法文档:http://php.net/manual/zh/ref.image.php
掏中并且压缩类
class lswimage { private $filename; private $ext; private $x; private $y; private $x1; private $y1; private $width; private $height; private $dst_name; private $jpeg_quality = 90; private $mark_img="/data/water.png"; /** * 截取并且压缩图片 陈荣达 * @param type $filename 原来文件 * @param type $dst_w 压缩的宽度 * @param type $dst_h 压缩的高度 * @param type $dst_name 压缩的命名:比如y_480_***.jpg, 只需要赋予$dst_name="y_480"即可 */ public function index($filename, $dst_w, $dst_h, $dst_name) { //初始化 $this->width = $dst_w; $this->height = $dst_h; $this->dst_name = $dst_name; //判断比例 以便截取 $proportion = $dst_h / $dst_w; //获取待处理图片信息 $image_info = getimagesize($filename); //算出需要的高度 $need_height = $image_info['0'] * $proportion; //进行比较 if ($image_info['1'] > $need_height) {//实际的高比需要的高大 $src_y = ($image_info['1'] - $need_height) / 2; $src_x = 0; $src_h = $need_height; $src_w = $image_info['0']; } else if ($image_info['0'] < $need_height) {//实际的高比需要的高小 $need_width = $image_info['1'] / $proportion; $src_y = 0; $src_x = ($image_info['0'] - $need_width) / 2; $src_h = $image_info['1']; $src_w = $need_width; } else {//实际的高与需要的高相等,那么就不需要截取了,直接压缩 $src_y = 0; $src_x = 0; $src_h = $image_info['1']; $src_w = $image_info['0']; } $this->initialize($filename, $src_x, $src_y, $src_w, $src_h); $this->generate_shot(); } /** * 给图片添加水印 * @param filepath $src 待处理图片 * @param filepath $mark_img 水印图片路径 * @param string $position 水印位置 lt左上 rt右上 rb右下 lb左下 其余取值为中间 * @param int $quality jpg图片质量,仅对jpg有效 默认85 取值 0-100之间整数 * @param int $pct 水印图片融合度(透明度) * * @return void */ public function water_mark($src, $position = 'rb', $pct = 80) { $mark_img= $this->mark_img; $quality= $this->jpeg_quality; if (function_exists('imagecopy') && function_exists('imagecopymerge')) { $data = getimagesize($src); if ($data[2] > 3) { return false; } $src_width = $data[0]; $src_height = $data[1]; $src_type = $data[2]; $data = getimagesize($mark_img); $mark_width = $data[0]; $mark_height = $data[1]; $mark_type = $data[2]; if ($src_width < ($mark_width + 20) || $src_width < ($mark_height + 20)) { return false; } switch ($src_type) { case 1: $src_im = imagecreatefromgif($src); $imagefunc = function_exists('imagejpeg') ? 'imagejpeg' : ''; break; case 2: $src_im = imagecreatefromjpeg($src); $imagefunc = function_exists('imagegif') ? 'imagejpeg' : ''; break; case 3: $src_im = imagecreatefrompng($src); $imagefunc = function_exists('imagepng') ? 'imagejpeg' : ''; break; } switch ($mark_type) { case 1: $mark_im = imagecreatefromgif($mark_img); break; case 2: $mark_im = imagecreatefromjpeg($mark_img); break; case 3: $mark_im = imagecreatefrompng($mark_img); break; } switch ($position) { case 'lt': $x = 10; $y = 10; break; case 'rt': $x = $src_width - $mark_width - 10; $y = 10; break; case 'rb': $x = $src_width - $mark_width - 10; $y = $src_height - $mark_height - 10; break; case 'lb': $x = 10; $y = $src_height - $mark_height - 10; break; default: $x = ($src_width - $mark_width - 10) / 2; $y = ($src_height - $mark_height - 10) / 2; break; } if (function_exists('imagealphablending')) imageAlphaBlending($mark_im, true); // imageCopyMerge($src_im, $mark_im, $x, $y, 0, 0, $mark_width, $mark_height, $pct); imagecopy($src_im, $mark_im, $x, $y, 0, 0, $mark_width, $mark_height); if ($src_type == 2) { $imagefunc($src_im, $src, $quality); } else { $imagefunc($src_im, $src); } } } /** * 初始化截图对象 * @param filename 源文件路径全明 * @param width 截图的宽 * @param height 截图的高 * @param x 横坐标1 * @param y 纵坐标1 * @param x1 横坐标1 * @param y1 横坐标2 * */ private function initialize($filename, $x, $y, $x1, $y1) { if (file_exists($filename)) { $this->filename = $filename; $pathinfo = pathinfo($filename); $this->ext = $pathinfo['extension']; } else { $e = new Exception('the file is not exists!', 1050); throw $e; } $this->x = $x; $this->y = $y; $this->x1 = $x1; $this->y1 = $y1; } /** * 生成截图 * 根据图片的格式,生成不同的截图 */ private function generate_shot() { switch ($this->ext) { case 'jpg': return $this->generate_jpg(); break; case 'png': return $this->generate_png(); break; case 'gif': return $this->generate_gif(); break; default: return false; } } /** * 得到生成的截图的文件名 * */ private function get_shot_name() { $pathinfo = pathinfo($this->filename); $filename = $this->dst_name . "_" . $pathinfo['basename']; return $pathinfo['dirname'] . '/' . $filename; } /** * 生成jpg格式的图片 * */ private function generate_jpg() { $shot_name = $this->get_shot_name(); $img_r = imagecreatefromjpeg($this->filename); $dst_r = ImageCreateTrueColor($this->width, $this->height); imagecopyresampled($dst_r, $img_r, 0, 0, $this->x, $this->y, $this->width, $this->height, $this->x1, $this->y1); imagejpeg($dst_r, $shot_name, $this->jpeg_quality); return $shot_name; } /** * 生成gif格式的图片 * */ private function generate_gif() { $shot_name = $this->get_shot_name(); $img_r = imagecreatefromgif($this->filename); $dst_r = ImageCreateTrueColor($this->width, $this->height); imagecopyresampled($dst_r, $img_r, 0, 0, $this->x, $this->y, $this->width, $this->height, $this->x1, $this->y1); imagegif($dst_r, $shot_name); return $shot_name; } /** * 生成png格式的图片 * */ private function generate_png() { $shot_name = $this->get_shot_name(); $img_r = imagecreatefrompng($this->filename); $dst_r = ImageCreateTrueColor($this->width, $this->height); imagecopyresampled($dst_r, $img_r, 0, 0, $this->x, $this->y, $this->width, $this->height, $this->x1, $this->y1); imagepng($dst_r, $shot_name); return $shot_name; } }