189 8069 5689

PHP图片缩放

    /**
     * 图片缩放
     */
    function resizeImage($p_w_picpath, $prefix = '', $width = 266, $compression = 0)
    {
        //判断操作系统转换图片名称编码
        if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
        {
            $p_w_picpath = iconv('utf-8', 'gbk', $p_w_picpath);
        }
        //获取图片相关信息
        $imgInfo = getp_w_picpathsize($p_w_picpath);
        $imgInfo['width'] = $imgInfo[0];
        $imgInfo['height'] = $imgInfo[1];
        $width = $imgInfo['width'] < $width ? $imgInfo['width'] : $width;
        
        if(!empty($imgInfo['width']) && !empty($imgInfo['height']))
        {
            
            switch ($imgInfo['mime'])
            {
                case 'p_w_picpath/jpeg':
                    $imgRes = p_w_picpathcreatefromjpeg($p_w_picpath);
                    break;
                case 'p_w_picpath/gif':
                    $imgRes = p_w_picpathcreatefromgif($p_w_picpath);
                    break;
                case 'p_w_picpath/png':
                    $imgRes = p_w_picpathcreatefrompng($p_w_picpath);
                    break;
                case 'p_w_picpath/bmp':
                    $imgRes = p_w_picpathcreatefromwbmp($p_w_picpath);
                    break;
            }
            
            if($imgRes)
            {
                $proportion = $width / $imgInfo['width'];
                $height = $imgInfo['height'] * $proportion;

                $white = p_w_picpathcreatetruecolor($width, $height);

                //缩小图片
                p_w_picpathcopyresampled($white, $imgRes, 0, 0, 0, 0, $width, $height, $imgInfo['width'], $imgInfo['height']);

                $imgName = $prefix ? $prefix . $this->nameToChina($p_w_picpath) : $this->nameToChina($p_w_picpath);
                $p_w_picpathName = dirname($p_w_picpath) . DS . $imgName;
                if(file_exists($p_w_picpathName))
                {
                    unlink($p_w_picpathName);
                }
                
                if(!$compression)
                {
                    p_w_picpathpng($white, $p_w_picpathName);
                }
                else
                {
                    p_w_picpathjpeg($white,$p_w_picpathName);
                }
                


                //销毁资源
                p_w_picpathdestroy($white);
                p_w_picpathdestroy($imgRes);
            }
        }
    }

文章名称:PHP图片缩放
本文网址:http://cdxtjz.cn/article/peehdp.html

其他资讯