PHP缩略图类ImageResize
web PHP 2019-04-17 1751 0
关于本站

“最难不过坚持”

本人承接扒站仿站,php网站维护,病毒查杀,网站编辑,网站改版,html制作

有需要网站维护,改版,病毒查杀,网站编辑,网站备案,html制作等相关的工作可以联系我。
本人有多年相关工作经验,也可提供免费咨询,交个朋友。
有需要探讨问题的朋友,也可以加我微信,共同探讨!
微信:15011482830 QQ:408917339

6399400 2609 39
最新评论
https://jueru.net/
评 留言
:weixiao:
评 留言
:shuijiao: :weiqu: :zhenbang: :leng:
评 留言
:yiwen: :yiwen: :yiwen: :yiwen:
评 EasySass: could not generate CSS file. See Output panel for details.
这个业务逻辑多少都有点奇怪了,阅读浏览次数增值在新闻详情页的控制器方法里setInc,这怎么还写进模型事件里了。如果非要用onAfterRead也可以,把新闻文章的内容单独分出来一个news_content表,然后把它和news做关联,然后给news_content表的onAfterRead事件做增值处理,这样点进新闻页内查询到文章内容时才会触发它。
评 TP6模型事件-查询后onAfterRead不好用
文章标签更多
ThinkPHP (254)
Mysql (58)
DedeCms (33)
jQuery (67)
证件照 (1)
setInc (4)
setDec (4)
onclick (5)
打开边栏(ESC) 关闭边栏(ESC)

PHP缩略图类ImageResize,分别生成三种不同尺寸的图片。调用方法很简单只要传参数高度和宽度,及新图片的名称。

缩略图类ImageResize.class.php

<?php
/**
 * 缩略图生成类,使用示例:
 */
/*
$newimage=new ImageResize();
$newimage->resize("1.jpg","1_lit.jpg",644,373);
echo $newimage->GetLastError();
 */
class ImageResize{
    private $localimage;//原图路径
    private $remoteimage;//缩略图保存路径
    private $localinfo;//原图属性
    private $error;
            
    function resize($localimg, $remoteimg, $x, $y) {
        //检测是否支持gd图像处理
        if(!$this->_checkenv()){
            return false;
        }
        $this->localimage = $localimg;
        $this->remoteimage = $remoteimg;
        $this->localinfo = getimagesize($this->localimage); //获取本地图像的信息
        return $this->_resize($x,$y);
    }
    /**
     * 检测当前环境是否支持GD
     */
    private function _checkenv(){
        $gd_info=gd_info();
        if(!$gd_info){
            $this->error[]="当前环境不支持GD图像处理,请先安装GD库并开启PHP相关扩展";
            return false;
        }
        return true;
    }
    
    /**
     * 生成缩略图主函数
     * @param int $x 指定的缩略图宽度
     * @param int $y 指定的缩略图高度
     * @return boolean
     */
    private function _resize($x,$y){
        if(!$this->localinfo){
            $this->error[]="本地图像文件不存在";
            return false;
        }
        //创建图像句柄
        $im=@$this->_create($this->localinfo[2]);
        if(!$im){
            $this->error[]="当前GD库不支持图像类型:{$this->localinfo['mime']}";
            return false;
        }
        $dstsize=$this->_dstsize($x, $y);
        $dstim=@imagecreatetruecolor($dstsize["width"],$dstsize["height"]);
        $whitecolor=@imagecolorallocatealpha($dstim, 0, 0, 0,127);
        imagefill($dstim,0,0,$whitecolor);
        $re=@imagecopyresampled($dstim, $im, 0, 0, 0, 0, $dstsize["width"], $dstsize["height"], $this->localinfo[0], $this->localinfo[1]);
        if(!$re){
            $this->error[]="图像重新采样失败";
            return false;
        }
        if(!imagejpeg($dstim, $this->remoteimage)){
            if(!imagepng($dstim,$this->remoteimage)){
                if(!imagegif($dstim,$this->remoteimage)){
                    $this->error[]="保存缩略图到{$this->remoteimage}失败,请检查gd环境是否正常和缩略图文件夹的写入权限。";
                    return false;
                }
            }
        }
        $this->error[]="success";
        return true;
    }
    
    /**
     * 根据本地图片类型,创建图片资源
     * @param 图像类型代码 $code
     * @return resource/boolean 成功则返回resourse失败则返回false
     */
    private function _create($code){
        $src=$this->localimage;
        switch ($code){
            case 1:
                return imagecreatefromgif($src);
                break;
            case 2:
                return imagecreatefromjpeg($src);
                break;
            case 3:
                return imagecreatefrompng($src);
                break;
            default :
                return false;
                break;
        }
    }
    
    /**
     * 按比例计算合适的宽度
     * @param int $x 指定的缩略图宽度
     * @param int $y 指定的缩略图高度
     * @return array 包含调整后的缩略图宽度和高度
     */
    private function _dstsize($x,$y){
        list($srcwidth,$srcheight)=$this->localinfo;
        if(($srcwidth/$srcheight)<($x/$y)){
            $x=floor($y*$srcwidth/$srcheight);
        }else{
            $y=floor($x*$srcheight/$srcwidth);
        }
        $dstsize["width"]=$x;
        $dstsize["height"]=$y;
        return $dstsize;
    }


    /**
     * 获取最后一条错误信息
     * return string
     */
    function GetLastError(){
        return array_pop($this->error);
    }
    
    /**
     * 获取所有错误信息
     * return array
     */
    function GetAllError(){
        return $this->error;
    }
}
使用

<?php 
header("Content-type: text/html; charset=utf-8"); 
include_once 'ImageResize.class.php'; 
//实例化缩略图类  
$newimage = new ImageResize(); 
//将当前目录下的test.jpg生成缩略图并保存到test_400_400.jpg,指定的宽度高度分别是400和400像素 
$newimage->resize("demo.jpg", "demo_400_400.jpg", 400, 400); 
$newimage->resize("demo.jpg", "demo_200_200.jpg", 200, 200); 
$newimage->resize("demo.jpg", "demo_50_50.jpg", 50, 50); 
//获取最后一条错误信息,如果生成成功则输出'success' 
echo "<p style='color:red;font-weight:bold;text-align:center;font-size:16px;margin:50px 0 20px'>结果:".$newimage->GetLastError()."</p>"; 
?>

版权声明:本篇文章来源于网络。 来源链接

相关推荐
php实现ZIP压缩文件解压缩,中文乱码解决方法(重要)
PHP | 2019-04-30 5599
直接上代码,具体代码里面都有注释。直接中文压缩文件解压到中文文件夹。 <?php // 需开启配置 php_zip.dll // phpinfo(); header("Content-type:text/html;charset=utf-8"); /* * $filename 被解压文件名 * $path 解压...
openssl_private_decrypt解密失败
PHP | 2019-01-16 5574
复制别人的解密程序,原程序可以解密,复制过来就不可以 一步步尝试发现,秘钥换行符有区别, 原秘钥有换行符,复制过来不知道什么时候把换行符清空了,成了一行字符串了。
评论:0条
评论加载中...
发表评论