当前位置:Gxlcms > PHP教程 > php封装gd库

php封装gd库

时间:2021-07-01 10:21:17 帮助过:12人阅读

使用sae 版 thinkphp 在本地和sae 上无法实现缩略图,缩略图主要使用在瀑布流上面,不然一张图片2 3 M,速度太慢,就自己封装了一个,见笑!

max_width = $max_widht; $this->file_name = $file_name; $this->water_name = $water_name; } public function create_image(){ // 获得ori图片信息 list($width,$height,$type) = getimagesize($this->file_name); // 当原有图片大于 要求的最大宽度时,才需要进行压缩 if($width > $this->max_width){ // 获得图片压缩百分比 $per = $this->max_width / $width; $new_width = $width * $per; $new_height = $height * $per; }else{ $new_height = $height; $new_width = $width; } //创建一个真彩色图像 $image_p = imagecreatetruecolor($new_width, $new_height -10); $image = $this->image_obj($type, $this->file_name); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); $this->image_dump($type, $image_p, $this->file_name); $this->water(); } /* * 生成为图片添加水印 */ private function water(){ $water_name = $this->water_name; $dist_name = $this->file_name; list($dist_width,$dist_height,$type) = getimagesize($dist_name); $dist_im = $this->image_obj($type, $this->file_name); $water_name = "D:/xampps/htdocs/buyingfeiblog/1/App/Modules/Admin/Tpl/Public/Images/water.png"; list($w_width,$w_height) = getimagesize($water_name); // 获得图片水印信息 $water_src = imagecreatefrompng($water_name); // 设置图片水印位置 在右下角 $x = ($dist_width - $w_width) / 4 * 3 ; $y =($dist_height - $w_height) /4 * 3 ; if(imagecopy($dist_im, $water_src, $x, $y, 0, 0, $w_width, $w_height)){ imagepng($dist_im,$dist_name); echo "success"; }else{ echo "error"; } } // 生成图片类型,生成不同图片 保持图片原本类型不发生变化 private function image_dump($type,$image_p,$filename){ switch ($type){ case 1: imagegif($image_p, $filename); $dis_im = imagecreatefromgif(file_name); break; case 2: imagejpeg($image_p, $this->file_name); $dis_im = imagecreatefromjpeg(file_name); break; case 3: imagepng($image_p,file_name); $dis_im = imagecreatefrompng(file_name); break; default : } } // 根据图片不同,生成不同资源对象 private function image_obj($type,$filename){ switch ($type){ // 1 = GIF,2 = JPG,3 = PNG, case 1: $image = imagecreatefromgif($filename); break; case 2: $image = imagecreatefromjpeg($filename); break; case 3: $image = imagecreatefrompng($filename); break; default : } return $image; } } $thumb = new Thumb(725,"D:/xampps/htdocs/test/test.jpg"); $thumb->create_image();//create_image ?>

就是这么简单,
主要包括生成真彩图,
创建图形对象资源
图片进行合并,ok!

人气教程排行