年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2306|回复: 0

PHP简单图片处理类封装实现

[复制链接]
  • TA的每日心情

    2024-10-15 10:05
  • 签到天数: 372 天

    [LV.9]以坛为家II

    发表于 2016-1-9 23:26:49 | 显示全部楼层 |阅读模式
    项目链接:http://www.yusian.com/thread-11071-1-1.html
    [PHP] 纯文本查看 复制代码
    <?php
        class Image{
            private $path;
            // 构造方法,给定图片所在的目录
            public function __construct($path){
                $this->path = $path;
            }
             
            // 1、创建缩略图
            public function thumb($filename, $w, $h, $prefix){
                $file = $this->path."/".$filename;
                list($width, $height, $type) = getimagesize($file);
                // 修正比例
                $p = 1;
                if ($width > $height){
                    $p = $w / $width;
                    $h = $p * $height;
                }else{
                    $p = $h / $height;
                    $w = $p * $width;
                }
                // 创建图片资源和新图的图片资源
                $image = $this->get_image_source($file);
                $image_new = imagecreatetruecolor($w, $h);
                // 将原图的指定区域绘制到新图的指定区域即可
                imagecopyresampled($image_new, $image, 0, 0, 0, 0, $w, $h, $width, $height);
                // 输出图片并释放资源
                $save_image_method = "image".$this->file_type($file);
                $save_image_method($image_new, $this->path."/".$prefix.$filename);
                imagedestroy($image);
            }
             
            // 2、增加图片水印
            public function water_mark($filename, $string, $position, $save_name){
                $file = $this->path."/".$filename;
                list($width, $height, $type) = getimagesize($file);
                $image = $this->get_image_source($file);
                $color = imagecolorallocate($image, 128, 128, 128);
                $font = 5;
                $w = imagefontwidth($font) * strlen($string);
                $h = imagefontheight($font);
                $x = 10;
                $y = 10;
                switch($position){
                    case 1:{$x = 10; $y = 10;}break;
                    case 4:{$x = 10; $y = ($height - $h)*0.5;}break;
                    case 7:{$x = 10; $y = ($height - $h) - 10;}break;
                     
                    case 2:{$x = ($width - $w)*0.5; $y = 10;}break;
                    case 5:{$x = ($width - $w)*0.5; $y = ($height - $h)*0.5;}break;
                    case 8:{$x = ($width - $w)*0.5; $y = ($height - $h) - 10;}break;
                     
                    case  3:{$x = ($width - $w) - 10; $y = 10;}break;
                    case  6:{$x = ($width - $w) - 10; $y = ($height - $h)*0.5;}break;
                    default:{$x = ($width - $w) - 10; $y = ($height - $h) - 10;}break;
                }
                imagestring($image, $font, $x, $y, $string, $color);
                // 输出图片并释放资源
                $save_image_method = "image".$this->file_type($file);
                $new_filename = strlen($save_name) ? $save_name : $filename;
                $save_image_method($image, $this->path."/".$new_filename);
                imagedestroy($image);
            }
             
            // 3、使用unlink函数删除图片
            public function delete_image($filename){
                $file_path = $this->path."/".$filename;
                $thumb_path = $this->path."/thumb_".$filename;
                if(file_exists($file_path) && $filename)unlink($file_path);
                if(file_exists($thumb_path) && $filename)unlink($thumb_path);
            }
             
            /****************私有方法******************/
            // 获取图片文件类型
            private function file_type($file){
                list($width, $height, $type) = getimagesize($file);
                $type_array = array(1=>"gif", 2=>"jpeg", 3=>"png");
                return $type_array[$type];
            }
            // 创建图片文件资源
            private function get_image_source($file){
                $createimage = "imagecreatefrom".$this->file_type($file);
                return $createimage($file);
            }
        }

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2025-4-10 08:03 , Processed in 0.048139 second(s), 22 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表