`
hudeyong926
  • 浏览: 2018339 次
  • 来自: 武汉
社区版块
存档分类
最新评论

上传 缩略图 水印

阅读更多

缩略图,水印

 

生成缩列图有二种方式:

一,提前生成好,供调用

缩列图常规做法是,开始根据网站中的图片规格,要生成多少种缩列图,图片上传过程中就会生成缩列,供不同需求去调用。像这种方法有一点不好就是,如果增加图片规格以后,以前上传的图片没有该规格的,还要重新生成。见附件

二,调用的时候在生成缩列图。这也是我写这篇博客原因。

推荐一个timthumb,个人觉得在不考虑性能,效率的情况下,这个东西,绝对是好用的。

1,下载地址:http://timthumb.googlecode.com/svn/trunk/timthumb.php

2,使用方法

下载下来后,放到你的web环境中,在与timthumb.php平行的目录中建一个cache文件夹,给于755权限,windows的话,就不用了。测试代码如下:

<?php

$image = "http://localhost/test/pen_pic03.png";
//$ALLOWED_SITES 需设置
?>
<html>
<head>
<title>timthumb</title>
</head>
<body>
 <img src="http://localhost/test/timthumb.php?src=<?php echo $image; ?>&w=58&h=46&zc=1" alt="" />
</body>
</html>

参数说明:

w=58 表示图片宽为58

h=46 表示图片高为46

zc = 1/2/3

可以根据需要,随便设置。

timthumb最大的优点就是方便,目前还没有遇到过这样的工具,缺点同样明显,每次请求的时候,都会调用php-cgi或者是php-cli,缓存的数据放在cache目录下,调用数据要去查找目录,性能上会比较低。

 

<?php

/**
 * 不变型裁剪及图片按比例裁剪
 */
class resizeimage
{

    /**
     *$source_path传入的图片路径
     *$target_width生成缩略图的宽度
     *$target_height生成缩略图的高度
     *$save_path生成之后保存的路径
     *$output 浏览器输出
     */
    public function imagecropper($source_path, $target_width, $target_height, $save_path = '', $output = true)
    {
        $filename = basename($source_path); //获取传入的文件名
        $fname = explode('.', $filename);
        $newfile = $save_path . $fname[0] . "_$target_width" . "_$target_height." . $fname[1];
        if (file_exists($newfile)) //判断文件是否存在,存在就不生成
        {
            if ($output) {
                header('Content-Type: image/jpeg');
                readfile($newfile); //输出图像
            }
            die;
        }
        $source_info = getimagesize($source_path);
        $source_width = $source_info[0];
        $source_height = $source_info[1];
        $source_mime = $source_info['mime'];
        $source_ratio = $source_height / $source_width;
        $target_ratio = $target_height / $target_width;

        // 源图过高
        if ($source_ratio > $target_ratio) {
            $cropped_width = $source_width;
            $cropped_height = $source_width * $target_ratio;
            $source_x = 0;
            $source_y = ($source_height - $cropped_height) / 2;
        } // 源图过宽
        elseif ($source_ratio < $target_ratio) {
            $cropped_width = $source_height / $target_ratio;
            $cropped_height = $source_height;
            $source_x = ($source_width - $cropped_width) / 2;
            $source_y = 0;
        } // 源图适中
        else {
            $cropped_width = $source_width;
            $cropped_height = $source_height;
            $source_x = 0;
            $source_y = 0;
        }

        switch ($source_mime) {
            case 'image/gif':
                $source_image = imagecreatefromgif($source_path);
                break;

            case 'image/jpeg':
                $source_image = imagecreatefromjpeg($source_path);
                break;

            case 'image/png':
                $source_image = imagecreatefrompng($source_path);
                break;

            default:
                return false;
                break;
        }

        $target_image = imagecreatetruecolor($target_width, $target_height);
        $cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);

        // 裁剪
        imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
        // 缩放
        imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);

        if ($output) {
            header('Content-Type: image/jpeg');
            imagejpeg($target_image); //输出图像
        }
        imagejpeg($target_image, $newfile, 80);

        imagedestroy($source_image);
        imagedestroy($target_image);
        imagedestroy($cropped_image);
    }
}

?>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics