php 创建缩略图

全屏阅读
  • 基本信息
/**
 * @param $image_path  //原始图片路径
 * @param $thumb_width  //缩略图 宽度
 * @param $thumb_height  //缩略图 高度
 * @param $thumb_pro    //生成方式  1 约束宽高缩放(缩略图和原图 比例不一致会拉伸)   2  约束宽度  3  约束高度   4 按尺寸生成约束宽高 (不拉伸)
 * @param $thumb_path
 * @param int $ml
 * @param int $thumb_quality
 */
function CreateThumbs($image_path, $thumb_width, $thumb_height, $thumb_pro, $thumb_path, $ml = 1, $thumb_quality = 100)
{
    if (!extension_loaded('gd')) {
        die('GD库未安装或未启用');
    }
    if (!empty($thumb_path) && ($thumb_width > 0 || $thumb_height > 0)) {
        /*
        $thumb_array=explode('/',$thumb_path);
        if (count($thumb_array)>=2){
            $thumb_parent='';
            if ($ml==1) $thumb_parent='../';
            $thumb_dir=$thumb_parent.$thumb_array[0].'/'.$thumb_array[1].'/';
            if (is_date($thumb_array[1]) && !is_dir($thumb_dir)) mkdir($thumb_dir);
        }
        */
        if ($ml == 1) {
            $image_path = '../' . $image_path;
            $thumb_path = '../' . $thumb_path;
        }
        if (!is_file($image_path)) return;
        $image_size = getimagesize($image_path);
        switch ($thumb_pro) {
            case 1:
                $image_width  = $image_size[0];
                $image_height = $image_size[1];
                if ($image_size[0] > $thumb_width && $image_size[1] > $thumb_height) {
                    $image_width  = $image_size[0] / $image_size[1] * $thumb_height;
                    $image_height = $thumb_height;
                    if ($image_width > $thumb_width) {
                        $image_width  = $thumb_width;
                        $image_height = $image_size[1] / $image_size[0] * $thumb_width;
                    }
                } elseif ($image_size[0] > $thumb_width && $image_size[1] <= $thumb_height) {
                    $image_width  = $thumb_width;
                    $image_height = $image_size[1] / $image_size[0] * $thumb_width;
                } elseif ($image_size[1] > $thumb_height && $image_size[0] <= $thumb_width) {
                    $image_width  = $image_size[0] / $image_size[1] * $thumb_height;
                    $image_height = $thumb_height;
                }
                break;
            case 2:
                $image_width  = $thumb_width;
                $image_height = $image_size[1] / $image_size[0] * $thumb_width;
                if ($image_size[0] < $thumb_width) {
                    $image_width  = $image_size[0];
                    $image_height = $image_size[1];
                }
                break;
            case 3:
                $image_width  = $image_size[0] / $image_size[1] * $thumb_height;
                $image_height = $thumb_height;
                if ($image_size[1] < $thumb_height) {
                    $image_width  = $image_size[0];
                    $image_height = $image_size[1];
                }
                break;
            case 4:
                //等比缩放,居中显示
                $old_width  = $image_size[0];
                $old_height = $image_size[1];
                // 计算缩放比例
                $scale = min($thumb_width / $old_width, $thumb_height / $old_height);
                $image_width = round($scale * $old_width);
                $image_height = round($scale * $old_height);
                break;
            default:
                $image_width  = $thumb_width;
                $image_height = $thumb_height;
                break;
        }
        switch ($image_size[2]) {
            case 1: //gif
                $image_path = imagecreatefromgif($image_path);
                break;
            case 2: //jpg
                $image_path = imagecreatefromjpeg($image_path);
                break;
            case 3: //png
                $image_path = imagecreatefrompng($image_path);
                imagesavealpha($image_path, true);
                break;
            case 6: //bmp
                break;
        }
        //$image = imagecreatetruecolor($image_width, $image_height);
        $image = imagecreatetruecolor($thumb_width, $thumb_height);
        if ($image_size[2] == 3) {
            //imagealphablending($image, false);
            //imagesavealpha($image, true);
            $transparentColor = imagecolorallocatealpha($image, 255, 255, 255, 127); // 最后一个参数是alpha值,127表示50%透明
            imagefill($image, 0, 0, $transparentColor);
        } else {
            $color = imagecolorallocate($image, 255, 255, 255);
            imagefill($image, 0, 0, $color);
        }
        // 计算居中坐标
        $dstX = ($thumb_width - $image_width) / 2;
        $dstY = ($thumb_height - $image_height) / 2;
        imagecopyresampled($image, $image_path,$dstX,$dstY, 0, 0, $image_width, $image_height, $image_size[0], $image_size[1]);
        switch ($image_size[2]) {
            case 1: //gif
                imagegif($image, $thumb_path, $thumb_quality);
                break;
            case 2: //jpg
                imagejpeg($image, $thumb_path, $thumb_quality);
                break;
            case 3: //png
                imagepng($image, $thumb_path);
                break;
            case 6: //bmp
                break;
        }
        imagedestroy($image);
    }
}

顶一下
(0)
100%
订阅 回复
踩一下
(0)
100%
» 固定链接:恒富网 » 《php 创建缩略图》
» 郑重声明:本文由mpxq168发布,所有内容仅代表个人观点。版权归恒富网mpxq168共有,欢迎转载, 但未经作者同意必须保留此段声明,并给出文章连接,否则保留追究法律责任的权利! 如果本文侵犯了您的权益,请留言。

目前有 0 条留言 其中:访客:0 条, 博主:0 条

给我留言

您必须 [ 登录 ] 才能发表留言!