标签归档:php图片略缩

改了一下oscommerce…

原因就是商品原图片太大的话,浏览目录会消耗很多带宽。

因为图片显示的时候没有略缩图,直接是让浏览器下载原图再按比例缩小显示了。

这样首页和目录的下载时间太长而看原图就很快(都下来本地了当然快)。

增加里一个small_img.php

主要代码:

if (isset($_GET[‘p’])

isset($_GET[‘w’])

isset($_GET[‘h’])) {

$imgfile = $_GET[‘p’];

$width = $_GET[‘w’];

$height = $_GET[‘h’];

list($owidth, $oheight, $type, $attr) = getimagesize($imgfile);

switch ($type) {

case 1: $img = imagecreatefromgif($imgfile); break;

case 2: $img = imagecreatefromjpeg($imgfile); break;

case 3: $img = imagecreatefrompng($imgfile); break;

default: $img = imagecreate($width, $height);

}

$newImg = imagecreatetruecolor($width, $height);

imagecopyresampled($newImg, $img, 0, 0, 0, 0, $width, $height, $owidth, $oheight);

switch ($type) {

case 1:

Header(

Content-type: image/gif

);

imagegif($newImg);

break;

case 2:

Header(

Content-type: image/jpeg

);

imagejpeg($newImg);

break;

case 3:

Header(

Content-type: image/png

);

imagepng($newImg);

break;

}

ImageDestroy($newImg);

ImageDestroy($img);

}

现在机器那么强,动态生成一批小图也不算什么吧。。。

然后修改html_output.php里面的tep_image

原:

$image = ‘

img src=

‘ . tep_output_string($src) . ‘

border=

0

alt=

‘ . tep_output_string($alt) . ‘

‘;

新:

$image_size = @getimagesize($src);

if (!empty($width)

!empty($height)

$image_size[0]

$width

$image_size[1]

$height)

$image = ‘

img src=

small_img.php?p=’ . tep_output_string($src) . ‘

w=’.$width.’

h=’.$height.’

border=

0

alt=

‘ . tep_output_string($alt) . ‘

‘;

else

$image = ‘

img src=

‘ . tep_output_string($src) . ‘

border=

0

alt=

‘ . tep_output_string($alt) . ‘

‘;

只有确定略缩的情况下才改变这个img tag。

效果请看:

http://www.shenzhenmobile.com/

用的是godaddy的域名+空间,原来的图都那么大,国内看真是慢死了。