缩略图用得最多的是我们上传图片时生成一张小图,这样就可以很好的解决图片大小或影响网站整体美观的问题了,下面介绍的生成图片缩略图类都不支持图片上传,我们要先上传再调用此类来操作。
//使用如下类就可以生成图片缩略图,
代码如下 复制代码
srcimg = $img;
$this->resize_width = $wid;
$this->resize_height = $hei;
$this->cut = $c;
//图片的类型
$this->type = strtolower(substr(strrchr($this->srcimg,.),1));
//初始化图象
$this->initi_img();
//目标图象地址
$this -> dst_img($dstpath);
//--
$this->width = imagesx($this->im);
$this->height = imagesy($this->im);
//生成图象
$this->newimg();
imagedestroy ($this->im);
}
function newimg()
{
//改变后的图象的比例
$resize_ratio = ($this->resize_width)/($this->resize_height);
//实际图象的比例
$ratio = ($this->width)/($this->height);
if(($this->cut)==1)
//裁图
{
if($ratio>=$resize_ratio)
//高度优先
{
$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this-
>resize_height, (($this->height)*$resize_ratio), $this->height);
imagejpeg ($newimg,$this->dstimg);
}
if($ratio //宽度优先
{
$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this-
>resize_height, $this->width, (($this->width)/$resize_ratio));
imagejpeg ($newimg,$this->dstimg);
}
}
else
//不裁图
{
if($ratio>=$resize_ratio)
{
$newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this-
>resize_width)/$ratio, $this->width, $this->height);
imagejpeg ($newimg,$this->dstimg);
}
if($ratio {
$newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio,
$this->resize_height, $this->width, $this->height);
imagejpeg ($newimg,$this->dstimg);
}
}
}
//初始化图象
function initi_img()
{
if($this->type==jpg)
{
$this->im = imagecreatefromjpeg($this->srcimg);
}
if($this->type==gif)
{
$this->im = imagecreatefromgif($this->srcimg);
}
if($this->type==png)
{
$this->im = imagecreatefrompng($this->srcimg);
}
}
//图象目标地址
function dst_img($dstpath)
{
$full_length = strlen($this->srcimg);
$type_length = strlen($this->type);
$name_length = $full_length-$type_length;
$name = substr($this->srcimg,0,$name_length-1);
$this->dstimg = $dstpath;
//echo $this->dstimg;
}
}
?>
类的调用方法
$resizeimage = new resizeimage(图片源文件地址, 200, 100, 0,缩略图地址);
//就只用上面的一句话,就能生成缩略图,其中,源文件和缩略图地址可以相同,200,100分别代表宽和高
实例2
php缩略图 等比例无损压缩,可填充空白区域补充色
代码如下 复制代码
0 ) {
$dst_w = $src_width * ( $dst_height / $src_height );
$dst_x = ( $dst_width - $dst_w ) / 2;
} elseif ( ($dst_width / $dst_height - $src_width / $src_height) $dst_h = $src_height * ( $dst_width / $src_width );
$dst_y = ( $dst_height - $dst_h ) / 2;
}
imagecopyresampled($dstimg, $srcimg, $dst_x
, $dst_y, 0, 0, $dst_w, $dst_h, $src_width, $src_height);
// 保存格式
$arr = array(
'jpg' => 'imagejpeg'
, 'jpeg' => 'imagejpeg'
, 'png' => 'imagepng'
, 'gif' => 'imagegif'
, 'bmp' => 'imagebmp'
);
$suffix = strtolower( array_pop(explode('.', $dstimage ) ) );
if (!in_array($suffix, array_keys($arr)) ) {
echo 保存的文件名错误;
exit;
} else {
eval( $arr[$suffix] . '($dstimg, '.$dstimage.');' );
}
imagejpeg($dstimg, $dstimage);
imagedestroy($dstimg);
imagedestroy($srcimg);
}
function getcreatemethod( $file ) {
$arr = array(
'474946' => imagecreatefromgif('$file')
, 'ffd8ff' => imagecreatefromjpeg('$file')
, '424d' => imagecreatefrombmp('$file')
, '89504e' => imagecreatefrompng('$file')
);
$fd = fopen( $file, rb );
$data = fread( $fd, 3 );
$data = str2hex( $data );
if ( array_key_exists( $data, $arr ) ) {
return $arr[$data];
} elseif ( array_key_exists( substr($data, 0, 4), $arr ) ) {
return $arr[substr($data, 0, 4)];
} else {
return false;
}
}
function str2hex( $str ) {
$ret = ;
for( $i = 0; $i $ret .= ord($str[$i]) >= 16 ? strval( dechex( ord($str[$i]) ) )
: '0'. strval( dechex( ord($str[$i]) ) );
}
return strtoupper( $ret );
}
// bmp 创建函数 php本身无
function imagecreatefrombmp($filename)
{
if (! $f1 = fopen($filename,rb)) return false;
$file = unpack(vfile_type/vfile_size/vreserved/vbitmap_offset, fread($f1,14));
if ($file['file_type'] != 19778) return false;
$bmp = unpack('vheader_size/vwidth/vheight/vplanes/vbits_per_pixel'.
'/vcompression/vsize_bitmap/vhoriz_resolution'.
'/vvert_resolution/vcolors_used/vcolors_important', fread($f1,40));
$bmp['colors'] = pow(2,$bmp['bits_per_pixel']);
if ($bmp['size_bitmap'] == 0) $bmp['size_bitmap'] = $file['file_size'] - $file['bitmap_offset'];
$bmp['bytes_per_pixel'] = $bmp['bits_per_pixel']/8;
$bmp['bytes_per_pixel2'] = ceil($bmp['bytes_per_pixel']);
$bmp['decal'] = ($bmp['width']*$bmp['bytes_per_pixel']/4);
$bmp['decal'] -= floor($bmp['width']*$bmp['bytes_per_pixel']/4);
$bmp['decal'] = 4-(4*$bmp['decal']);
if ($bmp['decal'] == 4) $bmp['decal'] = 0;
$palette = array();
if ($bmp['colors'] {
$palette = unpack('v'.$bmp['colors'], fread($f1,$bmp['colors']*4));
}
$img = fread($f1,$bmp['size_bitmap']);
$vide = chr(0);
$res = imagecreatetruecolor($bmp['width'],$bmp['height']);
$p = 0;
$y = $bmp['height']-1;
while ($y >= 0)
{
$x=0;
while ($x {
if ($bmp['bits_per_pixel'] == 24)
$color = unpack(v,substr($img,$p,3).$vide);
elseif ($bmp['bits_per_pixel'] == 16)
{
$color = unpack(n,substr($img,$p,2));
$color[1] = $palette[$color[1]+1];
}
elseif ($bmp['bits_per_pixel'] == 8)
{
$color = unpack(n,$vide.substr($img,$p,1));
$color[1] = $palette[$color[1]+1];
}
elseif ($bmp['bits_per_pixel'] == 4)
{
$color = unpack(n,$vide.substr($img,floor($p),1));
if (($p*2)%2 == 0) $color[1] = ($color[1] >> 4) ; else $color[1] = ($color[1] & 0x0f);
$color[1] = $palette[$color[1]+1];
}
elseif ($bmp['bits_per_pixel'] == 1)
{
$color = unpack(n,$vide.substr($img,floor($p),1));
if (($p*8)%8 == 0) $color[1] = $color[1] >>7;
elseif (($p*8)%8 == 1) $color[1] = ($color[1] & 0x40)>>6;
elseif (($p*8)%8 == 2) $color[1] = ($color[1] & 0x20)>>5;
elseif (($p*8)%8 == 3) $color[1] = ($color[1] & 0x10)>>4;
elseif (($p*8)%8 == 4) $color[1] = ($color[1] & 0x8)>>3;
elseif (($p*8)%8 == 5) $color[1] = ($color[1] & 0x4)>>2;
elseif (($p*8)%8 == 6) $color[1] = ($color[1] & 0x2)>>1;
elseif (($p*8)%8 == 7) $color[1] = ($color[1] & 0x1);
$color[1] = $palette[$color[1]+1];
}
else
return false;
imagesetpixel($res,$x,$y,$color[1]);
$x++;
$p += $bmp['bytes_per_pixel'];
}
$y--;
$p+=$bmp['decal'];
}
fclose($f1);
return $res;
}
// bmp 保存函数,php本身无
function imagebmp ($im, $fn = false)
{
if (!$im) return false;
if ($fn === false) $fn = 'php://output';
$f = fopen ($fn, w);
if (!$f) return false;
$biwidth = imagesx ($im);
$biheight = imagesy ($im);
$bibpline = $biwidth * 3;
$bistride = ($bibpline + 3) & ~3;
$bisizeimage = $bistride * $biheight;
$bfoffbits = 54;
$bfsize = $bfoffbits + $bisizeimage;
fwrite ($f, 'bm', 2);
fwrite ($f, pack ('vvvv', $bfsize, 0, 0, $bfoffbits));
fwrite ($f, pack ('vvvvvvvvvvv', 40, $biwidth, $biheight, 1, 24, 0, $bisizeimage, 0, 0, 0, 0));
$numpad = $bistride - $bibpline;
for ($y = $biheight - 1; $y >= 0; --$y)
{
for ($x = 0; $x {
$col = imagecolorat ($im, $x, $y);
fwrite ($f, pack ('v', $col), 3);
}
for ($i = 0; $i fwrite ($f, pack ('c', 0));
}
fclose ($f);
return true;
}
?>
总结,第一个类文件不如第二个好,因为第一个生成图之后图片会有点变模糊哦,后面这个生成类是高质量的。
http://www.bkjia.com/phpjc/632959.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/632959.htmltecharticle缩略图用得最多的是我们上传图片时生成一张小图,这样就可以很好的解决图片大小或影响网站整体美观的问题了,下面介绍的生成图片缩...