_openImage($filePath); // 将重绘后的图片流赋值使用 ob_start(); // 开始输出缓存. $fileName = $file['name']; $ext = pathinfo($fileName, PATHINFO_EXTENSION); // 后缀 if ($ext == 'jpg' || $ext == 'jpeg') { imagejpeg($this->image); // imagejpeg()通常会输出图像, 由于ob_start(), 所以不会. } if ($ext == 'png') { imagepng($this->image); // imagepng()通常会输出图像, 由于ob_start(), 所以不会. } $contents = ob_get_contents(); // 把上面的输出保存为 $content ob_end_clean(); // 结束输出缓存. imagedestroy($this->image); return $contents; } /** * 内部:打开图片 */ private function _openImage($src) { list($width, $height, $type, $attr) = getimagesize($src); $this->imageinfo = array( 'width' => $width, 'height' => $height, 'type' => image_type_to_extension($type, false), 'attr' => $attr ); $fun = "imagecreatefrom" . $this->imageinfo['type']; $this->image = $fun($src); $this->_thumpImage(); } /** * 内部:操作图片 */ private function _thumpImage() { $new_width = $this->imageinfo['width'] * $this->percent; $new_height = $this->imageinfo['height'] * $this->percent; $image_thump = imagecreatetruecolor($new_width, $new_height); //绘制图片透明底色 imagesavealpha($image_thump, true); $black = imagecolorallocate($image_thump, 0, 0, 0); imagefilledrectangle($image_thump, 0, 0, 150, 25, $black); $trans_colour = imagecolorallocatealpha($image_thump, 0, 0, 0, 127); imagefill($image_thump, 0, 0, $trans_colour); //将原图复制带图片载体上面,并且按照一定比例压缩,极大的保持了清晰度 imagecopyresampled($image_thump, $this->image, 0, 0, 0, 0, $new_width, $new_height, $this->imageinfo['width'], $this->imageinfo['height']); imagedestroy($this->image); $this->image = $image_thump; } }