联系信息

php根据地址栏参数自动生成缩略图

2021-05-07 16:55 By 致远 1075
当前位置: 企业网站建设 > ThinkPHP > php根据地址栏参数自动生成缩略图

首先,为什么需要缩略图?

以传统的企业网站为例,一般有首页、栏目页和详情页面,对于首页、栏目页,调用的图片很多,如果直接使用原图势必造成加载迟钝的问题,所以,缩略图是必须的!列表页面调用缩略图,详情页面调用原图,这样既不影响使用又能提升网站的响应速度。

对此,您可以在上传图片的时候自动生成缩略图即可。

为什么需要不同尺寸的缩略图?

以淘宝详情页面为例(类似的需求还有很多)。其中顶部大图,有小图、中等图片和原图三种形式,而事实上我们可能用到更多不同尺寸的缩略图形式。此时,如果在上传图片的时候就生成多种尺寸的图片,不是不行,缺点不少:

1、后期图片尺寸要求发生变化,需要重新跑一遍。

2、有的图片可能需要多种尺寸,而大部分图片都不需要多尺寸,这时如果都生成势必造成空间的浪费。

3、前台需要按照后端生成的缩略图的命名规则去调用

怎么按需生成缩略图?

基本思路:针对图像文件进行重定向,如果存在文件则直接返回,不存在则通过程序生成并返回。

apache的rewrite规则如下:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^upfiles/(.*)$ images.php?url=$1 [L]  

iis的rewrite规则如下:


<rule name="picSRC">
    <match url="^upfiles/(.*)$" />
    <conditions logicalGrouping="MatchAny">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="image.php?url={R:1}" />
</rule>


php根据地址栏参数自动生成缩略图的核心代码:

$url = $_GET['url'];
$src = './upfiles/' . preg_replace('/_(\d+)x(\d+)/', '', $url);
$filename = './upfiles/' . $url;
if (file_exists($filename)) {
    ob_start();
    header('Content-type:image/jpeg');
    readfile($filename);
    ob_flush();
    flush();
} else {
    if(!preg_match('/_(\d+)x(\d+)/', $url, $wh)){
        exit();
    }
    $width = $wh[1];
    $height = $wh[2];
    thumb(realpath($src), $width, $height, $filename, 'crop', '85');
}
function thumb($src, $width, $height, $filename, $mode = 'scale', $quality = '100') {
    try {
        $imageValue = getimagesize($src);
        //$mime=$imageValue['mime'];
        $sourceWidth = $imageValue[0]; //原图宽
        $sourceHeight = $imageValue[1]; //原图高
        $thumbWidth = $width; //缩略图宽
        $thumbHeight = $height; //缩略图高
        $_x = 0;
        $_y = 0;
        $w = $sourceWidth;
        $h = $sourceHeight;
        if ($mode == 'scale') {
            if ($sourceWidth <= $thumbWidth && $sourceHeight <= $thumbHeight) {
                $_x = floor(($thumbWidth - $sourceWidth) / 2);
                $_y = floor(($thumbHeight - $sourceHeight) / 2);
                $thumbWidth = $sourceWidth;
                $thumbHeight = $sourceHeight;
            } else {
                if ($thumbHeight * $sourceWidth > $thumbWidth * $sourceHeight) {
                    $thumbHeight = floor($sourceHeight * $width / $sourceWidth);
                    $_y = floor(($height - $thumbHeight) / 2);
                } else {
                    $thumbWidth = floor($sourceWidth * $height / $sourceHeight);
                    $_x = floor(($width - $thumbWidth) / 2);
                }
            }
        } else if ($mode == 'crop') {
            if ($sourceHeight < $thumbHeight) { //如果原图尺寸小于当前尺寸
                $thumbWidth = floor($thumbWidth * $sourceHeight / $thumbHeight);
                $thumbHeight = $sourceHeight;
            }
            if ($sourceWidth < $thumbWidth) {
                $thumbHeight = floor($thumbHeight * $sourceWidth / $thumbWidth);
                $thumbWidth = $sourceWidth;
            }
            $s1 = $sourceWidth / $sourceHeight; //原图比例
            $s2 = $width / $height; //新图比例
            if ($s1 == $s2) {

            } else if ($s1 > $s2) { //全高度
                $y = 0;
                $ax = floor($sourceWidth * ($thumbHeight / $sourceHeight));
                $x = ($ax - $thumbWidth) / 2;
                $w = $thumbWidth / ($thumbHeight / $sourceHeight);
            } else { //全宽度
                $x = 0;
                $ay = floor($sourceHeight * ($thumbWidth / $sourceWidth)); //模拟原图比例高度
                $y = ($ay - $thumbHeight) / 2;
                $h = $thumbHeight / ($thumbWidth / $sourceWidth);
            }
        }
        //应该先缩放到目标尺寸再进行裁剪;
        switch ($imageValue[2]) {
            case 2:
                $source = imagecreatefromjpeg($src);
                break;
            case 1:
                $source = imagecreatefromgif($src);
                break;
            case 3:
                $source = imagecreatefrompng($src);
                break;
            case 6:
                $source = imagecreatefromwbmp($src);
                break;
            default:
                break;
        }
        header("Content-type: image/jpeg");
        $thumb = imagecreatetruecolor($width, $height);
        imagefill($thumb, 0, 0, imagecolorallocate($thumb, 255, 255, 255));
        imagecopyresampled($thumb, $source, 0, 0, $x, $y, $width, $height, $w, $h);
        imagejpeg($thumb, null, $quality);
        // if ($_SERVER['HTTP_REFERER'] || false !== stripos($_SERVER['HTTP_REFERER'], 'http://' . $_SERVER['SERVER_NAME'])) {
        imagejpeg($thumb, $filename, $quality);
        // }
        imagedestroy($thumb);
        imagedestroy($source);
    } catch (Exception $ex) {
        //defulat();
    }


php根据地址栏参数自动生成缩略图的调用方法:

<img src="/upfiles/xxx/demo_400x400.jpg" />
<img src="/upfiles/xxx/demo_400x300.jpg" />
<img src="/upfiles/xxx/demo_200x160.jpg" />


© 致远 2021-05-07,原创内容,转载请注明出错:php根据地址栏参数自动生成缩略图

留下您的评论

>